I generate an in-memory Excel file via pd.ExcelWriter and BytesIO for a click event in my Python3.8 dash app.
Everything works as it should be. When I download my file I get this popup message asking me if I would like to continue to download/open the generated file. However, the popup message shows this (I guess base64 encoded) string (or path?), e.g. ...ydaHdjhgk328AAAAnxsAA== and after downloading the download gets a (randomly assigned?) set of characters as the filename (e.g. ZySzsdn1.xlsx).
How can I adjust this so it would display and assign the filename to something like download.xlsx? My guess is that is has something to do with the base64 encoded href.
The function to generate the excel file:
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
The button in my Dash application:
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="",
),
],
),
And finally my callback:
@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 = write_product_file()
excelfile.seek(0)
media_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
data = base64.b64encode(excelfile.read()).decode("utf-8")
href_data = f"data:{media_type};base64,{data}"
return href_data, "success", href_data,
else:
return None, "secondary", None