Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import io
- import dash
- import dash_html_components as html
- from flask import send_file
- import pandas as pd
- app = dash.Dash()
- app.layout = html.Div(
- children=[html.A("download excel", href="/download_excel/")])
- @app.server.route("/download_excel/")
- def download_excel():
- # Create DF
- d = {"col1": [1, 2], "col2": [3, 4]}
- df = pd.DataFrame(data=d)
- # Convert DF
- buf = io.BytesIO()
- excel_writer = pd.ExcelWriter(buf, engine="xlsxwriter")
- df.to_excel(excel_writer, sheet_name="sheet1")
- excel_writer.save()
- excel_data = buf.getvalue()
- buf.seek(0)
- return send_file(
- buf,
- mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
- attachment_filename="test11311.xlsx",
- as_attachment=True,
- cache_timeout=0
- )
- if __name__ == "__main__":
- app.run_server(debug=True)
Add Comment
Please, Sign In to add comment