I am writing a program to concate and modify some CSV files. My code returns 5 or fewer DataFrames to me. I want to allow users to download all these DataFrames as CSV files, but st.download_button
allows me to download only one. I Try to make just a few buttons but when I click the first rest just disappears.
This is my code :
uploaded_files = st.file_uploader("Proszę wybrać pliki csv", accept_multiple_files=True)
if st.button("Rozpocznij proces"):
data1 = structure.create_structure(uploaded_files)
@st.cache
def convert_df(df):
# IMPORTANT: Cache the conversion to prevent computation on every rerun
return df.to_csv().encode('utf-8')
mlode = convert_df(data1[0])
lochy = convert_df(data1[1])
knury = convert_df(data1[2])
rozplodowa = convert_df(data1[3])
mag = convert_df(data1[4])
#choices = st.multiselect("Download",["mlode","lochy","knury","rozplodowa"])
razem = ['mlode','lochy','knury','rozplodowa','mag']
st.download_button(label="Download mlode as CSV",
data = mlode,
file_name="mlode.csv")
st.download_button(label="Download lochy as CSV",
data=lochy,
file_name="lochy.csv")
st.download_button(label="Download lochy as CSV",
data=knury,
file_name="knury.csv")
st.download_button(label="Download lochy as CSV",
data=rozplodowa,
file_name="rozplodowa.csv")
st.download_button(label="Download lochy as CSV",
data=mag,
file_name="mag.csv")
Is there any way to download multiple files using st.download_button
?
CodePudding user response:
This is not possible with one download button afaik. A workaround would be to zip the selected files and use the zip file for the download.
CodePudding user response:
Since st.download_button
doesn't allow (yet) to download multiple files, you can try to zip all your dataframes/csvs so can the users download them at once.
You may need to take a look at this article that explains how to do that in Django and I think it's much more simpler to do it in Streamlit.
CodePudding user response:
It is not the download button
, the problem is from if st.button("Rozpocznij proces"):
Streamlit button has no callbacks so any user entry under a st.button()
will always rerun the app so you end up losing the data, in that case you can replace if st.button("Rozpocznij proces"):
with a checkbox like if st.checkbox("Rozpocznij proces"):
and you will be fine to go.
Change
if st.button("Rozpocznij proces"):
To
if st.checkbox("Rozpocznij proces"):
I also recommend you create a function as well for your download button which will make your work much better instead of repeating almost same code over and over again.
But before we do that lets first rename these variables so we can have a clear understanding when entering the function arguments.
mlode_csv = convert_df(data1[0]) # I added csv to the variable names
lochy_csv = convert_df(data1[1])
knury_csv = convert_df(data1[2])
rozplodowa_csv = convert_df(data1[3])
mag_csv = convert_df(data1[4])
Now lets construct our download_button
function.
import streamlit as st
import csv
def generate_download_button(csv_data, filename, file_label):
st.download_button(label=f"Download {file_label} as CSV",
data=csv_data,
file_name=f"{filename}.csv")
generate_download_button(csv_data=mlode_csv, filename="mlode", file_label="mlode")
generate_download_button(csv_data=lochy_csv, filename="lochy", file_label="lochy")
generate_download_button(csv_data=knury_csv, filename="knury", file_label="knury")
generate_download_button(csv_data=rozplodowa_csv, filename="rozplodowa", file_label="rozplodowa")
generate_download_button(csv_data=mag_csv, filename="mag", file_label="mag")
Whenever you call generate_download_button
it will generate a new download button.