From bb94cd4739c5df22f9b03ab85bd46a5e4ac40b0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20David=20M=C3=BCller?= Date: Tue, 2 Jul 2024 15:39:54 +0200 Subject: [PATCH 1/2] add download page --- pages/16_Download_Section.py | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 pages/16_Download_Section.py diff --git a/pages/16_Download_Section.py b/pages/16_Download_Section.py new file mode 100644 index 000000000..073ca6300 --- /dev/null +++ b/pages/16_Download_Section.py @@ -0,0 +1,64 @@ +import streamlit as st + +from os.path import join, exists, isfile, basename +from os import listdir + +from src.common import page_setup +from io import StringIO, BytesIO +from zipfile import ZipFile, ZIP_DEFLATED + +# Define output folder here; all subfolders will be handled as downloadable +# directories +output_folder = 'your_folder_goes_here' + +def content(): + page_setup() + + dirpath = join(st.session_state["workspace"], output_folder) + + if exists(dirpath): + directories = sorted( + [entry for entry in listdir(dirpath) if not isfile(entry)] + ) + else: + directories = [] + + if len(directories) == 0: + st.error('No results to show yet. Please run a workflow first!') + return + + # Table Header + columns = st.columns((0.4, 0.6)) + columns[0].write('**Run**') + columns[1].write('**Download**') + + # Table Body + for i, directory in enumerate(directories): + st.divider() + columns = st.columns((0.4, 0.6)) + columns[0].empty().write(directory) + + with columns[1]: + button_placeholder = st.empty() + + clicked = button_placeholder.button('Prepare Download', key=i) + if clicked: + button_placeholder.empty() + with st.spinner(): + out_zip = join(dirpath, directory, 'output.zip') + if not exists(out_zip): + with ZipFile(out_zip, 'w', ZIP_DEFLATED) as zip_file: + for output in listdir(join(dirpath, directory)): + try: + with open(join(dirpath, directory, output), 'r') as f: + zip_file.writestr(basename(output), f.read()) + except: + continue + with open(out_zip, 'rb') as f: + button_placeholder.download_button( + "Download ⬇️", f, + file_name = f'{directory}.zip' + ) + +if __name__ == "__main__": + content() From 89d88e1ed67590f270b70e28e001ad4bedbbee33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=20David=20M=C3=BCller?= Date: Tue, 2 Jul 2024 15:55:15 +0200 Subject: [PATCH 2/2] comments --- pages/16_Download_Section.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pages/16_Download_Section.py b/pages/16_Download_Section.py index 073ca6300..ea9de97c8 100644 --- a/pages/16_Download_Section.py +++ b/pages/16_Download_Section.py @@ -14,8 +14,10 @@ def content(): page_setup() + # Generate full path dirpath = join(st.session_state["workspace"], output_folder) - + + # Detect downloadable content if exists(dirpath): directories = sorted( [entry for entry in listdir(dirpath) if not isfile(entry)] @@ -23,6 +25,7 @@ def content(): else: directories = [] + # Show error if no content is available for download if len(directories) == 0: st.error('No results to show yet. Please run a workflow first!') return @@ -41,10 +44,12 @@ def content(): with columns[1]: button_placeholder = st.empty() + # Show placeholder button before download is prepared clicked = button_placeholder.button('Prepare Download', key=i) if clicked: button_placeholder.empty() with st.spinner(): + # Create ZIP file out_zip = join(dirpath, directory, 'output.zip') if not exists(out_zip): with ZipFile(out_zip, 'w', ZIP_DEFLATED) as zip_file: @@ -54,6 +59,7 @@ def content(): zip_file.writestr(basename(output), f.read()) except: continue + # Show download button after ZIP file was created with open(out_zip, 'rb') as f: button_placeholder.download_button( "Download ⬇️", f,