Plotly Dash URL路径名回调在加载页面时未启动



我正在尝试使用url回调构建一个多页Dash应用程序。我的应用程序dash_apps.py看起来是这样的:

from flask import Flask
from flask import request
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
server = Flask(__name__)
app = dash.Dash(
__name__,
server = server,
serve_locally = False,
requests_pathname_prefix = "/plotary/dash/",
)
app.layout = html.Div([
html.Div([
html.A([
html.Div(id="logo")
],href='https://man-es.com'),
html.Div([
html.H1('Keine Auswertung ausgewählt. ')
],id="description"),
],
id="navbar")]
)
@app.callback(Output('description', 'children'),
[Input('url', 'pathname')])
def display_page(pathname):
return html.Div([
html.H1('Auswertung Nr {}'.format(pathname))
])
@server.route('/plotary/dash/<int:report_id>')
def create_report(report_id):
return app
if __name__ == '__main__':
app.run_server(debug=True)

并通过一个wsgi.py调用,它看起来像这样:

from dash_apps import server
if __name__ == "__main__":
server.run()

我使用的服务器运行nginx,我使用一个web套接字,该套接字通过gunicorn启动wsgi.py,如本文所述。

现在,如果我打开例如http://<ip.to.server>/plotary/dash/18,它将只显示Keine Auswertung ausgewählt.,尽管我希望它显示Auswertung Nr. 18

我在这里错过了什么?

或者,我也可以从Flask的route中获得report_id,但我不知道如何将此变量传递给app.layout

您需要在布局中添加一个dcc.Location对象,它的id应该与您在回调中使用的对象匹配(在Input('url', 'pathname')的情况下为"url"(。

app.layout = html.Div([
dcc.Location(id='url', refresh=False),
html.Div([
html.A([
html.Div(id="logo")
],href='https://man-es.com'),
html.Div([
html.H1('Keine Auswertung ausgewählt. ')
],id="description"),
],
id="navbar")]
)

最新更新