如何在多页面应用程序中使用plotly-dash下载文件?

卢卡斯

我已经知道以下方法(在此处链接):

server = Flask(__name__)
app = dash.Dash(server=server)


@server.route("/download/<path:path>")
def download(path):
    """Serve a file from the upload directory."""
    return send_from_directory(UPLOAD_DIRECTORY, path, as_attachment=True)

但是问题是,当我使用Plotly所建议的多页面方法时(在此处链接(在“构建多页面应用程序的结构”-下方index.py)):

    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 == '/apps/app1':
        return app1.layout
    elif pathname == '/apps/app2':
        return app2.layout
    else:
        return '404'

我无法使用,server.route因为它会被上callback图所示抓住

仍然使文件可下载的最佳方法是什么?

卢卡斯

好的,我现在已经解决了。

文档中说:

dcc.Location组件表示Web浏览器中的位置或地址栏。

所以我使用了带有下载选项的html.A元素。如前所述这里download

提示用户保存链接的URL,而不是导航到该URL。

这意味着,当用户单击链接时,它不会更改地址栏。因此,不会调用callbackfrom中的from,display_page(pathname)而是download(path)通过该@server.route语句将链接定向到-method

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章