Gradio 的月活跃用户达到 100 万!
阅读更多Gradio 的月活跃用户达到 100 万!
阅读更多gradio.DownloadButton(···)
str
传递到函数中。def predict(
value: str | None
)
...
str
或 pathlib.Path
文件路径def predict(···) -> str | Path | None
...
return value
value: str | Path | Callable | None
= None
要下载的 str 或 pathlib.Path 文件路径或 URL,或返回要下载的 str 或 pathlib.Path 文件路径或 URL 的 Callable。
every: Timer | float | None
= None
如果 `value` 是函数,则持续调用 `value` 以重新计算它(否则无效)。可以提供一个 Timer,其滴答声重置 `value`,或一个浮点数,提供重置 Timer 的常规间隔。
inputs: Component | list[Component] | set[Component] | None
= None
用作计算 `value` 的输入的组件(如果 `value` 是函数)(否则无效)。每当输入更改时,`value` 都会重新计算。
variant: Literal['primary', 'secondary', 'stop']
= "secondary"
'primary' 用于主要行动号召,'secondary' 用于更柔和的样式,'stop' 用于停止按钮。
scale: int | None
= None
相对于相邻组件的相对大小。例如,如果组件 A 和 B 在同一行中,并且 A 的 scale=2,而 B 的 scale=1,则 A 的宽度将是 B 的两倍。应为整数。scale 应用于行,以及 Blocks 中 fill_height=True 的顶级组件。
min_width: int | None
= None
最小像素宽度,如果屏幕空间不足以满足此值,则会换行。如果某个 scale 值导致此组件比 min_width 更窄,则首先尊重 min_width 参数。
类 | Interface 字符串快捷方式 | 初始化 |
---|---|---|
| "downloadbutton" | 使用默认值 |
from pathlib import Path
import gradio as gr
def upload_file(filepath):
name = Path(filepath).name
return [gr.UploadButton(visible=False), gr.DownloadButton(label=f"Download {name}", value=filepath, visible=True)]
def download_file():
return [gr.UploadButton(visible=True), gr.DownloadButton(visible=False)]
with gr.Blocks() as demo:
gr.Markdown("First upload a file and and then you'll be able download it (but only once!)")
with gr.Row():
u = gr.UploadButton("Upload a file", file_count="single")
d = gr.DownloadButton("Download the file", visible=False)
u.upload(upload_file, u, [u, d])
d.click(download_file, None, [u, d])
if __name__ == "__main__":
demo.launch()
事件监听器允许您响应用户与您在 Gradio Blocks 应用中定义的 UI 组件的交互。当用户与元素交互时,例如更改滑块值或上传图像,将调用一个函数。
DownloadButton 组件支持以下事件监听器。每个事件监听器都采用相同的参数,这些参数在下面的事件参数表中列出。
监听器 | 描述 |
---|---|
| 当 DownloadButton 被点击时触发。 |
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 的限制。