Launching Panel App

Hi Everyone,

I am trying to deploy a Panel app within FastAPI (following the documentation here). Things work fine locally while running the command: uvicorn main:app --reload

But since I want to deploy using Docker, I am running into an issue:

The step where the Panel App is served to a local port (say, 5000) blocks the rest of the code (e.g., launching a uvicorn server, etc, which serves the FastAPI app that consumes and renders the Panel App)

Here is a simple example:
main.py

import panel as pn
from bokeh.embed import server_document
from fastapi import FastAPI, Request
from fastapi.templating import Jinja2Templates
import uvicorn

app = FastAPI()
templates = Jinja2Templates(directory="templates")

@app.get("/")
async def read_root():
    return {"message": "Success"}

@app.get("/panelapp")
async def bkapp_page(request: Request):
    script = server_document("http://127.0.0.1:5000/app")
    return templates.TemplateResponse(
        "base.html", {"request": request, "script": script}
    )

def create_app():
    app = pn.Row("Hello World from Panel!")
    return app.servable()

pn.serve(
    {"/app": create_app},
    port=5000,
    allow_websocket_origin=["127.0.0.1:8000"],
    address="127.0.0.1",
    show=False,
)

if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=8000)

Any help would be useful.

Sam

1 Like

Maybe adding the argument ‘threaded=True” to pn.serve would be a possibility and help?

Thanks very much. This at least works to the extent that the base end point works, so the function is deployed.

The problem though is that the local panel server cannot be accessed when accessed by me outside the machine:

As you see, the text above the app in the HTML page is rendering well, but not the script that was to be placed there by the bokeh server_document.

Maybe it is a naive question, but would be glad to get some right direction to resolve this.

Best regards and thanks once again,
Sam

@Marc
Do you or others have any comments on this? I am trying to deploy an AWS lambda function with this small app for testing.

Sam