Hi Community,
I am looking forward to achieve a conditional execution only if the data is downloaded. I am moving data from folder A to folder B (OutputToBeZipped), then archiving the folder B in a location C (ZippedOutputs), and downloading the zipped folder from C. I want to delete the folder B with shutil.rmtree(path), but this should only be executed once the download_button is clicked. Here, if statement is not working with download_button. As mentioned in an earlier post, I have been unsuccessful to download with st.button previously. My effort so far:
import streamlit as st
import shutil
import os
import zipfile
if st.button("Initiate download"):
folder_to_be_zipped = "OutputToBeZipped"
zipped_file_name = r"ZippedOutputs\output.zip"
# moving the output data to the folder to be zipped
src = r"C:\Users\....\source"
dest = "C:\\Users\\....\\OutputToBeZipped"
shutil.copytree(src, dest, dirs_exist_ok=True)
# creating zip of the folder
with zipfile.ZipFile(zipped_file_name, "w", zipfile.ZIP_DEFLATED) as newzip:
for dirpath, dirnames, files in os.walk(folder_to_be_zipped):
for file in files:
newzip.write(os.path.join(dirpath, file))
# byte code of the zip file
with open(zipped_file_name, "rb") as zip_file:
ZIPbyte = zip_file.read()
# download the byte code as a zipped file
st.download_button(label="Download Output",
data=ZIPbyte,
file_name="testZIP.zip",
mime='application/zip')
# Delete the folder OutputToBeZipped
path = r"C:\Users\.....\OutputToBeZipped"
shutil.rmtree(path)
Any suggestion is appreciated. Please help