Gradio 迈向每月 100 万用户的旅程!
阅读更多Gradio 迈向每月 100 万用户的旅程!
阅读更多gradio.CheckboxGroup(···)
list[str | int | float]
或它们的索引作为 list[int]
传递到函数中,具体取决于 type
。def predict(
value: list[str | int | float] | list[int | None]
)
...
list[str | int | float]
的值或单个 str | int | float
值,具有这些值的复选框将被选中。def predict(···) -> list[str | int | float] | str | int | float | None
...
return value
choices: list[str | int | float | tuple[str, str | int | float]] | None
= None
从中选择的字符串或数字选项列表。选项也可以是 (name, value) 形式的元组,其中 name 是复选框按钮的显示名称,value 是要传递给函数或由函数返回的值。
value: list[str | float | int] | str | float | int | Callable | None
= None
默认选定的选项列表。如果选择了单个选项,则可以作为字符串或数字类型传入。如果提供了函数,则每次应用程序加载时都会调用该函数以设置此组件的初始值。
type: Literal['value', 'index']
= "value"
组件返回的值的类型。“value” 返回所选选项的字符串列表,“index” 返回所选选项的索引列表。
label: str | None
= None
此组件的标签,如果 `show_label` 为 `True`,则显示在组件上方,如果此组件有示例表,则也用作标题。如果为 None 且在 `gr.Interface` 中使用,则标签将是此组件对应的参数名称。
every: Timer | float | None
= None
如果 `value` 是函数,则持续调用 `value` 以重新计算(否则无效)。可以提供一个 Timer,其滴答声重置 `value`,或者提供一个浮点数,为重置 Timer 提供常规间隔。
inputs: Component | list[Component] | set[Component] | None
= None
如果 `value` 是函数,则用作输入以计算 `value` 的组件(否则无效)。每当输入更改时,`value` 都会重新计算。
scale: int | None
= None
与 Row 中相邻组件相比的相对宽度。例如,如果组件 A 的 scale=2,组件 B 的 scale=1,则 A 将是 B 的两倍宽。应为整数。
min_width: int
= 160
最小像素宽度,如果屏幕空间不足以满足此值,则会换行。如果某个 scale 值导致此组件比 min_width 窄,则将首先遵循 min_width 参数。
类 | Interface 字符串快捷方式 | 初始化 |
---|---|---|
| "checkboxgroup" | 使用默认值 |
import gradio as gr
def sentence_builder(quantity, animal, countries, place, activity_list, morning):
return f"""The {quantity} {animal}s from {" and ".join(countries)} went to the {place} where they {" and ".join(activity_list)} until the {"morning" if morning else "night"}"""
demo = gr.Interface(
sentence_builder,
[
gr.Slider(2, 20, value=4, label="Count", info="Choose between 2 and 20"),
gr.Dropdown(
["cat", "dog", "bird"], label="Animal", info="Will add more animals later!"
),
gr.CheckboxGroup(["USA", "Japan", "Pakistan"], label="Countries", info="Where are they from?"),
gr.Radio(["park", "zoo", "road"], label="Location", info="Where did they go?"),
gr.Dropdown(
["ran", "swam", "ate", "slept"], value=["swam", "slept"], multiselect=True, label="Activity", info="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed auctor, nisl eget ultricies aliquam, nunc nisl aliquet nunc, eget aliquam nisl nunc vel nisl."
),
gr.Checkbox(label="Morning", info="Did they do it in the morning?"),
],
"text",
examples=[
[2, "cat", ["Japan", "Pakistan"], "park", ["ate", "swam"], True],
[4, "dog", ["Japan"], "zoo", ["ate", "swam"], False],
[10, "bird", ["USA", "Pakistan"], "road", ["ran"], False],
[8, "cat", ["Pakistan"], "zoo", ["ate"], True],
]
)
if __name__ == "__main__":
demo.launch()
事件监听器允许您响应用户与在 Gradio Blocks 应用程序中定义的 UI 组件的交互。当用户与元素交互时,例如更改滑块值或上传图像,将调用一个函数。
该 CheckboxGroup 组件支持以下事件监听器。每个事件监听器都采用相同的参数,这些参数在 事件参数 下表中列出。
监听器 | 描述 |
---|---|
| 当 CheckboxGroup 的值因用户输入(例如,用户在文本框中键入)或函数更新(例如,图像从事件触发器的输出接收值)而更改时触发。请参阅 |
| 当用户更改 CheckboxGroup 的值时,将触发此监听器。 |
| 当用户选择或取消选择 CheckboxGroup 时的事件监听器。使用事件数据 gradio.SelectData 来携带 |
fn: Callable | None | Literal['decorator']
= "decorator"
当触发此事件时要调用的函数。通常是机器学习模型的预测函数。函数的每个参数都对应一个输入组件,函数应返回单个值或值元组,元组中的每个元素都对应一个输出组件。
inputs: Component | BlockContext | list[Component | BlockContext] | Set[Component | BlockContext] | None
= None
用作输入的 gradio.components 列表。如果函数不接受任何输入,则应为空列表。
outputs: Component | BlockContext | list[Component | BlockContext] | Set[Component | BlockContext] | None
= None
用作输出的 gradio.components 列表。如果函数不返回任何输出,则应为空列表。
api_name: str | None | Literal[False]
= None
定义端点在 API 文档中的显示方式。可以是字符串、None 或 False。如果设置为字符串,则端点将在 API 文档中以给定名称公开。如果为 None(默认),则函数名称将用作 API 端点。如果为 False,则端点将不会在 API 文档中公开,下游应用程序(包括那些 `gr.load` 此应用程序的应用程序)将无法使用此事件。
show_progress: Literal['full', 'minimal', 'hidden']
= "full"
在事件运行时如何显示进度动画:“full” 显示一个覆盖输出组件区域的微调器以及右上角的运行时显示,“minimal” 仅显示运行时显示,“hidden” 完全不显示进度动画
show_progress_on: Component | list[Component] | None
= None
要在其上显示进度动画的组件或组件列表。如果为 None,则将在所有输出组件上显示进度动画。
queue: bool
= True
如果为 True,则会将请求放在队列中(如果已启用队列)。如果为 False,即使已启用队列,也不会将此事件放在队列中。如果为 None,将使用 gradio 应用程序的队列设置。
batch: bool
= False
如果为 True,则该函数应处理一批输入,这意味着它应接受每个参数的输入值列表。列表的长度应相等(且最大长度为 `max_batch_size`)。然后 *需要* 该函数返回一个列表元组(即使只有一个输出组件),元组中的每个列表都对应一个输出组件。
preprocess: bool
= True
如果为 False,则在运行 'fn' 之前不会运行组件数据的预处理(例如,如果使用 `Image` 组件调用此方法,则将其保留为 base64 字符串)。
cancels: dict[str, Any] | list[dict[str, Any]] | None
= None
触发此监听器时要取消的其他事件的列表。例如,设置 cancels=[click_event] 将取消 click_event,其中 click_event 是另一个组件的 .click 方法的返回值。尚未运行的函数(或正在迭代的生成器)将被取消,但当前正在运行的函数将被允许完成。
trigger_mode: Literal['once', 'multiple', 'always_last'] | None
= None
如果为“once”(除 `.change()` 之外的所有事件的默认值),则在事件挂起时将不允许任何提交。如果设置为“multiple”,则在挂起时允许无限次提交,而“always_last”(`.change()` 和 `.key_up()` 事件的默认值)将在挂起事件完成后允许第二次提交。
js: str | Literal[True] | None
= None
在运行 'fn' 之前运行的可选前端 js 方法。js 方法的输入参数是 'inputs' 和 'outputs' 的值,返回值应为输出组件的值列表。
concurrency_limit: int | None | Literal['default']
= "default"
如果设置,则这是可以同时运行的此事件的最大数量。可以设置为 None 以表示没有 concurrency_limit(可以同时运行任意数量的此事件)。设置为“default”以使用默认并发限制(由 `Blocks.queue()` 中的 `default_concurrency_limit` 参数定义,默认值为 1)。
concurrency_id: str | None
= None
如果设置,则这是并发组的 id。具有相同 concurrency_id 的事件将受到最低设置的 concurrency_limit 的限制。