From 336541fcae0ad684e2a8cd36098def8ddf9cf882 Mon Sep 17 00:00:00 2001 From: "Bobby Abellana (aider)" Date: Tue, 11 Feb 2025 11:10:54 -0800 Subject: [PATCH] feat: Add tkinter directory browsing with error handling in Streamlit app --- src/streamlit_app.py | 57 ++++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/src/streamlit_app.py b/src/streamlit_app.py index 65e9d1b..caa6e83 100644 --- a/src/streamlit_app.py +++ b/src/streamlit_app.py @@ -4,8 +4,9 @@ import logging import warnings import shutil from io import BytesIO -from io import BytesIO import tempfile +import tkinter as tk +from tkinter import filedialog import zipfile from pathlib import Path import glob @@ -161,29 +162,43 @@ with col1: else: # Select Directory # Source Directory source_dir = st.text_input("Source Directory Path", - value=st.session_state.get('source_dir', ''), - key='source_dir_input', - help="Enter the full path to the directory containing files to process") - source_browse = st.button("Browse Source Directory") - if source_browse: - path = get_directory_path("Select Source Directory") - if path: - st.session_state['source_dir'] = path - st.session_state['source_dir_input'] = path - st.experimental_rerun() + value=st.session_state.get('source_dir', ''), + key='source_dir_input', + help="Enter the full path to the directory containing files to process") + + if st.button("Browse Source Directory", key='source_browse'): + try: + root = tk.Tk() + root.withdraw() + root.wm_attributes('-topmost', 1) + path = filedialog.askdirectory(title="Select Source Directory") + root.destroy() + if path: + st.session_state['source_dir'] = path + st.session_state['source_dir_input'] = path + st.experimental_rerun() + except Exception as e: + st.error(f"Error opening directory dialog: {str(e)}") # Destination Directory dest_dir = st.text_input("Destination Directory Path", - value=st.session_state.get('dest_dir', ''), - key='dest_dir_input', - help="Enter the full path where processed files will be saved") - dest_browse = st.button("Browse Destination Directory") - if dest_browse: - path = get_directory_path("Select Destination Directory") - if path: - st.session_state['dest_dir'] = path - st.session_state['dest_dir_input'] = path - st.experimental_rerun() + value=st.session_state.get('dest_dir', ''), + key='dest_dir_input', + help="Enter the full path where processed files will be saved") + + if st.button("Browse Destination Directory", key='dest_browse'): + try: + root = tk.Tk() + root.withdraw() + root.wm_attributes('-topmost', 1) + path = filedialog.askdirectory(title="Select Destination Directory") + root.destroy() + if path: + st.session_state['dest_dir'] = path + st.session_state['dest_dir_input'] = path + st.experimental_rerun() + except Exception as e: + st.error(f"Error opening directory dialog: {str(e)}") with col2: if file_type == "Excel":