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

No query_params, session_args or FastAPI Query Params #7338

Open
MarcSkovMadsen opened this issue Sep 28, 2024 · 4 comments
Open

No query_params, session_args or FastAPI Query Params #7338

MarcSkovMadsen opened this issue Sep 28, 2024 · 4 comments
Labels
type: bug Something isn't correct or isn't working

Comments

@MarcSkovMadsen
Copy link
Collaborator

MarcSkovMadsen commented Sep 28, 2024

panel==1.5.1, bokeh-fastapi==0.1.0

No pn.state.location.query_params

In https://discourse.holoviz.org/t/how-to-access-query-params-when-running-panel-using-fastapi/8240 Makalai reports that pn.state.location.query_params is always empty when following https://panel.holoviz.org/how_to/integrations/FastAPI.html.

I can reproduce this via

import panel as pn
from fastapi import FastAPI
from panel.io.fastapi import add_application

app = FastAPI()


@app.get("/")
async def read_root():
    return {"Hello": "World"}


@add_application("/panel", app=app, title="My Panel App")
def create_panel_app():
    slider = pn.widgets.IntSlider(name="Slider", start=0, end=10, value=3)
    return pn.Column(
        pn.state.location.query_params,
        slider.rx() * "⭐",
    )
fastapi dev main.py

image

@MarcSkovMadsen MarcSkovMadsen added the type: bug Something isn't correct or isn't working label Sep 28, 2024
@MarcSkovMadsen MarcSkovMadsen changed the title No query_params when using FastAPI No query_params or session_args when using FastAPI Sep 28, 2024
@MarcSkovMadsen
Copy link
Collaborator Author

MarcSkovMadsen commented Sep 28, 2024

No pn.state.session_args

I see the same problem when using pn.state.session_args

import panel as pn
from fastapi import FastAPI
from panel.io.fastapi import add_application

app = FastAPI()


@app.get("/")
async def read_root():
    return {"Hello": "World"}


@add_application("/panel", app=app, title="My Panel App")
def create_panel_app():
    slider = pn.widgets.IntSlider(name="Slider", start=0, end=10, value=3)
    return pn.Column(
        pn.state.location.query_params,
        pn.state.session_args,
        slider.rx() * "⭐",
    )

image

@MarcSkovMadsen
Copy link
Collaborator Author

No FastAPI Query Parameters

According to https://fastapi.tiangolo.com/tutorial/query-params/ the query-params should be additional keywords not in the path. But that does not work either

import panel as pn
from fastapi import FastAPI
from panel.io.fastapi import add_application

app = FastAPI()


@app.get("/")
async def read_root():
    return {"Hello": "World"}


@add_application("/panel", app=app, title="My Panel App")
def create_panel_app(hello: str=""):
    print(hello)
    slider = pn.widgets.IntSlider(name="Slider", start=0, end=10, value=3)
    return pn.Column(
        pn.pane.Str(hello, styles={"border": "1px solid black"}),
        slider.rx() * "⭐",
    )

image

@MarcSkovMadsen MarcSkovMadsen changed the title No query_params or session_args when using FastAPI No query_params, session_args or FastAPI Query Params Sep 28, 2024
@philippjfr
Copy link
Member

This issue has been mentioned on HoloViz Discourse. There might be relevant details there:

https://discourse.holoviz.org/t/how-to-access-query-params-when-running-panel-using-fastapi/8240/2

@MarcSkovMadsen
Copy link
Collaborator Author

MarcSkovMadsen commented Sep 28, 2024

FYI. Syncing parameters works though

You can sync widget/parameter values to the url though

import panel as pn
from fastapi import FastAPI
from panel.io.fastapi import add_application

app = FastAPI()


@app.get("/")
async def read_root():
    return {"Hello": "World"}


@add_application("/panel", app=app, title="My Panel App")
def create_panel_app():
    widget = pn.widgets.FloatSlider(name='Slider', start=0, end=10)
    widget2 = pn.widgets.TextInput(name='Text')
    widget3 = pn.widgets.RangeSlider(name='RangeSlider', start=0, end=10)

    if pn.state.location:
        pn.state.location.sync(widget, {'value': 'slider_value'})
        pn.state.location.sync(widget2, {'value': 'text_value'})
        pn.state.location.sync(widget3, {'value': 'range_value'})

    return pn.Column(widget, widget2, widget3)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't correct or isn't working
Projects
None yet
Development

No branches or pull requests

2 participants