From e7ee3c7d25b7907a975ecd87c5619fc3be6820e0 Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Mon, 23 Sep 2024 17:45:40 +0000 Subject: [PATCH 1/3] fastapi docs --- doc/how_to/integrations/FastAPI.md | 32 +++++++++++++++++------------- pyproject.toml | 4 ++-- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/doc/how_to/integrations/FastAPI.md b/doc/how_to/integrations/FastAPI.md index fec10d37d2..a03ad1e230 100644 --- a/doc/how_to/integrations/FastAPI.md +++ b/doc/how_to/integrations/FastAPI.md @@ -2,32 +2,26 @@ 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 and uvicorn based server via . 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). 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` -```bash -pip install panel[fastapi] -``` +:::{tab-item} `conda` + +Currently not supported + ::: :::: @@ -92,7 +86,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 +159,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', From 2dc2a1e4102ac61900b0e9db2c16ba067e28f794 Mon Sep 17 00:00:00 2001 From: Marc Skov Madsen Date: Mon, 23 Sep 2024 17:51:02 +0000 Subject: [PATCH 2/3] remove word --- doc/how_to/integrations/FastAPI.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/how_to/integrations/FastAPI.md b/doc/how_to/integrations/FastAPI.md index a03ad1e230..01c8aa9e5a 100644 --- a/doc/how_to/integrations/FastAPI.md +++ b/doc/how_to/integrations/FastAPI.md @@ -2,7 +2,7 @@ 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 via . 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. From 368713e9147d48c57221d45a1da2bcd364562290 Mon Sep 17 00:00:00 2001 From: Philipp Rudiger Date: Tue, 24 Sep 2024 14:38:33 +0200 Subject: [PATCH 3/3] Update note on conda install --- doc/how_to/integrations/FastAPI.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/how_to/integrations/FastAPI.md b/doc/how_to/integrations/FastAPI.md index 01c8aa9e5a..532e1dccb2 100644 --- a/doc/how_to/integrations/FastAPI.md +++ b/doc/how_to/integrations/FastAPI.md @@ -19,9 +19,11 @@ pip install panel[fastapi] ::: :::{tab-item} `conda` +```bash +conda install -c conda-forge bokeh-fastapi +``` -Currently not supported - +Note: As of September 24th 2024 this is waiting on [conda-forge/staged-recipes#27575](https://github.com/conda-forge/staged-recipes/pull/27575). ::: ::::