小编Ste*_*eij的帖子

单击按钮时 Dash 下载内存中生成的文件:如何给出文件名?

我生成内存通过Excel文件pd.ExcelWriter,并BytesIO在我的Python3.8仪表应用点击事件。

一切正常。当我下载我的文件时,我收到此弹出消息,询问我是否要继续下载/打开生成的文件。但是,弹出消息显示了这个(我猜是base64编码的)字符串(或路径?),例如...ydaHdjhgk328AAAAnxsAA==,下载下载后会获得一组(随机分配的?)字符集作为文件名(例如ZySzsdn1.xlsx)。

我该如何调整它才能显示文件名并将其分配给类似的东西download.xlsx?我的猜测是这与base64编码的href.

生成excel文件的函数:

def write_product_file():
    output = BytesIO()
    writer = pd.ExcelWriter(output, engine="xlsxwriter")
    upload_df = pd.DataFrame()
    upload_df.to_excel(writer, index=False, sheet_name="sheet1")
    writer.save()
    return output
Run Code Online (Sandbox Code Playgroud)

我的 Dash 应用程序中的按钮:

html.Div(
  id="select-upload-form",
  style={"width": "100%"},
  children=[
    dbc.Button(
      "Download the upload form",
      id="download-excel",
      color="secondary",
      external_link="true",
      target="",
      href="",
    ),
  ],
),
Run Code Online (Sandbox Code Playgroud)

最后我的回调:

@app.callback(
    [
        Output("download-excel", "href"),
        Output("download-excel", "color"),
        Output("download-excel", "target"),
    ],
    [Input("download-excel", "n_clicks")],
)
def download_template_file(n_clicks):
    if n_clicks:
        excelfile = …
Run Code Online (Sandbox Code Playgroud)

filenames download button python-3.x plotly-dash

3
推荐指数
1
解决办法
1501
查看次数

标签 统计

button ×1

download ×1

filenames ×1

plotly-dash ×1

python-3.x ×1