Introducing Gradio ClientsJoin us on Thursday, 9am PST

Livestream

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/35bc484c1744ff435057642495be05fb3e839b6e/gradio-4.35.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

Parameter Description
app

fastapi.FastAPI

required

The parent FastAPI application.

blocks

gradio.Blocks

required

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

path

str

required

The path at which the gradio application will be mounted.

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.