ume*_*esh 6 python integration flask plotly-dash
我有一个在一端运行的烧瓶应用程序和一个破折号应用程序都单独运行,但我在烧瓶应用程序主页中有一个链接,单击该链接将重定向到破折号应用程序,但我想将一些值(例如当前 user_id)传递给破折号应用程序重定向到 dash 应用程序,然后我想从 URL 读取该值,然后我可以显示它的 dash 主页。
我请求有人可以帮助我,请帮助我。
此Dash 教程页面解释了如何使用dcc.Location
. 您可以获取路径名作为回调输入,并使用类似的库urllib
来解析它。
此片段改编自示例和StackOverflow 线程:
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)
Run Code Online (Sandbox Code Playgroud)