1. 助手
  2. set_static_paths

刚开始使用 Gradio? 从这里开始: 入门指南

查看 发布历史

set_static_paths

gradio.set_static_paths(···)

描述

设置由gradio应用提供服务的静态路径。
静态文件直接从文件系统提供服务,而不是被复制。当将这些文件发送给用户时,会设置 Content-Disposition HTTP 标头为 "inline"。这表示如果可能,文件应该直接显示在浏览器窗口中。当你想要提供你知道在 gradio 应用生命周期内不会被修改的文件(例如 gr.Examples 中使用的文件)时,此功能非常有用。通过设置静态路径,你的应用启动会更快,并且会占用更少的磁盘空间。调用此函数将为在同一解释器会话中定义的所有 gradio 应用设置静态路径,直到再次调用或会话结束。

示例用法

import gradio as gr

# Paths can be a list of strings or pathlib.Path objects
# corresponding to filenames or directories.
gr.set_static_paths(paths=["test/test_files/"])

# The example files and the default value of the input
# will not be copied to the gradio cache and will be served directly.
demo = gr.Interface(
    lambda s: s.rotate(45),
    gr.Image(value="test/test_files/cheetah1.jpg", type="pil"),
    gr.Image(),
    examples=["test/test_files/bus.png"],
)

demo.launch()

初始化

参数
🔗
paths: str | Path | list[str | Path]

要由 gradio 应用提供服务的文件路径或文件路径列表或目录名。如果是目录名,该目录下所有文件都将被视为静态文件,并且不会移动到 gradio 缓存中。这也意味着该目录中的所有文件都将可以通过网络访问。

gradio