Custom Components GalleryNEW

Explore

New to Gradio? Start here: Getting Started

See the Release History

mount_gradio_app

gradio.mount_gradio_app(app, blocks, path, ยทยทยท)

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_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 app (they will see a 401 Unauthorized response). To be used with external authentication systems like OAuth.