In case it helps, here’s an example of creating an Excel file w/ openpyxl, saving it to a stream vs. a file on disk, and sending to a user. A non-Pandas example : ) Also, using an export button.
import io
import dash
import dash_html_components as html
from flask import send_file
from openpyxl import Workbook
app = dash.Dash()
app.layout = html.Div(
children=[html.A(id='export_link', href='/download_excel/', children=[html.Button(id='export_button', type='button', children=['Export'])])])
@app.server.route("/download_excel/")
def download_excel():
workbook = Workbook()
worksheet = workbook.active
worksheet.cell(column=1, row=1, value='hi!')
# save workbook to a stream (vs. saving to a file on disk) to later send to user
excel_stream = io.BytesIO()
workbook.save(excel_stream)
excel_stream.seek(0) # go to the beginning of the stream
return send_file(
excel_stream,
mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
attachment_filename="greeting.xlsx",
as_attachment=True,
cache_timeout=0
)
if __name__ == "__main__":
app.run_server(debug=True)