From 79d9ba2901ddc3e5b26545080b39eb24ea57cd07 Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Tue, 24 Sep 2024 14:38:54 +0200 Subject: [PATCH] fastapi docs (#7317) --- doc/how_to/integrations/FastAPI.md | 30 ++++++++++++++++++------------ pyproject.toml | 4 ++-- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/doc/how_to/integrations/FastAPI.md b/doc/how_to/integrations/FastAPI.md index fec10d37d2..532e1dccb2 100644 --- a/doc/how_to/integrations/FastAPI.md +++ b/doc/how_to/integrations/FastAPI.md @@ -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] ``` ::: -:::{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). ::: :::: @@ -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 @@ -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. diff --git a/pyproject.toml b/pyproject.toml index 6e1fd344cf..2bcd8dce9f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -77,8 +77,8 @@ recommended = [ 'plotly', ] fastapi = [ - 'bokeh-fastapi == 0.1.0', - 'uvicorn', + 'bokeh-fastapi >= 0.1.0', + 'fastapi[standard]', ] dev = [ 'watchfiles',