简体   繁体   English

如何在 Streamlit 中下载多个文件

[英]How to download more than one file in Streamlit

I need to make a download button for more than one file.我需要为多个文件制作一个下载按钮。 Streamlit's download button doesn't let you download more than one file. Streamlit 的下载按钮不允许您下载多个文件。 I tried to make a few buttons, but the rest just disappear when I click the first one.我试着做几个按钮,但是当我点击第一个按钮时 rest 就消失了。 Is there any way to download two or more files in Streamlit?有什么方法可以在 Streamlit 中下载两个或更多文件?

I tried this solution from Github, this is what the code looks like:我从 Github 尝试了这个解决方案,代码如下所示:

if st.button("Rozpocznij proces"):
    raport2 = Raport.raport_naj_10(gender,year,week,engine)
    raportM = raport2[0]
    raportO = raport2[1]
    st.dataframe(raportM)
    st.dataframe(raportO)
    zipObj = ZipFile("sample.zip", "w")
    # Add multiple files to the zip
    zipObj.write("raportM")
    zipObj.write("raportO")
    # close the Zip File
    zipObj.close()

    ZipfileDotZip = "sample.zip"

    with open(ZipfileDotZip, "rb") as f:
        bytes = f.read()
        b64 = base64.b64encode(bytes).decode()
        href = f"<a href=\"data:file/zip;base64,{b64}\" download='{ZipfileDotZip}.zip'>\
            Click last model weights\
        </a>"
    st.sidebar.markdown(href, unsafe_allow_html=True)

But I get this error:但我得到这个错误:

FileNotFoundError: [WinError 2] Nie można odnaleźć określonego pliku: 'raportM'

It says that can't find the file named "raportM".它说找不到名为“raportM”的文件。

You are having those errors because the code is written with an assumption that you already have the files stored and you want to generate a zip file for them.您遇到这些错误是因为代码是在假设您已经存储了文件并且您希望为它们生成 zip 文件的情况下编写的。 zipObj.write("raportM") is looking for the file named "raportM" and there isn't any, because in your case you do not have these files stored. zipObj.write("raportM")正在寻找名为“raportM”的文件,但没有,因为在您的情况下,您没有存储这些文件。 I can see that you are passing variable names as files and that is not going to work.我可以看到您将变量名作为文件传递,但这是行不通的。

What you will have to do is to save those variable names CSV files in your local machine before doing the above operations.在执行上述操作之前,您需要做的是在本地机器中保存这些变量名 CSV 文件。

In this case lets modify your code.在这种情况下,让我们修改您的代码。 But before that we need to initialize a session state for the button st.button("Rozpocznij proces") because streamlit button have no callbacks .但在此之前,我们需要为按钮st.button("Rozpocznij proces")初始化 session state 因为流光按钮没有回调

processbtn = st.button("Rozpocznij proces")
# Initialized session states
if "processbtn_state" not in st.session_state:
    st.session_state.processbtn_state = False

if processbtn or st.session_state.processbtn_state:
    st.session_state.processbtn_state = True

    raport2 = Raport.raport_naj_10(gender,year,week,engine)
    raportM = raport2[0]
    raportO = raport2[1]
    st.dataframe(raportM)
    st.dataframe(raportO)

    # Save files
    raportM.to_csv('raportM.csv') # You can specify a directory where you want
    raportO.to_csv('raportO.csv') # these files to be stored

    # Create a zip folder
    zipObj = ZipFile("sample.zip", "w")

    # Add multiple files to the zip
    zipObj.write("raportM.csv")
    zipObj.write("raportO.csv")
    # close the Zip File
    zipObj.close()

    ZipfileDotZip = "sample.zip"

    with open(ZipfileDotZip, "rb") as f:
        bytes = f.read()
        b64 = base64.b64encode(bytes).decode()
        href = f"<a href=\"data:file/zip;base64,{b64}\" download='{ZipfileDotZip}.zip'>\
            Click last model weights\
        </a>"
    st.sidebar.markdown(href, unsafe_allow_html=True)

At this moment, when you pay close attention to your directories you will find 'raportM.csv' and 'raportO.csv' files.此时,当您密切注意您的目录时,您会发现'raportM.csv''raportO.csv'文件。 You can pass a condition to the download button so that whenever a download is made the files should be deleted in case you don't want to keep them.您可以将条件传递给下载按钮,以便在下载时删除文件,以防您不想保留它们。

Note: You may encounter fileNotFound Error but does not mean that it won't work, you will just need to know where you are saving the files.注意:您可能会遇到fileNotFound 错误,但这并不意味着它不起作用,您只需要知道保存文件的位置。

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

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