feat: Add tkinter directory browsing with error handling in Streamlit app

This commit is contained in:
Bobby Abellana (aider) 2025-02-11 11:10:54 -08:00
parent c4e107b40e
commit 336541fcae

View File

@ -4,8 +4,9 @@ import logging
import warnings import warnings
import shutil import shutil
from io import BytesIO from io import BytesIO
from io import BytesIO
import tempfile import tempfile
import tkinter as tk
from tkinter import filedialog
import zipfile import zipfile
from pathlib import Path from pathlib import Path
import glob import glob
@ -161,29 +162,43 @@ with col1:
else: # Select Directory else: # Select Directory
# Source Directory # Source Directory
source_dir = st.text_input("Source Directory Path", source_dir = st.text_input("Source Directory Path",
value=st.session_state.get('source_dir', ''), value=st.session_state.get('source_dir', ''),
key='source_dir_input', key='source_dir_input',
help="Enter the full path to the directory containing files to process") help="Enter the full path to the directory containing files to process")
source_browse = st.button("Browse Source Directory")
if source_browse: if st.button("Browse Source Directory", key='source_browse'):
path = get_directory_path("Select Source Directory") try:
if path: root = tk.Tk()
st.session_state['source_dir'] = path root.withdraw()
st.session_state['source_dir_input'] = path root.wm_attributes('-topmost', 1)
st.experimental_rerun() 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 # Destination Directory
dest_dir = st.text_input("Destination Directory Path", dest_dir = st.text_input("Destination Directory Path",
value=st.session_state.get('dest_dir', ''), value=st.session_state.get('dest_dir', ''),
key='dest_dir_input', key='dest_dir_input',
help="Enter the full path where processed files will be saved") help="Enter the full path where processed files will be saved")
dest_browse = st.button("Browse Destination Directory")
if dest_browse: if st.button("Browse Destination Directory", key='dest_browse'):
path = get_directory_path("Select Destination Directory") try:
if path: root = tk.Tk()
st.session_state['dest_dir'] = path root.withdraw()
st.session_state['dest_dir_input'] = path root.wm_attributes('-topmost', 1)
st.experimental_rerun() 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: with col2:
if file_type == "Excel": if file_type == "Excel":