Plotly Dash URL 路径名回调在加载页面时未触发

Plotly Dash URL pathname callback not firing when loading page

我正在尝试使用 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,我使用的网络套接字以 wsgi.py 和 gunicorn 开头,如 this post 中所述。

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

我在这里错过了什么?

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

您需要向布局添加一个 dcc.Location 对象,其 id 应与您在回调中使用的对象匹配(url" 在 Input('url', 'pathname')).

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")]
)