Hi Everyone!
I’m new to Dash and loving it so far! I was wondering whether it is possible to use a xlsxwriter.workbook.Workbook object with the dcc.Download component? I was looking at the docs and it seems pretty straightforward to create a single-page, single-dataframe example but I wasn’t sure how to go about sending this writer object over. I have the writer object created with something along the lines of the following code (apologies if I’m missing something because I rewrote my original code – the error message I got here and in my original code was “AttributeError: ‘XlsxWriter’ object has no attribute ‘name’”):
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
import pandas as pd
import numpy as np
import xlsxwriter
app = dash.Dash(__name__)
app.layout = html.Div(
children = [
html.Button(
id = 'export-button',
children = 'Export',
),
dcc.Download(
id = "download-data"
),
]
)
@app.callback(
Output('download-data', 'data'),
Input('export-button', 'n_clicks')
)
def export_data(n_clicks):
filename = 'placeholder.xlsx'
df1 = pd.DataFrame(np.random.randint(0, 100, size = (17, 3)), columns = ['A', 'B', 'C'])
df2 = pd.DataFrame(np.random.randint(0, 100, size = (17, 3)), columns = ['A', 'B', 'C'])
df3 = pd.DataFrame(np.random.randint(0, 100, size = (17, 3)), columns = ['A', 'B', 'C'])
writer = pd.ExcelWriter(filename, engine = 'xlsxwriter')
df1.to_excel(writer, sheet_name = 'df1')
df2.to_excel(writer, sheet_name = 'df2')
df3.to_excel(writer, sheet_name = 'df3')
# At this point, the writer has been created, but I don't know how to proceed to allowing this to interact with the dcc.Download component if even possible -- the rest of this is just conjecture...
# This return statement was really just a hail-mary attempt
return dcc.send_data_frame(writer, filename = filename)
if __name__ == '__main__':
app.run_server(debug=True, port=8036)
If this method is along the wrong lines, I’m also more than happy to explore other options if anyone has suggestions!
Thanks in advance for the help!