| """ Predefined buttons with bound events that can be included in a gr.Blocks for convenience. """ |
|
|
| from __future__ import annotations |
|
|
| import json |
| from typing import Literal |
|
|
| from gradio_client.documentation import document, set_documentation_group |
|
|
| from gradio.components import Button, Component |
|
|
| set_documentation_group("component") |
|
|
|
|
| @document("add") |
| class ClearButton(Button): |
| """ |
| Button that clears the value of a component or a list of components when clicked. It is instantiated with the list of components to clear. |
| Preprocessing: passes the button value as a {str} into the function |
| Postprocessing: expects a {str} to be returned from a function, which is set as the label of the button |
| """ |
|
|
| is_template = True |
|
|
| def __init__( |
| self, |
| components: None | list[Component] | Component = None, |
| *, |
| value: str = "Clear", |
| variant: Literal["primary", "secondary", "stop"] = "secondary", |
| size: Literal["sm", "lg"] | None = None, |
| icon: str | None = None, |
| link: str | None = None, |
| visible: bool = True, |
| interactive: bool = True, |
| elem_id: str | None = None, |
| elem_classes: list[str] | str | None = None, |
| scale: int | None = None, |
| min_width: int | None = None, |
| **kwargs, |
| ): |
| super().__init__( |
| value, |
| variant=variant, |
| size=size, |
| icon=icon, |
| link=link, |
| visible=visible, |
| interactive=interactive, |
| elem_id=elem_id, |
| elem_classes=elem_classes, |
| scale=scale, |
| min_width=min_width, |
| **kwargs, |
| ) |
| self.add(components) |
|
|
| def add(self, components: None | Component | list[Component]) -> ClearButton: |
| """ |
| Adds a component or list of components to the list of components that will be cleared when the button is clicked. |
| """ |
| if not components: |
| |
| |
| |
| return self |
|
|
| if isinstance(components, Component): |
| components = [components] |
| clear_values = json.dumps( |
| [component.postprocess(None) for component in components] |
| ) |
| self.click(None, [], components, _js=f"() => {clear_values}") |
| return self |
|
|