简体   繁体   中英

Streamlit - How to let users download an Excel file from the app?

I want users to be able to download an Excel file by clicking a button. I have an existing Excel file, though it can also be generated from a dataframe, that I want to be provided in Excel format.

Documentation gives an example for.csv files:

with open('my_file.csv') as f:
    st.download_button('Download', f)

but I can't adapt this use case for an Excel file. I can't manage to put the excel file in the right format so that the download_button method accepts it. I tried passing pd.to_excel() object but it also didn't work.

I'll appreciate any and every suggestion!

Found the solution:

with open(file_path, 'rb') as my_file:
    st.download_button(label = 'Download', data = my_file, file_name = 'filename.xlsx', mime = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')      

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM