Skip to content

Commit 1beb54a

Browse files
authored
Add documentation about how to create and use the Gradio FastAPI app (#2263)
* added a demo * added in docs
1 parent 101bc7e commit 1beb54a

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

demo/custom_path/run.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from fastapi import FastAPI
2+
import gradio as gr
3+
4+
CUSTOM_PATH = "/gradio"
5+
6+
app = FastAPI()
7+
8+
@app.get("/")
9+
def read_main():
10+
return {"message": "This is your main app"}
11+
12+
io = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox")
13+
gradio_app = gr.routes.App.create_app(io)
14+
15+
app.mount(CUSTOM_PATH, gradio_app)
16+
17+
# Run this from the terminal as you would normally start a FastAPI app: `uvicorn run:app` and navigate to http://localhost:8000/gradio in your browser.

guides/1)getting_started/3)sharing_your_app

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,28 @@ This will document the endpoint `/api/addition/` to the automatically generated
9090

9191
## Authentication
9292

93-
You may wish to put an authentication page in front of your app to limit who can open your app. With the `auth=` keyword argument in the `launch()` method, you can pass a list of acceptable username/password tuples; or, for more complex authentication handling, you can even pass a function that takes a username and password as arguments, and returns True to allow authentication, False otherwise. Here's an example that provides password-based authentication for a single user named "admin":
93+
You may wish to put an authentication page in front of your app to limit who can open your app. With the `auth=` keyword argument in the `launch()` method, you can provide a tuple with a username and password, or a list of acceptable username/password tuples; Here's an example that provides password-based authentication for a single user named "admin":
9494

9595
```python
9696
demo.launch(auth=("admin", "pass1234"))
9797
```
9898

99-
Here's one that accepts any login where the username and password are the same.
99+
For more complex authentication handling, you can even pass a function that takes a username and password as arguments, and returns True to allow authentication, False otherwise. This can be used for, among other things, making requests to 3rd-party authentication services.
100+
101+
Here's an example of a function that accepts any login where the username and password are the same:
100102

101103
```python
102104
def same_auth(username, password):
103105
return username == password
104106
demo.launch(auth=same_auth)
105107
```
106108

109+
## Mounting Within Another FastAPI App
110+
111+
In some cases, you might have an existing FastAPI app, and you'd like to add a path for a Gradio demo. You can do this by easily using the `gradio.routes.App.create_app()` function, which creates a FastAPI app (but does not launch it), and then adding it to your existing FastAPI app with `FastAPI.mount()`.
112+
113+
Here's a complete example:
114+
115+
$code_custom_path
107116

117+
Note that this approach also allows you run your Gradio apps on custom paths (`http://localhost:8000/gradio` in the example above).

0 commit comments

Comments
 (0)