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'
)
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.