I’m trying to create an app, that converts the resulting data frame (df) into an Excel file. The program is working fine in jupyter notebook. However, when integrating this into Streamlit, I am encountering an issue where the program appears to revert to a previous step.
Steps to reproduce
Code snippet:
if df is not None:
file_name = st.text_input('Enter a file name for the Excel file (e.g., email_data.xlsx)')
download = st.download_button(label='Download Excel', data=df.to_excel(index=False, header=True), key='download')
if file_name and download:
with open(file_name, "wb") as f:
f.write(download)
st.success(f"File '{file_name}' has been downloaded successfully.")
Expected behavior:
It should create an excel file.
Actual behavior:
It’s getting back to the previous step and not responding to the download function.
Hi @Shawn_Pereira, this isn’t working. This is responding in the same way as the provided code. Any other solution would be really appreciated.
Thanks.
For me, that code shows a download button after filling in a file name. Clicking the download button downloads the dataframe as an excel file. If that is not what you want please clarify.
Your code cannot possibly work because df.to_excel(index=False, header=True) will raise a TypeError.The first parameter to DatFrame.to_excel must be:
excel_writer : path-like, file-like, or ExcelWriter object
@Sidra_Tul_Muntaha, the code works perfectly on my computer. I am using the latest version on Streamlit
You need to provide your file name and then click on the download button. If you want, I can upload a GIF file displaying its working… Do copy-past only my code into a new .py file and check the same at your end.
It will be awesome if you can share any source to your code something like github repo like or streamlit project link aside
there is how you can covert it into excel you have to import xlsxwriter
pip install xlxswriter
import
df = pd.DataFrame(data)
# download button to download dataframe as xlsx
with pd.ExcelWriter(buffer, engine='xlsxwriter') as writer:
# Write each dataframe to a different worksheet.
df.to_excel(writer, sheet_name='Sheet1', index=False)
download2 = st.download_button(
label="Download data as Excel",
data=buffer,
file_name='large_df.xlsx',
mime='application/vnd.ms-excel'
)