Hi All!
I’m trying to download raw data in xls or xlsx format, but can’t get it to work. In the community I can already find how to download raw data in csv-format, which works perfectly fine for me:
# In app.layout
html.A(
'Download Data',
id='download-link',
download="rawdata.csv",
href="",
target="_blank"
)
@app.callback(
Output('download-link', 'href'),
[Input('dropdown-view-data', 'value')])
def update_download_button(value):
#Create DF
d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
csv_string = df.to_csv(index=False, encoding='utf-8')
csv_string = "data:text/csv;charset=utf-8,%EF%BB%BF" + urllib.parse.quote(csv_string)
return csv_string
As I’m using Dash within Django (and not Flask), I can’t use the flask-route approach described in https://community.plotly.com/t/allow-users-to-dowload-an-excel-in-a-click/9410.
I think that changing the csv_string should return me the right file. I came up with the following with the help of Google:
# In app.layout
html.A(
'Download Data',
id='download-link',
download="table.xls",
href="",
target="_blank"
)
@app.callback(
Output('download-link', 'href'),
[Input('dropdown-view-data', 'value')])
def update_download_button(value):
#Create DF
d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
writer = pd.ExcelWriter('table.xls', engine='xlsxwriter')
excel_data = df.to_excel(writer, sheet_name='Sheet1', index=False, encoding='base64')
excel_data = "data:application/vnd.ms-excel;base64" + urllib.parse.quote(excel_data)
return excel data
I have figured out how to save an Excel file on the server-side (by adding writer.save()), but I would like to have this on the client-side. Anyone any ideas if this is possible? Am I doing something wrong within excel_data?
Many thanks for your help!