- Routes
- mount_gradio_app
New to Gradio? Start here: Getting Started
See the Release History
mount_gradio_app
gradio.mount_gradio_app(···)Description
Mount a gradio.Blocks to an existing FastAPI application.
Example Usage
from fastapi import FastAPI
import gradio as gr
app = FastAPI()
@app.get("/")
def read_main():
return {"message": "This is your main app"}
io = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox")
app = gr.mount_gradio_app(app, io, path="/gradio")Then run uvicorn run:app from the terminal and navigate to http://localhost:8000/gradio.
Initialization
app_kwargs: dict[str, Any] | None
app_kwargs: dict[str, Any] | None= NoneAdditional keyword arguments to pass to the underlying FastAPI app as a dictionary of parameter keys and argument values. For example, `{"docs_url": "/docs"}`
auth: Callable | tuple[str, str] | list[tuple[str, str]] | None
auth: Callable | tuple[str, str] | list[tuple[str, str]] | None= NoneIf provided, username and password (or list of username-password tuples) required to access the gradio app. Can also provide function that takes username and password and returns True if valid login.
auth_message: str | None
auth_message: str | None= NoneIf provided, HTML message provided on login page for this gradio app.
auth_dependency: Callable[[fastapi.Request], str | None] | None
auth_dependency: Callable[[fastapi.Request], str | None] | None= NoneA function that takes a FastAPI request and returns a string user ID or None. If the function returns None for a specific request, that user is not authorized to access the gradio app (they will see a 401 Unauthorized response). To be used with external authentication systems like OAuth. Cannot be used with `auth`.
root_path: str | None
root_path: str | None= NoneThe subpath corresponding to the public deployment of this FastAPI application. For example, if the application is served at "https://example.com/myapp", the `root_path` should be set to "/myapp". A full URL beginning with http:// or https:// can be provided, which will be used in its entirety. Normally, this does not need to provided (even if you are using a custom `path`). However, if you are serving the FastAPI app behind a proxy, the proxy may not provide the full path to the Gradio app in the request headers. In which case, you can provide the root path here.
allowed_paths: list[str] | None
allowed_paths: list[str] | None= NoneList of complete filepaths or parent directories that this gradio app is allowed to serve. Must be absolute paths. Warning: if you provide directories, any files in these directories or their subdirectories are accessible to all users of your app.
blocked_paths: list[str] | None
blocked_paths: list[str] | None= NoneList of complete filepaths or parent directories that this gradio app is not allowed to serve (i.e. users of your app are not allowed to access). Must be absolute paths. Warning: takes precedence over `allowed_paths` and all other directories exposed by Gradio by default.
favicon_path: str | None
favicon_path: str | None= NoneIf a path to a file (.png, .gif, or .ico) is provided, it will be used as the favicon for this gradio app's page.
show_error: bool
show_error: bool= TrueIf True, any errors in the gradio app will be displayed in an alert modal and printed in the browser console log. Otherwise, errors will only be visible in the terminal session running the Gradio app.
max_file_size: str | int | None
max_file_size: str | int | None= NoneThe maximum file size in bytes that can be uploaded. Can be a string of the form "<value><unit>", where value is any positive integer and unit is one of "b", "kb", "mb", "gb", "tb". If None, no limit is set.
ssr_mode: bool | None
ssr_mode: bool | None= NoneIf True, the Gradio app will be rendered using server-side rendering mode, which is typically more performant and provides better SEO, but this requires Node 20+ to be installed on the system. If False, the app will be rendered using client-side rendering mode. If None, will use GRADIO_SSR_MODE environment variable or default to False.
node_server_name: str | None
node_server_name: str | None= NoneThe name of the Node server to use for SSR. If None, will use GRADIO_NODE_SERVER_NAME environment variable or search for a node binary in the system.
node_port: int | None
node_port: int | None= NoneThe port on which the Node server should run. If None, will use GRADIO_NODE_SERVER_PORT environment variable or find a free port.
mcp_server: bool | None
mcp_server: bool | None= NoneIf True, the MCP server will be launched on the gradio app. If None, will use GRADIO_MCP_SERVER environment variable or default to False.
theme: Theme | str | None
theme: Theme | str | None= NoneA Theme object or a string representing a theme. If a string, will look for a built-in theme with that name (e.g. "soft" or "default"), or will attempt to load a theme from the Hugging Face Hub (e.g. "gradio/monochrome"). If None, will use the Default theme.
css: str | None
css: str | None= NoneCustom css as a code string. This css will be included in the demo webpage.
css_paths: str | Path | list[str | Path] | None
css_paths: str | Path | list[str | Path] | None= NoneCustom css as a pathlib.Path to a css file or a list of such paths. This css files will be read, concatenated, and included in the demo webpage. If the `css` parameter is also set, the css from `css` will be included first.
js: str | Literal[True] | None
js: str | Literal[True] | None= NoneCustom js as a code string. The custom js should be in the form of a single js function. This function will automatically be executed when the page loads. For more flexibility, use the head parameter to insert js inside <script> tags.
head: str | None
head: str | None= NoneCustom html code to insert into the head of the demo webpage. This can be used to add custom meta tags, multiple scripts, stylesheets, etc. to the page.
head_paths: str | Path | list[str | Path] | None
head_paths: str | Path | list[str | Path] | None= NoneCustom html code as a pathlib.Path to a html file or a list of such paths. This html files will be read, concatenated, and included in the head of the demo webpage. If the `head` parameter is also set, the html from `head` will be included first.