How to reference url parameters on page load?

I’m interested in taking optional query parameters, and passing them to an LLM, and updating the ChatInterface.

For example:

https://localhost:3000/ ← no behavior, just renders ChatInterface
https://localhost:3000/?query=who+is+president ← executes the query, updates the ChatInterface

I cant quite figure out how to watch the location though…

    def run_after(*args, **kwargs):
        print(args), "location")

…I get:

Exception has occurred: ValueError
location parameter was not found in list of parameters of class _state

I’ve also tried the example

    widget = pn.widgets.FloatSlider(name='Slider', start=0, end=10)
    if pn.state.location:
        pn.state.location.sync(widget, {'value': 'slider_value'})

…but setting http://localhost:21000/app?slider_value=3 doesnt set the slider value.

What browser are you using? The latter example works for me:

import panel as pn

pn.extension(design='material', template='material')

pn.state.template.main_max_width = '768px'

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'})

pn.Column(widget, widget2, widget3).servable()

Hi @ahuang11 thanks for your reply, you are fast! I’m using Chrome.

One possible difference is I’m running pn.serve() vs servable(), passing in the template.

    APP_ROUTES = {"app": template}

        ...<other config values>

Does that make a difference?

1 Like

Potentially :slight_smile:
Are you able to try panel serve

You can additionally rely on the state property session_args that returns a dict of query parameters to their bytes-encoded values. It’s useful when you only need to catch the query parameters and not sync them. Search for session_args on these pages for more details:

Thanks @ahuang11 and @maximlt, it works using .servable() on a much smaller project mimicking your example, but fails with panel serve on my project which uses serve(). state and session_args never seems to be populated, possibly I’m trying to access them in the wrong place. I’ll dig in a little further.