Manually setting url state with `update_query`

Hello,

I am trying to write a panel application where I manually set a query parameter to the url like so:

import panel as pn

dashboard = pn.Row(pn.Column(pn.Row(pn.Pane("Classification", width=200), pn.Pane("Title", width=400))))
pn.template.FastListTemplate(
    site="Panel",
    title="Material Test Summary",
    main=[dashboard], main_max_width="100%",
).servable()
pn.state.location.update_query(some_key="some_value")

But this doesn’t seem to work. I run the panel server like so:

panel serve <filename>.py --dev --show

I also cannot show the url data when I initially enter the app if I print:

print(pn.state.location.query_params)
1 Like

Hi @sammaphey

Welcome to the community. I believe @Hoxbro contributed some fixes for what you are experiencing for the next version of Panel (v0.13)?

For now you can use something like

import panel as pn

pn.extension(sizing_mode="stretch_width", template="fast")

some_key = pn.state.session_args.get("some_key", [b"some_value"])[0].decode()

dashboard = pn.Row(
    pn.Column(
        pn.pane.Markdown(f"some_key: {some_key}"),
        pn.Row(
            pn.Pane("Classification", width=200), pn.Pane("Title", width=400)
        )
    )
).servable()


def update_query_args(): 
    pn.state.location.update_query(some_key=some_key)

pn.state.onload(update_query_args)

accent = "#ffa600"
pn.state.template.param.update(title="Query Args", accent_base_color=accent, header_background=accent)

Hi Marc,

This solution works well if you know the state that you want the application to be in in the first place. But I would really prefer that the application drive the state.

For instance I should initialize my application with a certain state that is driven by the url.

Say I enter the server with the following URL: http://localhost:5006/material-test-summary-panel?app_user=sammaphey&records=123456&relationships=456789 if I log the state location (pn.state.location.query_params) then the resulting dict looks like: {}, when I would expect it to look like: {'app_user': ['sammaphey'], 'records': ['123456'], ...}. So How am I supposed to get the url state when the app is initially loaded? utilizing the pn.state.onload method does not seem to work unless I manually set what the state should be.

In addition how would I now be able to change any of the url params as the application runs? Using update_query doesn’t actually seem to do anything if I call it when I make a change to a particular parameter in the application