Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fastapi docs #7317

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions doc/how_to/integrations/FastAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,28 @@

Panel generally runs on the Bokeh server, which itself runs on [Tornado](https://tornadoweb.org/en/stable/). However, it is also often useful to embed a Panel app in an existing web application, such as a [FastAPI](https://fastapi.tiangolo.com/) web server.

Since Panel 1.5.0 it is possible to run Panel application(s) natively on a FastAPI and uvicorn based server. Therefore this how-to guide will explain how to add Panel application(s) directly to an existing FastAPI application. This functionality is new and experimental so we also provide a [how-to guide to embed a Tornado based Panel server application inside a FastAPI application](./FastAPI_Tornado).
Since Panel 1.5.0 it is possible to run Panel application(s) natively on a FastAPI based server. Therefore this how-to guide will explain how to add Panel application(s) directly to an existing FastAPI application. This functionality is new and experimental so we also provide the original [how-to guide to embed a Tornado based Panel server application inside a FastAPI application](./FastAPI_Tornado).

By the end of this guide, you'll be able to run a FastAPI application that serves a simple interactive Panel app. The Panel app will consist of a slider widget that dynamically updates a string of stars (⭐) based on the slider's value.

## Setup

Following FastAPI's [Tutorial - User Guide](https://fastapi.tiangolo.com/tutorial/) make sure you first have FastAPI installed using:
Following FastAPI's [Tutorial - User Guide](https://fastapi.tiangolo.com/tutorial/) make sure you first have [FastAPI](https://fastapi.tiangolo.com/) and [bokeh-fastapi] installed using:

::::{tab-set}

:::{tab-item} `conda`
```bash
conda install fastapi
```
:::

:::{tab-item} `pip`
```bash
pip install fastapi
pip install panel[fastapi]
philippjfr marked this conversation as resolved.
Show resolved Hide resolved
```
:::

:::{tab-item} `panel`
:::{tab-item} `conda`
```bash
pip install panel[fastapi]
conda install -c conda-forge bokeh-fastapi
```

Note: As of September 24th 2024 this is waiting on [conda-forge/staged-recipes#27575](https://github.com/conda-forge/staged-recipes/pull/27575).
:::

::::
Expand Down Expand Up @@ -92,7 +88,7 @@ fastapi dev main.py

You should see the following output:

```
```bash
INFO Using path main.py
INFO Resolved absolute path /home/user/code/awesomeapp/main.py
INFO Searching for package file structure from directories with __init__.py files
Expand Down Expand Up @@ -165,6 +161,16 @@ add_applications({
}, app=app)
```

## Tips & Tricks

### Running Behind a Proxy

In some cases, you might be running your FastAPI app behind a reverse proxy, which adds an extra path prefix that your application doesn't directly handle. This is common when working in environments like JupyterHub or deploying to Kubernetes.

For instance, if your FastAPI `/` endpoint is accessed at `https://some.domain/some/path/`, you will need to specify the path prefix when starting your FastAPI server. To do this, use the flag `--root-path /some/path/`. This ensures you can access the OpenAPI docs at `https://some.domain/some/path/docs`.

For more details, refer to the [Behind a Proxy](https://fastapi.tiangolo.com/advanced/behind-a-proxy/) guide.

## Conclusion

That's it! You now have embedded panel in FastAPI! You can now build off of this to create your own web app tailored to your needs.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ recommended = [
'plotly',
]
fastapi = [
'bokeh-fastapi == 0.1.0',
'uvicorn',
'bokeh-fastapi >= 0.1.0',
philippjfr marked this conversation as resolved.
Show resolved Hide resolved
'fastapi[standard]',
]
dev = [
'watchfiles',
Expand Down
Loading