Capture query string for use in callback

Is it possible to use query strings in callbacks? In the docs, the query string value in the URL is used inside the layout, but I’d like to capture the query parameter and use it as a callback input.

You should be able to pull them down with a dcc.Location callback? Something like:

dcc.Location(id='url')

@app.callback(Output('some-div','children'),Input('url','pathname')
def do_something(url)
     return url
1 Like

Thanks @bgivens33 – I’m using Dash Pages and specifying the path in dash.register_page() in each page, and I’m hoping to take additions to the URL as parameters. But if I just append some value like /parameter in the URL to then capture it from dcc.Location, the page doesn’t render (unless I’m misinterpreting how I can leverage that)

Hello @nlyle,

Read this:

Go down to the part about query strings.

Query strings start with a ?, a different page location starts with a /.

Thanks @jinnyzor – I’d like to use Query Strings, but I’m wondering how to capture the query parameter value for later use in a callback?

In the docs it looks like the values are meant to just be used in the layout. Maybe in my layout, I could use dcc.Store(data={query_param}, id="query-param").

Edit: I see dcc.Location has a search property which might be useful

1 Like

That’s what I was also thinking you could use.

1 Like

Thanks all - I ended up using dcc.Location’s search value, and then parsing it with some regex to separate the parameter name and parameter value. I could then use those values in a callback to update my AG Grid’s filterModel.

@callback(
    Output('data-grid', 'filterModel'),
    Input('url', 'search'),
)