This Dash tutorial page explains how to handle URLs using dcc.Location
. You can obtain the pathname as a callback input, and use a library like urllib
to parse it.
This snippet is adapted from one the examples and this StackOverflow thread:
import urllib.parse
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Location(id='url', refresh=False),
html.Div(id='page-content')
])
@app.callback(Output('page-content', 'children'),
Input('url', 'pathname'))
def display_page(pathname):
if pathname.startswith("my-dash-app"):
# e.g. pathname = '/my-dash-app?firstname=John&lastname=Smith&birthyear=1990'
parsed = urllib.parse.urlparse(pathname)
parsed_dict = urllib.parse.parse_qs(parsed.query)
print(parsed_dict)
# e.g. {'firstname': ['John'], 'lastname': ['Smith'], 'birthyear': ['1990']}
# use parsed_dict below
# ...
return page_content
if __name__ == '__main__':
app.run_server(debug=True)
<a link="https://www.example.com/dash/otherapi?userid=17116">
. – Hallucinosis