Pn.state.location.pathname is empty, why?

I’m trying to understand why I can see the pathname in the browser, but not programmatically. Let me show you what I see:

Now, that’s coming from pn.Row(..., pn.state.location).servable()

It has all the information I wanna see, its great, but when I run print('pn.state.location.pathname', pn.state.location.pathname) it’s empty; let me show you:

very confusing to me. Is this normal, or is this a bug? If it’s normal, is there any way to access the pathname, (or any of the other attributes) programmatically?

Here’s my full notebook, which is served via panel serve, then shown in an iframe through a flask app:

import panel as pn

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)
widget4 = pn.widgets.TextInput(name='Text')

if pn.state.location:
    print('pn.state.location.pathname', pn.state.location.pathname)
    print(type(pn.state.location.pathname))
    pn.state.location.sync(widget, {'value': 'slider_value'})
    pn.state.location.sync(widget2, {'value': 'text_value'})
    pn.state.location.sync(widget3, {'value': 'range_value'})
    widget4.param.value = pn.state.location.pathname

pn.Row(widget, widget2, widget3, widget4, pn.state.location).servable()

pn.state.location is not populated until the page is rendered so when the print is executed the frontend will not have sent back a message containing that information yet. If you need the request argument you can use pn.state.session_args.

1 Like

ah, thanks!

I thought that might mean I could depend on it, but this doesn’t seem to work to access the data either. What’s the correct way for me to access the data?

@pn.depends(pn.state.location)
def my_location(loc):
    print(loc.pathname)
    return pn.pane.HTML(loc.pathname)

pn.Column(my_location, pn.state.location).servable()

perhaps the right way to do this is to use the curdoc object instead:

pn.state.curdoc.session_context.server_context.application_context.url

seems to work for me

1 Like

Your approach of depending on pn.state.location should work but it’s probably better to be explicit and use pn.depends(pn.state.location.param.pathname)

1 Like