Gradio Agents & MCP 黑客松

获胜者
Gradio logo
  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 缓存中。这也意味着该目录中的所有文件都将可以通过网络访问。