From 47f4287470c1c996844b40402f91675966281862 Mon Sep 17 00:00:00 2001 From: "Bobby Abellana (aider)" Date: Tue, 11 Feb 2025 11:21:43 -0800 Subject: [PATCH] feat: Enhance directory selection with native dialogs for cross-platform support --- src/streamlit_app.py | 70 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 54 insertions(+), 16 deletions(-) diff --git a/src/streamlit_app.py b/src/streamlit_app.py index 2ad50ac..47b9487 100644 --- a/src/streamlit_app.py +++ b/src/streamlit_app.py @@ -168,14 +168,33 @@ with col1: key='source_dir_input', help="Enter the full path to the directory containing files to process") - # Use a file uploader as directory selector - source_dir_files = st.file_uploader("Or select any file from the source directory", - key='source_dir_selector', - help="Select any file from the directory you want to process") - if source_dir_files: - dir_path = os.path.dirname(source_dir_files.name) - st.session_state['source_dir'] = dir_path - st.session_state['source_dir_input'] = dir_path + if st.button("Select Source Directory", key='source_browse'): + try: + import subprocess + if platform.system() == "Linux": + result = subprocess.run( + ['zenity', '--file-selection', '--directory'], + capture_output=True, + text=True + ) + if result.returncode == 0: + path = result.stdout.strip() + st.session_state['source_dir'] = path + st.session_state['source_dir_input'] = path + st.experimental_rerun() + else: # Windows or MacOS + import tkinter as tk + from tkinter import filedialog + root = tk.Tk() + root.withdraw() + path = filedialog.askdirectory(title="Select Source Directory") + if path: + st.session_state['source_dir'] = path + st.session_state['source_dir_input'] = path + st.experimental_rerun() + root.destroy() + except Exception as e: + st.error(f"Error selecting directory: {str(e)}") # Destination Directory dest_dir = st.text_input("Destination Directory Path", @@ -183,14 +202,33 @@ with col1: key='dest_dir_input', help="Enter the full path where processed files will be saved") - # Use a file uploader as directory selector - dest_dir_files = st.file_uploader("Or select any file from the destination directory", - key='dest_dir_selector', - help="Select any file from the directory where you want to save processed files") - if dest_dir_files: - dir_path = os.path.dirname(dest_dir_files.name) - st.session_state['dest_dir'] = dir_path - st.session_state['dest_dir_input'] = dir_path + if st.button("Select Destination Directory", key='dest_browse'): + try: + import subprocess + if platform.system() == "Linux": + result = subprocess.run( + ['zenity', '--file-selection', '--directory'], + capture_output=True, + text=True + ) + if result.returncode == 0: + path = result.stdout.strip() + st.session_state['dest_dir'] = path + st.session_state['dest_dir_input'] = path + st.experimental_rerun() + else: # Windows or MacOS + import tkinter as tk + from tkinter import filedialog + root = tk.Tk() + root.withdraw() + path = filedialog.askdirectory(title="Select Destination Directory") + if path: + st.session_state['dest_dir'] = path + st.session_state['dest_dir_input'] = path + st.experimental_rerun() + root.destroy() + except Exception as e: + st.error(f"Error selecting directory: {str(e)}") with col2: if file_type == "Excel":