簡體   English   中英

Streamlit - 如何讓用戶從應用程序下載 Excel 文件?

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

我希望用戶能夠通過單擊按鈕來下載 Excel 文件。 我有一個現有的 Excel 文件,盡管它也可以從 dataframe 生成,我希望以 Excel 格式提供。

文檔給出了 .csv 文件的示例:

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

但我無法將此用例改編為 Excel 文件。 我無法以正確的格式放置 excel 文件,以便 download_button 方法接受它。 我嘗試傳遞 pd.to_excel() object 但它也沒有用。

我將不勝感激任何建議!

找到解決方案:

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')      

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM