Python 如何在多页应用程序上下载带有plotly dash的文件?

Python 如何在多页应用程序上下载带有plotly dash的文件?,python,flask,plotly-dash,Python,Flask,Plotly Dash,我已经知道以下方法(链接): 我无法使用server.route,因为上面显示的回调将捕获它 让文件仍然可以下载的最好方法是什么?好的,我现在已经解决了 在报告中说: dcc.Location组件表示web浏览器中的位置或地址栏 所以我使用了一个带有下载选项的html.A元素。如上所述,下载 提示用户保存链接的URL,而不是导航到该URL 这意味着当用户点击链接时,地址栏不会改变。因此,不会调用显示页面(路径名)中的回调,链接通过@server.route语句指向下载(路径)-方法。我找到了 请

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

我无法使用
server.route
,因为上面显示的
回调将捕获它


让文件仍然可以下载的最好方法是什么?

好的,我现在已经解决了

在报告中说:

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

所以我使用了一个带有下载选项的html.A元素。如上所述,
下载

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

这意味着当用户点击链接时,地址栏不会改变。因此,不会调用
显示页面(路径名)
中的
回调
,链接通过
@server.route
语句指向
下载(路径)
-方法。

我找到了
请注意,目录阶段是存储临时下载文件的位置
首先,我的目录树是这样的:

   root
    |
    |---apps
    |     └── main.py
    |---assets
    |---layouts
    |     |--layout.py
    |     └── error.py
    |---stage 
    |---app.py
    |---functions.py   
    └── index.py
这就是我的index.py的样子

import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State

from app import app
from apps import main


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 == "/main":
        return main.layout
    else:
        return main.error_page


if __name__ == "__main__":
    app.run_server(debug=True, host="llp-lnx-dt-10200", port=15021)
functions.py中,我将动态生成一个短划线表+带有下载链接的
html.a(…
,函数为

def display_final_results(table):

    import dash_html_components as html
    import dash_core_components as dcc
    import dash_table
    import pandas as pd

    return html.Div(
        [
            html.H5("""File processed and stuff worked"""),
            dash_table.DataTable(
                id="result_table",
                data=table.iloc[:20, :].to_dict("records"),
                columns=[{"name": i, "id": i} for i in list(table)],
            ),
            html.Hr(),
            dcc.Store(id="result_vault", data=table.to_dict()),
            html.A(id="download_link", children=html.Button(children="Download")),
        ]
    )
main.py中,我调用函数
def_final_results(table)
传入我要在仪表板表格中显示的表格以及下载链接

下面是main.py中的回调,后面是
app.server.route()

所以每次路由都会转到
https://llp-lnx-dt-10200:15021/apps/stage/filename
而不是
https://llp-lnx-dt-10200:15021/stage/filename


通过从url中删除应用程序,问题很快得到了解决。

你能分享你的解决方案吗?你能分享你的代码吗?你分享了面包屑。如果我弄明白了,我会在下面发布解决方案。嗨,我不在公司了,因此不再有代码了。但我想我使用它就像
html.A(下载='filename.txt')一样
谢谢你的回答,还有一个问题,你还记得你的
/download
目录在哪里吗?它是在
apps
中还是在你的根目录中。不管我怎么做,@server.route总是重定向到apps/download。如果我把
download
放在
apps
中,它就会失败到
apps/apps/download
。这就像玩多页应用的猫捉老鼠游戏。
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State

from app import app
from apps import main


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 == "/main":
        return main.layout
    else:
        return main.error_page


if __name__ == "__main__":
    app.run_server(debug=True, host="llp-lnx-dt-10200", port=15021)
def display_final_results(table):

    import dash_html_components as html
    import dash_core_components as dcc
    import dash_table
    import pandas as pd

    return html.Div(
        [
            html.H5("""File processed and stuff worked"""),
            dash_table.DataTable(
                id="result_table",
                data=table.iloc[:20, :].to_dict("records"),
                columns=[{"name": i, "id": i} for i in list(table)],
            ),
            html.Hr(),
            dcc.Store(id="result_vault", data=table.to_dict()),
            html.A(id="download_link", children=html.Button(children="Download")),
        ]
    )
@app.callback(
    Output("download_link", "href"),
    [Input("result_vault","data"),
     Input("h5_filename", "children")]
)
def return_download_link(data, upload_filename):
    
    shutil.rmtree("stage")
    os.mkdir("stage")

    target = pd.DataFrame(data)
    download_filename = upload_filename.split(":")[1].strip() + f"""_{filestamp()}.xlsx"""
    uri = f"""stage/{download_filename}"""
    target.to_excel(
        uri, engine="xlsxwriter", index=False, header=True, sheet_name="results"
    )

    return uri

@app.server.route("/stage/<path:path>")
def serve_static(path):
    root_dir = os.getcwd()
    return flask.send_from_directory(os.path.join(root_dir, "stage"), filename=path)
if pathname == "apps/main":
    return main.layout