From ac5306ed30f701778fa699ae88b60295188d26b2 Mon Sep 17 00:00:00 2001 From: "Bobby Abellana (aider)" Date: Tue, 11 Feb 2025 10:08:38 -0800 Subject: [PATCH] feat: Add cross-platform file path selection for password files --- src/streamlit_app.py | 55 +++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/src/streamlit_app.py b/src/streamlit_app.py index f1824b7..5edb9dc 100644 --- a/src/streamlit_app.py +++ b/src/streamlit_app.py @@ -33,6 +33,43 @@ def save_uploaded_file(uploaded_file): tmp_file.write(uploaded_file.getvalue()) return tmp_file.name +def get_file_path(title="Select File", file_types=[("Text Files", "*.txt")]): + """Get file path using native file dialog when possible""" + try: + if platform.system() == "Windows": + import tkinter as tk + from tkinter import filedialog + root = tk.Tk() + root.withdraw() # Hide the main window + root.attributes('-topmost', True) # Bring dialog to front + path = filedialog.askopenfilename(title=title, filetypes=file_types) + return path if path else None + elif platform.system() == "Linux": + # Use zenity for Linux systems + try: + result = subprocess.run( + ['zenity', '--file-selection', '--title', title], + capture_output=True, + text=True + ) + return result.stdout.strip() if result.returncode == 0 else None + except FileNotFoundError: + return None + elif platform.system() == "Darwin": # macOS + try: + result = subprocess.run( + ['osascript', '-e', f'choose file with prompt "{title}"'], + capture_output=True, + text=True + ) + return result.stdout.strip() if result.returncode == 0 else None + except FileNotFoundError: + return None + except Exception as e: + st.error(f"Error opening file dialog: {str(e)}") + return None + return None + def get_directory_path(title="Select Directory"): """Get directory path using native file dialog when possible""" try: @@ -157,20 +194,10 @@ with col2: value=st.session_state.get('password_path', '')) password_browse = st.button("Browse Password File") if password_browse: - try: - import tkinter as tk - from tkinter import filedialog - root = tk.Tk() - root.withdraw() - file_path = filedialog.askopenfilename( - title="Select Password File", - filetypes=[("Text Files", "*.txt")] - ) - if file_path: - st.session_state['password_path'] = file_path - st.experimental_rerun() - except Exception as e: - st.error(f"Error opening file dialog: {str(e)}") + path = get_file_path("Select Password File", [("Text Files", "*.txt")]) + if path: + st.session_state['password_path'] = path + st.experimental_rerun() if password_path and os.path.exists(password_path): with open(password_path, 'r', encoding='utf-8') as pf: