Guest User

Untitled

a guest
Sep 25th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. import io
  2. import dash
  3. import dash_html_components as html
  4. from flask import send_file
  5. import pandas as pd
  6.  
  7.  
  8. app = dash.Dash()
  9. app.layout = html.Div(
  10. children=[html.A("download excel", href="/download_excel/")])
  11.  
  12.  
  13. @app.server.route("/download_excel/")
  14. def download_excel():
  15. # Create DF
  16. d = {"col1": [1, 2], "col2": [3, 4]}
  17. df = pd.DataFrame(data=d)
  18.  
  19. # Convert DF
  20. buf = io.BytesIO()
  21. excel_writer = pd.ExcelWriter(buf, engine="xlsxwriter")
  22. df.to_excel(excel_writer, sheet_name="sheet1")
  23. excel_writer.save()
  24. excel_data = buf.getvalue()
  25. buf.seek(0)
  26.  
  27. return send_file(
  28. buf,
  29. mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  30. attachment_filename="test11311.xlsx",
  31. as_attachment=True,
  32. cache_timeout=0
  33. )
  34.  
  35.  
  36. if __name__ == "__main__":
  37. app.run_server(debug=True)
Add Comment
Please, Sign In to add comment