mirror of
https://github.com/gradio-app/gradio.git
synced 2025-02-17 11:29:58 +08:00
Adds an every
parameter to every component (#2806)
* bigquery draft * updated guide * orjson fix * formatting * changelog * attach every * removed queue=False * added test * formatting * adding event * wip * fix implementation * remove prints * formatting * lint * formatting * reset guide * changelog * fix tests * added test * changelog * renamed * renamed * lint
This commit is contained in:
parent
20057aa946
commit
4e7317036d
13
CHANGELOG.md
13
CHANGELOG.md
@ -7,6 +7,19 @@ Adds a `gr.make_waveform()` function that creates a waveform video by combining
|
||||
|
||||
![waveform screenrecording](https://user-images.githubusercontent.com/7870876/206062396-164a5e71-451a-4fe0-94a7-cbe9269d57e6.gif)
|
||||
|
||||
### Allows Every Component to Accept an `every` Parameter
|
||||
|
||||
When a component's initial value is a function, the `every` parameter re-runs the function every `every` seconds. By [@abidlabs](https://github.com/abidlabs) in [PR 2806](https://github.com/gradio-app/gradio/pull/2806). Here's a code example:
|
||||
|
||||
```py
|
||||
import gradio as gr
|
||||
|
||||
with gr.Blocks() as demo:
|
||||
df = gr.DataFrame(run_query, every=60*60)
|
||||
|
||||
demo.queue().launch()
|
||||
```
|
||||
|
||||
## Bug Fixes:
|
||||
* Fixed issue where too many temporary files were created, all with randomly generated
|
||||
filepaths. Now fewer temporary files are created and are assigned a path that is a
|
||||
|
@ -834,7 +834,6 @@ class Blocks(BlockContext):
|
||||
for input_component, data in zip(block_fn.inputs, processed_input)
|
||||
}
|
||||
]
|
||||
|
||||
processed_input = add_request_to_inputs(
|
||||
block_fn.fn, list(processed_input), request
|
||||
)
|
||||
@ -1623,16 +1622,18 @@ class Blocks(BlockContext):
|
||||
for component in Context.root_block.blocks.values():
|
||||
if (
|
||||
isinstance(component, components.IOComponent)
|
||||
and component.attach_load_event
|
||||
and component.load_event_to_attach
|
||||
):
|
||||
load_fn, every = component.load_event_to_attach
|
||||
# Use set_event_trigger to avoid ambiguity between load class/instance method
|
||||
self.set_event_trigger(
|
||||
"load",
|
||||
component.load_fn,
|
||||
load_fn,
|
||||
None,
|
||||
component,
|
||||
no_target=True,
|
||||
queue=False,
|
||||
every=every,
|
||||
)
|
||||
|
||||
def startup_events(self):
|
||||
|
@ -33,6 +33,7 @@ from pandas.api.types import is_numeric_dtype
|
||||
|
||||
from gradio import media_data, processing_utils, utils
|
||||
from gradio.blocks import Block
|
||||
from gradio.context import Context
|
||||
from gradio.documentation import document, set_documentation_group
|
||||
from gradio.events import (
|
||||
Blurrable,
|
||||
@ -107,24 +108,23 @@ class IOComponent(Component, Serializable):
|
||||
visible: bool = True,
|
||||
elem_id: Optional[str] = None,
|
||||
load_fn: Optional[Callable] = None,
|
||||
every: Optional[float] = None,
|
||||
**kwargs,
|
||||
):
|
||||
super().__init__(elem_id=elem_id, visible=visible, **kwargs)
|
||||
|
||||
self.label = label
|
||||
self.show_label = show_label
|
||||
self.interactive = interactive
|
||||
|
||||
self.load_event = None
|
||||
self.load_event_to_attach = None
|
||||
load_fn, initial_value = self.get_load_fn_and_initial_value(value)
|
||||
self.value = self.postprocess(initial_value)
|
||||
if callable(load_fn):
|
||||
self.load_event = self.attach_load_event(load_fn, every)
|
||||
|
||||
self.set_interpret_parameters()
|
||||
if callable(load_fn):
|
||||
self.attach_load_event = True
|
||||
self.load_fn = load_fn
|
||||
else:
|
||||
self.attach_load_event = False
|
||||
self.load_fn = None
|
||||
|
||||
super().__init__(elem_id=elem_id, visible=visible, **kwargs)
|
||||
|
||||
def get_config(self):
|
||||
return {
|
||||
@ -248,6 +248,19 @@ class IOComponent(Component, Serializable):
|
||||
load_fn = None
|
||||
return load_fn, initial_value
|
||||
|
||||
def attach_load_event(self, callable: Callable, every: int | None):
|
||||
"""Add a load event that runs `callable`, optionally every `every` seconds."""
|
||||
if Context.root_block:
|
||||
return Context.root_block.load(
|
||||
callable,
|
||||
None,
|
||||
self,
|
||||
no_target=True,
|
||||
every=every,
|
||||
)
|
||||
else:
|
||||
self.load_event_to_attach = (callable, every)
|
||||
|
||||
def as_example(self, input_data):
|
||||
"""Return the input data in a way that can be displayed by the examples dataset component in the front-end."""
|
||||
return input_data
|
||||
@ -279,6 +292,7 @@ class Textbox(
|
||||
max_lines: int = 20,
|
||||
placeholder: Optional[str] = None,
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
interactive: Optional[bool] = None,
|
||||
visible: bool = True,
|
||||
@ -293,6 +307,7 @@ class Textbox(
|
||||
max_lines: maximum number of line rows to provide in textarea.
|
||||
placeholder: placeholder hint to provide behind textarea.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
interactive: if True, will be rendered as an editable textbox; if False, editing will be disabled. If not provided, this is inferred based on whether the component is used as an input or output.
|
||||
visible: If False, component will be hidden.
|
||||
@ -310,6 +325,7 @@ class Textbox(
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
interactive=interactive,
|
||||
visible=visible,
|
||||
@ -453,6 +469,7 @@ class Number(
|
||||
value: Optional[float | Callable] = None,
|
||||
*,
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
interactive: Optional[bool] = None,
|
||||
visible: bool = True,
|
||||
@ -464,6 +481,7 @@ class Number(
|
||||
Parameters:
|
||||
value: default value. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
interactive: if True, will be editable; if False, editing will be disabled. If not provided, this is inferred based on whether the component is used as an input or output.
|
||||
visible: If False, component will be hidden.
|
||||
@ -475,6 +493,7 @@ class Number(
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
interactive=interactive,
|
||||
visible=visible,
|
||||
@ -624,6 +643,7 @@ class Slider(Changeable, IOComponent, SimpleSerializable, FormComponent):
|
||||
*,
|
||||
step: Optional[float] = None,
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
interactive: Optional[bool] = None,
|
||||
visible: bool = True,
|
||||
@ -638,6 +658,7 @@ class Slider(Changeable, IOComponent, SimpleSerializable, FormComponent):
|
||||
value: default value. If callable, the function will be called whenever the app loads to set the initial value of the component. Ignored if randomized=True.
|
||||
step: increment between slider values.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
interactive: if True, slider will be adjustable; if False, adjusting will be disabled. If not provided, this is inferred based on whether the component is used as an input or output.
|
||||
visible: If False, component will be hidden.
|
||||
@ -656,6 +677,7 @@ class Slider(Changeable, IOComponent, SimpleSerializable, FormComponent):
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
interactive=interactive,
|
||||
visible=visible,
|
||||
@ -779,6 +801,7 @@ class Checkbox(Changeable, IOComponent, SimpleSerializable, FormComponent):
|
||||
value: bool | Callable = False,
|
||||
*,
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
interactive: Optional[bool] = None,
|
||||
visible: bool = True,
|
||||
@ -789,6 +812,7 @@ class Checkbox(Changeable, IOComponent, SimpleSerializable, FormComponent):
|
||||
Parameters:
|
||||
value: if True, checked by default. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
interactive: if True, this checkbox can be checked; if False, checking will be disabled. If not provided, this is inferred based on whether the component is used as an input or output.
|
||||
visible: If False, component will be hidden.
|
||||
@ -799,6 +823,7 @@ class Checkbox(Changeable, IOComponent, SimpleSerializable, FormComponent):
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
interactive=interactive,
|
||||
visible=visible,
|
||||
@ -871,6 +896,7 @@ class CheckboxGroup(Changeable, IOComponent, SimpleSerializable, FormComponent):
|
||||
value: List[str] | Callable = None,
|
||||
type: str = "value",
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
interactive: Optional[bool] = None,
|
||||
visible: bool = True,
|
||||
@ -883,6 +909,7 @@ class CheckboxGroup(Changeable, IOComponent, SimpleSerializable, FormComponent):
|
||||
value: default selected list of options. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
type: Type of value to be returned by component. "value" returns the list of strings of the choices selected, "index" returns the list of indicies of the choices selected.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
interactive: if True, choices in this checkbox group will be checkable; if False, checking will be disabled. If not provided, this is inferred based on whether the component is used as an input or output.
|
||||
visible: If False, component will be hidden.
|
||||
@ -901,6 +928,7 @@ class CheckboxGroup(Changeable, IOComponent, SimpleSerializable, FormComponent):
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
interactive=interactive,
|
||||
visible=visible,
|
||||
@ -1035,6 +1063,7 @@ class Radio(Changeable, IOComponent, SimpleSerializable, FormComponent):
|
||||
value: Optional[str | Callable] = None,
|
||||
type: str = "value",
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
interactive: Optional[bool] = None,
|
||||
visible: bool = True,
|
||||
@ -1047,6 +1076,7 @@ class Radio(Changeable, IOComponent, SimpleSerializable, FormComponent):
|
||||
value: the button selected by default. If None, no button is selected by default. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
type: Type of value to be returned by component. "value" returns the string of the choice selected, "index" returns the index of the choice selected.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
interactive: if True, choices in this radio group will be selectable; if False, selection will be disabled. If not provided, this is inferred based on whether the component is used as an input or output.
|
||||
visible: If False, component will be hidden.
|
||||
@ -1064,6 +1094,7 @@ class Radio(Changeable, IOComponent, SimpleSerializable, FormComponent):
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
interactive=interactive,
|
||||
visible=visible,
|
||||
@ -1179,6 +1210,7 @@ class Dropdown(Radio):
|
||||
value: Optional[str | Callable] = None,
|
||||
type: str = "value",
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
interactive: Optional[bool] = None,
|
||||
visible: bool = True,
|
||||
@ -1191,6 +1223,7 @@ class Dropdown(Radio):
|
||||
value: default value selected in dropdown. If None, no value is selected by default. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
type: Type of value to be returned by component. "value" returns the string of the choice selected, "index" returns the index of the choice selected.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
interactive: if True, choices in this dropdown will be selectable; if False, selection will be disabled. If not provided, this is inferred based on whether the component is used as an input or output.
|
||||
visible: If False, component will be hidden.
|
||||
@ -1202,6 +1235,7 @@ class Dropdown(Radio):
|
||||
choices=choices,
|
||||
type=type,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
interactive=interactive,
|
||||
visible=visible,
|
||||
@ -1248,6 +1282,7 @@ class Image(
|
||||
tool: str = None,
|
||||
type: str = "numpy",
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
interactive: Optional[bool] = None,
|
||||
visible: bool = True,
|
||||
@ -1266,6 +1301,7 @@ class Image(
|
||||
tool: Tools used for editing. "editor" allows a full screen editor (and is the default if source is "upload" or "webcam"), "select" provides a cropping and zoom tool, "sketch" allows you to create a binary sketch (and is the default if source="canvas"), and "color-sketch" allows you to created a sketch in different colors. "color-sketch" can be used with source="upload" or "webcam" to allow sketching on an image. "sketch" can also be used with "upload" or "webcam" to create a mask over an image and in that case both the image and mask are passed into the function as a dictionary with keys "image" and "mask" respectively.
|
||||
type: The format the image is converted to before being passed into the prediction function. "numpy" converts the image to a numpy array with shape (width, height, 3) and values from 0 to 255, "pil" converts the image to a PIL image object, "filepath" passes a str path to a temporary file containing the image.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
interactive: if True, will allow users to upload and edit an image; if False, can only be used to display images. If not provided, this is inferred based on whether the component is used as an input or output.
|
||||
visible: If False, component will be hidden.
|
||||
@ -1302,6 +1338,7 @@ class Image(
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
interactive=interactive,
|
||||
visible=visible,
|
||||
@ -1600,6 +1637,7 @@ class Video(
|
||||
format: Optional[str] = None,
|
||||
source: str = "upload",
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
interactive: Optional[bool] = None,
|
||||
visible: bool = True,
|
||||
@ -1613,6 +1651,7 @@ class Video(
|
||||
format: Format of video format to be returned by component, such as 'avi' or 'mp4'. Use 'mp4' to ensure browser playability. If set to None, video will keep uploaded format.
|
||||
source: Source of video. "upload" creates a box where user can drop an video file, "webcam" allows user to record a video from their webcam.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
interactive: if True, will allow users to upload a video; if False, can only be used to display videos. If not provided, this is inferred based on whether the component is used as an input or output.
|
||||
visible: If False, component will be hidden.
|
||||
@ -1631,6 +1670,7 @@ class Video(
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
interactive=interactive,
|
||||
visible=visible,
|
||||
@ -1804,6 +1844,7 @@ class Audio(
|
||||
source: str = "upload",
|
||||
type: str = "numpy",
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
interactive: Optional[bool] = None,
|
||||
visible: bool = True,
|
||||
@ -1817,6 +1858,7 @@ class Audio(
|
||||
source: Source of audio. "upload" creates a box where user can drop an audio file, "microphone" creates a microphone input.
|
||||
type: The format the audio file is converted to before being passed into the prediction function. "numpy" converts the audio to a tuple consisting of: (int sample rate, numpy.array for the data), "filepath" passes a str path to a temporary file containing the audio.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
interactive: if True, will allow users to upload and edit a audio file; if False, can only be used to play audio. If not provided, this is inferred based on whether the component is used as an input or output.
|
||||
visible: If False, component will be hidden.
|
||||
@ -1846,6 +1888,7 @@ class Audio(
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
interactive=interactive,
|
||||
visible=visible,
|
||||
@ -2108,6 +2151,7 @@ class File(
|
||||
file_types: List[str] = None,
|
||||
type: str = "file",
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
interactive: Optional[bool] = None,
|
||||
visible: bool = True,
|
||||
@ -2121,6 +2165,7 @@ class File(
|
||||
file_types: List of type of files to be uploaded. "file" allows any file to be uploaded, "image" allows only image files to be uploaded, "audio" allows only audio files to be uploaded, "video" allows only video files to be uploaded, "text" allows only text files to be uploaded.
|
||||
type: Type of value to be returned by component. "file" returns a temporary file object whose path can be retrieved by file_obj.name and original filename can be retrieved with file_obj.orig_name, "binary" returns an bytes object.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
interactive: if True, will allow users to upload a file; if False, can only be used to display files. If not provided, this is inferred based on whether the component is used as an input or output.
|
||||
visible: If False, component will be hidden.
|
||||
@ -2147,6 +2192,7 @@ class File(
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
interactive=interactive,
|
||||
visible=visible,
|
||||
@ -2323,6 +2369,7 @@ class Dataframe(Changeable, IOComponent, JSONSerializable):
|
||||
max_cols: Optional[int] = None,
|
||||
overflow_row_behaviour: str = "paginate",
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
interactive: Optional[bool] = None,
|
||||
visible: bool = True,
|
||||
@ -2343,6 +2390,7 @@ class Dataframe(Changeable, IOComponent, JSONSerializable):
|
||||
max_cols: Maximum number of columns to display at once. Set to None for infinite.
|
||||
overflow_row_behaviour: If set to "paginate", will create pages for overflow rows. If set to "show_ends", will show initial and final rows and truncate middle rows.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
interactive: if True, will allow users to edit the dataframe; if False, can only be used to display data. If not provided, this is inferred based on whether the component is used as an input or output.
|
||||
visible: If False, component will be hidden.
|
||||
@ -2391,6 +2439,7 @@ class Dataframe(Changeable, IOComponent, JSONSerializable):
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
interactive=interactive,
|
||||
visible=visible,
|
||||
@ -2586,6 +2635,7 @@ class Timeseries(Changeable, IOComponent, JSONSerializable):
|
||||
y: str | List[str] = None,
|
||||
colors: List[str] = None,
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
interactive: Optional[bool] = None,
|
||||
visible: bool = True,
|
||||
@ -2598,6 +2648,7 @@ class Timeseries(Changeable, IOComponent, JSONSerializable):
|
||||
x: Column name of x (time) series. None if csv has no headers, in which case first column is x series.
|
||||
y: Column name of y series, or list of column names if multiple series. None if csv has no headers, in which case every column after first is a y series.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
colors: an ordered list of colors to use for each line plot
|
||||
show_label: if True, will display label.
|
||||
interactive: if True, will allow users to upload a timeseries csv; if False, can only be used to display timeseries data. If not provided, this is inferred based on whether the component is used as an input or output.
|
||||
@ -2612,6 +2663,7 @@ class Timeseries(Changeable, IOComponent, JSONSerializable):
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
interactive=interactive,
|
||||
visible=visible,
|
||||
@ -2958,6 +3010,7 @@ class ColorPicker(Changeable, Submittable, IOComponent, SimpleSerializable):
|
||||
value: str | Callable = None,
|
||||
*,
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
interactive: Optional[bool] = None,
|
||||
visible: bool = True,
|
||||
@ -2968,6 +3021,7 @@ class ColorPicker(Changeable, Submittable, IOComponent, SimpleSerializable):
|
||||
Parameters:
|
||||
value: default text to provide in color picker. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
interactive: if True, will be rendered as an editable color picker; if False, editing will be disabled. If not provided, this is inferred based on whether the component is used as an input or output.
|
||||
visible: If False, component will be hidden.
|
||||
@ -2978,6 +3032,7 @@ class ColorPicker(Changeable, Submittable, IOComponent, SimpleSerializable):
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
interactive=interactive,
|
||||
visible=visible,
|
||||
@ -3063,6 +3118,7 @@ class Label(Changeable, IOComponent, JSONSerializable):
|
||||
*,
|
||||
num_top_classes: Optional[int] = None,
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
visible: bool = True,
|
||||
elem_id: Optional[str] = None,
|
||||
@ -3074,6 +3130,7 @@ class Label(Changeable, IOComponent, JSONSerializable):
|
||||
value: Default value to show in the component. If a str or number is provided, simply displays the string or number. If a {Dict[str, float]} of classes and confidences is provided, displays the top class on top and the `num_top_classes` below, along with their confidence bars. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
num_top_classes: number of most confident classes to show.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
visible: If False, component will be hidden.
|
||||
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
|
||||
@ -3084,6 +3141,7 @@ class Label(Changeable, IOComponent, JSONSerializable):
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
visible=visible,
|
||||
elem_id=elem_id,
|
||||
@ -3192,6 +3250,7 @@ class HighlightedText(Changeable, IOComponent, JSONSerializable):
|
||||
combine_adjacent: bool = False,
|
||||
adjacent_separator: str = "",
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
visible: bool = True,
|
||||
elem_id: Optional[str] = None,
|
||||
@ -3204,6 +3263,7 @@ class HighlightedText(Changeable, IOComponent, JSONSerializable):
|
||||
combine_adjacent: If True, will merge the labels of adjacent tokens belonging to the same category.
|
||||
adjacent_separator: Specifies the separator to be used between tokens if combine_adjacent is True.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
visible: If False, component will be hidden.
|
||||
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
|
||||
@ -3219,6 +3279,7 @@ class HighlightedText(Changeable, IOComponent, JSONSerializable):
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
visible=visible,
|
||||
elem_id=elem_id,
|
||||
@ -3346,6 +3407,7 @@ class JSON(Changeable, IOComponent, JSONSerializable):
|
||||
value: Optional[str | Callable] = None,
|
||||
*,
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
visible: bool = True,
|
||||
elem_id: Optional[str] = None,
|
||||
@ -3355,6 +3417,7 @@ class JSON(Changeable, IOComponent, JSONSerializable):
|
||||
Parameters:
|
||||
value: Default value. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
visible: If False, component will be hidden.
|
||||
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
|
||||
@ -3362,6 +3425,7 @@ class JSON(Changeable, IOComponent, JSONSerializable):
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
visible=visible,
|
||||
elem_id=elem_id,
|
||||
@ -3431,6 +3495,7 @@ class HTML(Changeable, IOComponent, SimpleSerializable):
|
||||
value: str | Callable = "",
|
||||
*,
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
visible: bool = True,
|
||||
elem_id: Optional[str] = None,
|
||||
@ -3440,6 +3505,7 @@ class HTML(Changeable, IOComponent, SimpleSerializable):
|
||||
Parameters:
|
||||
value: Default value. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
visible: If False, component will be hidden.
|
||||
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
|
||||
@ -3447,6 +3513,7 @@ class HTML(Changeable, IOComponent, SimpleSerializable):
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
visible=visible,
|
||||
elem_id=elem_id,
|
||||
@ -3495,6 +3562,7 @@ class Gallery(IOComponent, TempFileManager):
|
||||
value: Optional[List[np.ndarray | PIL.Image | str] | Callable] = None,
|
||||
*,
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
visible: bool = True,
|
||||
elem_id: Optional[str] = None,
|
||||
@ -3504,6 +3572,7 @@ class Gallery(IOComponent, TempFileManager):
|
||||
Parameters:
|
||||
value: List of images to display in the gallery by default. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
visible: If False, component will be hidden.
|
||||
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
|
||||
@ -3511,6 +3580,7 @@ class Gallery(IOComponent, TempFileManager):
|
||||
TempFileManager.__init__(self)
|
||||
super().__init__(
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
visible=visible,
|
||||
elem_id=elem_id,
|
||||
@ -3669,6 +3739,7 @@ class Chatbot(Changeable, IOComponent, JSONSerializable):
|
||||
color_map: Dict[str, str] = None, # Parameter moved to Chatbot.style()
|
||||
*,
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
visible: bool = True,
|
||||
elem_id: Optional[str] = None,
|
||||
@ -3678,6 +3749,7 @@ class Chatbot(Changeable, IOComponent, JSONSerializable):
|
||||
Parameters:
|
||||
value: Default value to show in chatbot. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
visible: If False, component will be hidden.
|
||||
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
|
||||
@ -3692,6 +3764,7 @@ class Chatbot(Changeable, IOComponent, JSONSerializable):
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
visible=visible,
|
||||
elem_id=elem_id,
|
||||
@ -3773,6 +3846,7 @@ class Model3D(
|
||||
*,
|
||||
clear_color: List[float] = None,
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
visible: bool = True,
|
||||
elem_id: Optional[str] = None,
|
||||
@ -3783,6 +3857,7 @@ class Model3D(
|
||||
value: path to (.obj, glb, or .gltf) file to show in model3D viewer. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
clear_color: background color of scene
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
visible: If False, component will be hidden.
|
||||
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
|
||||
@ -3792,6 +3867,7 @@ class Model3D(
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
visible=visible,
|
||||
elem_id=elem_id,
|
||||
@ -3894,6 +3970,7 @@ class Plot(Changeable, Clearable, IOComponent, JSONSerializable):
|
||||
value: Optional[Callable] = None,
|
||||
*,
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
visible: bool = True,
|
||||
elem_id: Optional[str] = None,
|
||||
@ -3901,8 +3978,9 @@ class Plot(Changeable, Clearable, IOComponent, JSONSerializable):
|
||||
):
|
||||
"""
|
||||
Parameters:
|
||||
value: Optionally, supply a default plot object to display, must be a matplotlib, plotly, altair, or bokeh figure. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
value: Optionally, supply a default plot object to display, must be a matplotlib, plotly, altair, or bokeh figure, or a callable. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
label: component name in interface.
|
||||
every: If `value` is a callable, run the function 'every' number of seconds while the client connection is open. Has no effect otherwise. Queue must be enabled. The event can be accessed (e.g. to cancel it) via this component's .load_event attribute.
|
||||
show_label: if True, will display label.
|
||||
visible: If False, component will be hidden.
|
||||
elem_id: An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
|
||||
@ -3910,6 +3988,7 @@ class Plot(Changeable, Clearable, IOComponent, JSONSerializable):
|
||||
IOComponent.__init__(
|
||||
self,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
visible=visible,
|
||||
elem_id=elem_id,
|
||||
@ -3999,13 +4078,14 @@ class ScatterPlot(Plot):
|
||||
caption: Optional[str] = None,
|
||||
interactive: Optional[bool] = True,
|
||||
label: Optional[str] = None,
|
||||
every: float | None = None,
|
||||
show_label: bool = True,
|
||||
visible: bool = True,
|
||||
elem_id: Optional[str] = None,
|
||||
):
|
||||
"""
|
||||
Parameters:
|
||||
value: The pandas dataframe containing the data to display in a scatter plot.
|
||||
value: The pandas dataframe containing the data to display in a scatter plot, or a callable. If callable, the function will be called whenever the app loads to set the initial value of the component.
|
||||
x: Column corresponding to the x axis.
|
||||
y: Column corresponding to the y axis.
|
||||
color: The column to determine the point color. If the column contains numeric data, gradio will interpolate the column data so that small values correspond to light colors and large values correspond to dark values.
|
||||
@ -4043,12 +4123,10 @@ class ScatterPlot(Plot):
|
||||
self.interactive_chart = interactive
|
||||
self.width = width
|
||||
self.height = height
|
||||
# self.value = None
|
||||
# if value is not None:
|
||||
# self.value = self.postprocess(value)
|
||||
super().__init__(
|
||||
value=value,
|
||||
label=label,
|
||||
every=every,
|
||||
show_label=show_label,
|
||||
visible=visible,
|
||||
elem_id=elem_id,
|
||||
|
@ -284,9 +284,9 @@ class TestComponentsInBlocks:
|
||||
for component in demo.blocks.values():
|
||||
if isinstance(component, gr.components.IOComponent):
|
||||
if "Non-random" in component.label:
|
||||
assert not component.attach_load_event
|
||||
assert not component.load_event
|
||||
else:
|
||||
assert component.attach_load_event
|
||||
assert component.load_event
|
||||
dependencies_on_load = [
|
||||
dep["trigger"] == "load" for dep in demo.config["dependencies"]
|
||||
]
|
||||
@ -295,12 +295,10 @@ class TestComponentsInBlocks:
|
||||
assert not any([dep["queue"] for dep in demo.config["dependencies"]])
|
||||
|
||||
def test_io_components_attach_load_events_when_value_is_fn(self, io_components):
|
||||
io_components = [
|
||||
comp for comp in io_components if comp not in [gr.State, gr.ScatterPlot]
|
||||
]
|
||||
io_components = [comp for comp in io_components if comp not in [gr.State]]
|
||||
interface = gr.Interface(
|
||||
lambda *args: None,
|
||||
inputs=[comp(value=lambda: None) for comp in io_components],
|
||||
inputs=[comp(value=lambda: None, every=1) for comp in io_components],
|
||||
outputs=None,
|
||||
)
|
||||
|
||||
@ -308,6 +306,14 @@ class TestComponentsInBlocks:
|
||||
dep for dep in interface.config["dependencies"] if dep["trigger"] == "load"
|
||||
]
|
||||
assert len(dependencies_on_load) == len(io_components)
|
||||
assert all([dep["every"] == 1 for dep in dependencies_on_load])
|
||||
|
||||
def test_get_load_events(self, io_components):
|
||||
components = []
|
||||
with gr.Blocks() as demo:
|
||||
for component in io_components:
|
||||
components.append(component(value=lambda: None, every=1))
|
||||
assert [comp.load_event for comp in components] == demo.dependencies
|
||||
|
||||
def test_blocks_do_not_filter_none_values_from_updates(self, io_components):
|
||||
io_components = [
|
||||
|
@ -410,9 +410,9 @@ class TestSlider:
|
||||
@patch("gradio.Slider.get_random_value", return_value=7)
|
||||
def test_slider_get_random_value_on_load(self, mock_get_random_value):
|
||||
slider = gr.Slider(minimum=-5, maximum=10, randomize=True)
|
||||
assert slider.attach_load_event
|
||||
assert slider.value == 7
|
||||
assert slider.load_fn() == 7
|
||||
assert slider.load_event_to_attach[0]() == 7
|
||||
assert slider.load_event_to_attach[1] is None
|
||||
|
||||
@patch("random.randint", return_value=3)
|
||||
def test_slider_rounds_when_using_default_randomizer(self, mock_randint):
|
||||
|
Loading…
Reference in New Issue
Block a user