加载页面时 Plotly Dash URL 路径名回调不会触发

Axe*_*xel 1 python callback flask plotly-dash

我正在尝试使用 url 回调构建多页面 Dash 应用程序。我的应用程序名为 dash_apps.py 如下所示:

\n
from flask import Flask\nfrom flask import request\nimport dash\nimport dash_core_components as dcc\nimport dash_html_components as html\nfrom dash.dependencies import Input, Output, State\n\nserver = Flask(__name__)\napp = dash.Dash(\n    __name__,\n    server = server,\n    serve_locally = False,\n    requests_pathname_prefix = "/plotary/dash/",\n)\n\napp.layout = html.Div([\n        html.Div([\n        html.A([\n            html.Div(id="logo")\n        ],href=\'https://man-es.com\'),\n        html.Div([\n            html.H1(\'Keine Auswertung ausgew\xc3\xa4hlt. \')\n        ],id="description"),\n    ],\n    id="navbar")]\n)\n\n@app.callback(Output(\'description\', \'children\'),\n              [Input(\'url\', \'pathname\')])\ndef display_page(pathname):\n    return html.Div([\n        html.H1(\'Auswertung Nr {}\'.format(pathname))\n    ])\n\n@server.route(\'/plotary/dash/<int:report_id>\')\ndef create_report(report_id):\n    return app\n\nif __name__ == \'__main__\':\n    app.run_server(debug=True)\n
Run Code Online (Sandbox Code Playgroud)\n

并通过 wsgi.py 调用,如下所示:

\n
from dash_apps import server\n\nif __name__ == "__main__":\n    server.run()\n
Run Code Online (Sandbox Code Playgroud)\n

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

\n

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

\n

我在这里缺少什么?

\n

report_id或者,我也可以从 Flask\'s获取route,但是我不知道如何将此变量传递给app.layout.

\n

Dan*_*l R 8

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

\n
app.layout = html.Div([\n    dcc.Location(id='url', refresh=False),\n    html.Div([\n    html.A([\n        html.Div(id="logo")\n    ],href='https://man-es.com'),\n    html.Div([\n        html.H1('Keine Auswertung ausgew\xc3\xa4hlt. ')\n    ],id="description"),\n],\nid="navbar")]\n)\n
Run Code Online (Sandbox Code Playgroud)\n