简体   繁体   English

在streamlit中下载多个文件

[英]Download multiple files in streamlit

I am writing a program to concate and modify some CSV files.我正在编写一个程序来连接和修改一些 CSV 文件。 My code returns 5 or fewer DataFrames to me.我的代码向我返回 5 个或更少的 DataFrame。 I want to allow users to download all these DataFrames as CSV files, but st.download_button allows me to download only one.我想允许用户将所有这些 DataFrames 下载为 CSV 文件,但st.download_button只允许我下载一个。 I Try to make just a few buttons but when I click the first rest just disappears.我尝试只制作几个按钮,但是当我单击第一个 rest 时,它就消失了。 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 ?有没有办法使用st.download_button下载多个文件?

This is not possible with one download button afaik.一个下载按钮 afaik 无法做到这一点。 A workaround would be to zip the selected files and use the zip file for the download.一种解决方法是 zip 选择文件并使用 zip 文件进行下载。

It is not the download button , the problem is from if st.button("Rozpocznij proces"):不是download button问题出在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. Streamlit 按钮没有回调,因此st.button()下的任何用户条目将始终重新运行应用程序,因此您最终会丢失数据,在这种情况下,您可以替换if st.button("Rozpocznij proces"):复选框类似if st.checkbox("Rozpocznij proces"):你可以使用 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.我还建议您创建一个 function 以及您的下载按钮,这将使您的工作更好,而不是一遍又一遍地重复几乎相同的代码。

But before we do that lets first rename these variables so we can have a clear understanding when entering the function arguments.但在我们这样做之前,让我们先重命名这些变量,以便我们在输入 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.现在让我们构建我们的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.每当您调用generate_download_button时,它都会生成一个新的下载按钮。

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.由于st.download_button (还)不允许下载多个文件,您可以尝试 zip 您的所有数据帧/csv,以便用户一次下载它们。

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 .您可能需要查看这篇文章,该文章解释了如何在Django中执行此操作,我认为在Streamlit中执行此操作要简单得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM