holoviz / panel

Panel: The powerful data exploration & web app framework for Python
https://panel.holoviz.org
BSD 3-Clause "New" or "Revised" License
4.76k stars 518 forks source link

No query_params, session_args or FastAPI Query Params #7338

Open MarcSkovMadsen opened 1 month ago

MarcSkovMadsen commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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

philippjfr commented 1 month ago

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 commented 1 month ago

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