Streamlit, Flask, Fastapi: How to upload zipped files to my web app and make them downloadable?

Question:

I am working on an app using streamlit to upload a zipped file and enable users to download that zipped file. For a reference example, see how online services take in a word file, convert it into pdf and make that pdf downloadable automatically.

I have been able to complete the first 2 steps. Requesting assistance with uploading the file and making it downloadable. Thank you.

Asked By: DarkFantasy

||

Answers:

This is how it is done.

import streamlit as st
# zipping the different parts of the song
def zipdir(dst, src):
    zf = zipfile.ZipFile(dst, "w", zipfile.ZIP_DEFLATED)
    abs_src = os.path.abspath(src)
    for dirname, subdirs, files in os.walk(src):
        for filename in files:
            absname = os.path.abspath(os.path.join(dirname, filename))
            arcname = absname[len(abs_src) + 1:]
            print('zipping %s as %s' % (os.path.join(dirname, filename),
                                        arcname))
            zf.write(absname, arcname)
    zf.close()

zip_path = 'Python.zip'
zipdir(zip_path, path)

# send to webapp to make downloadable link
with open(zip_path, "rb") as f:
    bytes_read = f.read()
    b64 = base64.b64encode(bytes_read).decode()
    href = f'<a href="data:file/zip;base64,{b64}" download='{title}.zip'>
            Click to download
        </a>'
st.sidebar.markdown(href, unsafe_allow_html=True)
Answered By: DarkFantasy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.