refactor: Replace Linux-specific directory selection with cross-platform tkinter dialog

This commit is contained in:
Bobby Abellana (aider) 2025-02-11 11:23:52 -08:00
parent 47f4287470
commit 800ae3b262

View File

@ -168,31 +168,19 @@ with col1:
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")
if st.button("Select Source Directory", key='source_browse'): if st.button("Browse Source Directory", key='source_browse'):
try: try:
import subprocess import tkinter as tk
if platform.system() == "Linux": from tkinter import filedialog
result = subprocess.run( root = tk.Tk()
['zenity', '--file-selection', '--directory'], root.withdraw()
capture_output=True, root.wm_attributes('-topmost', 1)
text=True path = filedialog.askdirectory(title="Select Source Directory")
) if path:
if result.returncode == 0: st.session_state['source_dir'] = path
path = result.stdout.strip() st.session_state['source_dir_input'] = path
st.session_state['source_dir'] = path st.experimental_rerun()
st.session_state['source_dir_input'] = path root.destroy()
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: except Exception as e:
st.error(f"Error selecting directory: {str(e)}") st.error(f"Error selecting directory: {str(e)}")
@ -202,31 +190,19 @@ with col1:
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")
if st.button("Select Destination Directory", key='dest_browse'): if st.button("Browse Destination Directory", key='dest_browse'):
try: try:
import subprocess import tkinter as tk
if platform.system() == "Linux": from tkinter import filedialog
result = subprocess.run( root = tk.Tk()
['zenity', '--file-selection', '--directory'], root.withdraw()
capture_output=True, root.wm_attributes('-topmost', 1)
text=True path = filedialog.askdirectory(title="Select Destination Directory")
) if path:
if result.returncode == 0: st.session_state['dest_dir'] = path
path = result.stdout.strip() st.session_state['dest_dir_input'] = path
st.session_state['dest_dir'] = path st.experimental_rerun()
st.session_state['dest_dir_input'] = path root.destroy()
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: except Exception as e:
st.error(f"Error selecting directory: {str(e)}") st.error(f"Error selecting directory: {str(e)}")