当单击按钮(dbc.Button)时,我正在使用 Dash 下载 excel 文件,该文件已存在于我的代码存储库中。这是代码:
dbc.Button(
href=os.path.join("/", "static", "Template.xlsx"),
download="Template.xlsx",
external_link=True,
children=["Download Template"],
style={
"background-color": "#4CAF50",
"border": "none", # No border
"fontWeight": "bold",
"width":"200px",
"marginBottom":"20px"
}
)
该代码在 Windows 中是工作文件,即它下载文件并且文件可以正确打开,但相同的代码在 linux(部署版本)中不起作用,它下载文件(文件大小比平常小 6kb:14kb)并且文件也是显示已损坏
这可能是由于多种原因造成的,因为您的应用程序在 Linux 与 Windows 上的部署方式存在差异。你没有指定。但是,您可能想首先尝试在 Python (Plotly Dash) 代码中对下载功能进行更稳健的编码。
例如,使用
dash.dcc.send_file
:
from dash import dcc, html, Input, Output, Dash
import dash_bootstrap_components as dbc
import os
app = Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = html.Div(
[
dbc.Button(
"Download Template",
id="download-button",
style={
"background-color": "#4CAF50",
"border": "none",
"fontWeight": "bold",
"width": "200px",
"marginBottom": "20px",
},
),
dcc.Download(id="download"),
],
style={"width": "50%", "textAlign": "center", "margin": "5%"},
)
@app.callback(
Output("download", "data"), [Input("download-button", "n_clicks")]
)
def download_file(n):
if n is None:
return dcc.no_update
return dcc.send_file(
os.path.join("static", "Template.xlsx"), filename="Template.xlsx"
)
if __name__ == "__main__":
app.run_server(debug=True)