Introducing Gradio 5.0

Read More
  1. Routes
  2. mount_gradio_app

New to Gradio? Start here: Getting Started

See the Release History

To install Gradio from main, run the following command:

pip install https://gradio-builds.s3.amazonaws.com/ac9bf5ec9208b92579f36ee94a247ae3e676c02f/gradio-5.6.0-py3-none-any.whl

*Note: Setting share=True in launch() will not work.

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

Parameters
app: fastapi.FastAPI

The parent FastAPI application.

blocks: gradio.Blocks

The blocks object we want to mount to the parent app.

path: str

The path at which the gradio application will be mounted, e.g. "/gradio".

server_name: str
default = "0.0.0.0"

The server name on which the Gradio app will be run.

server_port: int
default = 7860

The port on which the Gradio app will be run.

app_kwargs: dict[str, Any] | None
default = None

Additional 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
default = None

If 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
default = None

If provided, HTML message provided on login page for this gradio app.

auth_dependency: Callable[[fastapi.Request], str | None] | None
default = None

A 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
default = None

The 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
default = None

List 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
default = None

List 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
default = None

If 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
default = True

If 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
default = None

The 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
default = None

If 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
default = None

The 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
default = None

The port on which the Node server should run. If None, will use GRADIO_NODE_SERVER_PORT environment variable or find a free port.