Iâm working on an app that allows for information to be downloaded to a Microsoft Excel file. I use XLSXWriter (https://xlsxwriter.readthedocs.io/) to create a file. Using inputs from Streamlit, the app I have created then downloads (or at least I am hoping it will) the excel file for the user. XLSXWriter returns a:
<class âxlsxwriter.workbook.Workbookâ>
I am using the download button with the following code:
st.download_button(label=âExport to Excelâ
, data = excel_file # excel file is the name of the downloaded <class âxlsxwriter.workbook.Workbookâ>
, file_name= âdf_test.xlsxâ
, mime = âapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheetâ)
Any help on how to make this work would be greatly appreciated.
Hi @user-3135, welcome to the Streamlit community!!
Iâve shown how to use xlsxwriter
, pandas
, and st.download_button
to download an Excel file here: How to add a download excel/csv function to a button? - #16 by snehankekre
Best,
Snehan
Thank You. Iâve gone over that. I poorly explained where my holdup is. I donât have a dataframe that writes. A function takes in parameters and exports an excel sheet. The users for my app care about formating.
I wrote a function that makes a pretty excel sheet with xlsxwriter. It is super long (2000+ lines), but for broad strokes purposes the function does the following:
def write_excel(file_path, string_input):
workbook = xlsxwriter.Workbook(file_path)
worksheet = workbook.add_worksheet()
worksheet.write(âA1â, string_input)
workbook.close()
My end goal in streamlit is to call said function like so:
excel_file = write_excel(â/file_locationâ, âhello world!â)
I am having trouble getting that (the pretty excel file) to work with my Streamlit app.
Thank you for welcoming me to the Streamlit community! So far everything has been AMAZING!
I am having trouble getting that (the pretty excel file) to work with my Streamlit app.
By trouble you mean youâve not been able to use st.download_button
to download the Excel file?
That is correct. Currently, my function returns a <class âxlsxwriter.workbook.Workbookâ>
If I put that in the data part of the download button, Iâll get an error that says": Invalid binary data format: <class âxlsxwriter.workbook.Workbookâ>"
Right now Iâm trying to figure out how to tie the buffer = io.BytesIO() to the xlsxwriter function; however, Iâm a novice at this so Iâm doing a lot of reading at the moment. The goal would be to convert the Workbook to a binary data format.
Iâll offer a quick update. I changed my approach. I now specify the path when I create the file rather than try to use the download_button. This isnât ideal; however, Iâll choose working solutions over temporary solutions. Anyway. That works when I run my app on the local machine. When I connect to the app on Streamlitâs servers, I cannot get the file to appear anywhere on my computer. Iâm working to fix that problem now. I do not know entirely how this works so I have more reading to do the rest of tonight and tomorrow.
Hi @user-3135
Hereâs a reproducible example that uses st.download_button
to download an Excel workbook written to an in-memory string with BytesIO
:
import streamlit as st
import xlsxwriter
from io import BytesIO
output = BytesIO()
# Write files to in-memory strings using BytesIO
# See: https://xlsxwriter.readthedocs.io/workbook.html?highlight=BytesIO#constructor
workbook = xlsxwriter.Workbook(output, {'in_memory': True})
worksheet = workbook.add_worksheet()
worksheet.write('A1', 'Hello')
workbook.close()
st.download_button(
label="Download Excel workbook",
data=output.getvalue(),
file_name="workbook.xlsx",
mime="application/vnd.ms-excel"
)
You should be able to adapt this example to your use-case. Let us know if you run into roadblocks!
Happy Streamlit-ing!
Snehan
That worked! Thank You SO MUCH!!! You saved me hours of deep diving into the internet to fix this thing!
Great!!
Worked for me tooâŚ
This saved me a lot of time.
Thank You,
Streamlit is Amazing
thank u first ! but Iâm confused about when will the data in memory be cleared? or how to clear?
yes, I mean the python is stream! thank you very much!!
Hi, this works great for me locally. But when I deploy to azure cloud I get âthe site canât be reachedâ error. I am able to download another pickled file in a similar way an a pdf by generating a link, so I donât think it has to do with az cloud restrictions.
Any ideas on how to solve this? Best regards