- 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.