I have a flask app running at one end and a dash app both are running separately but I have a link in the flask app home on clicking which redirects to dash app but I want to pass some values such as current user_id to dash app when while redirecting to dash app, then I want to read that value from URL and then I can display it dash home page.
I request if some can help me, please help me out.
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)
The search
parameter from location will return the URL params as string, pathname
did not work for me as stated in @xhlulu answer:
@app.callback(Output('page-content', 'children'),
Input('url', 'search')) # <--- this has changed
def display_page(params):
# e.g. params = '?firstname=John&lastname=Smith&birthyear=1990'
parsed = urllib.parse.urlparse(params)
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.