How to download local folder?

Hi community!

Can I download a local folder in streamlit app?

I make a AI images style transfer app on streamlit

I put results(processed images) in A folder

and I wanna compress the processed images to rar or zip file and let user can download it

how can I do?

Hi @sevaroy,

You could try to zip the folder programmatically then expose it through a download link :slight_smile:.

Don’t hesitate to come back with your code snippet if you have trouble implementing !

Fanilo

1 Like

@sevaroy: this post seems related, and there’s a suggestion on how to do this :slight_smile:

1 Like
 def zipdir(path, ziph):
                 # ziph is zipfile handle
                for root, dirs, files in os.walk(png_path):
                    for file in files:
                        ziph.write(os.path.join(root, file))
            zip_path = 'Python.zip'        
            zipf = zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED)
            zipdir('tmp/', zipf)
            zipf.close()

            with open(zip_path, "rb") as f:
                bytes = f.read()
                b64 = base64.b64encode(bytes).decode()
                href = f'<a href="data:file/csv;base64,{b64}">Download File</a> (right-click and save as &lt;some_name&gt;.csv)'

                st.sidebar.markdown(href, unsafe_allow_html=True)

Hi community!

now I can compress A folder , it work

but the download link not working

thanks for help

would be interested to know myself. I could not get a download link working for something that was not an excel/text file. In my case I have a binary (midi) file and in both our cases this part

 b64 = base64.b64encode(bytes).decode()
href = f'<a href="data:file/csv;base64,{b64}"> .... etc etc

is not right for what we want.

with open(zip_path, “rb”) as f:

    bytes = f.read()
    b64 = base64.b64encode(bytes).decode()
    href = f'<a href="data:file/zip;base64,{b64}" download=\'{filename}.zip\'>\
        Click to download\
    </a>'
st.sidebar.markdown(href, unsafe_allow_html=True)

Hi community

now it all work!

I figure out the data type and fix it.

thanks for help

2 Likes