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

Passing query parameters to an embedded Panel app #4190

Closed
Midnighter opened this issue Dec 13, 2022 · 3 comments
Closed

Passing query parameters to an embedded Panel app #4190

Midnighter opened this issue Dec 13, 2022 · 3 comments

Comments

@Midnighter
Copy link
Contributor

Midnighter commented Dec 13, 2022

ALL software version info

Package Information

Package Version
panel 0.14.1

Dependency Information

Package Version
aiohttp 3.8.3
bleach 5.0.1
bokeh 2.4.3
holoviews 1.15.3
hvplot 0.8.2
ipython 8.7.0
markdown 3.4.1
matplotlib 3.6.2
networkx 2.8.8
pandas 1.5.2
param 1.12.3
pillow 9.3.0
pyct 0.4.8
pyviz-comms 2.2.1
requests 2.28.1
scipy 1.9.3
setuptools 65.6.3
tqdm 4.64.1
typing-extensions 4.4.0

Build Tools Information

Package Version
pip 22.3.1
setuptools 65.6.3
wheel 0.38.4

Platform Information

Linux 6.0.6-76060006-generic-x86_64
CPython 3.10.8

Description of expected behavior and the observed behavior

I would like to use query params within my panel application which is embedded inside of a FastAPI app. I thought this would be a simple matter of forwarding the query parameters from the FastAPI endpoint. However, (at least with reloading enabled) it seems that panel performs some URL transformations that do not take into account existing query params. With the following example below, the following URL is created.

http://127.0.0.1:5000/example?view=1/autoload.js?bokeh-autoload-element=1002&bokeh-app-path=/example&bokeh-absolute-url=http://127.0.0.1:5000/example?view=1

As you can see /autoreload.js etc. is directly appended without removing the outer query param.

Complete, minimal, self-contained example code that reproduces the issue

mre.py

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


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


@app.get("/")
async def bokeh_app(request: Request):
    script = server_document(f"http://127.0.0.1:5000/example?{request.query_params}")
    return templates.TemplateResponse(
        "panel.html", {"request": request, "script": script}
    )


def create_app():
    return pn.Column(str(pn.state.session_args))


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

panel.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Panel</title>
    </head>
    <body>
        {{ script|safe }}
    </body>
</html>

You need to install FastAPI, panel, and uvicorn, then start the app with:

uvicorn --reload mre:app
@hoxbro
Copy link
Member

hoxbro commented Dec 13, 2022

@Midnighter
Copy link
Contributor Author

Perfect, that looks almost exactly like my FastAPI app. Hadn't seen it before. I'll give it a try but looks like it'll be my solution.

@Midnighter
Copy link
Contributor Author

Was able to confirm that this works. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants