feat: Add native file dialog support for directory and file selection

This commit is contained in:
Bobby Abellana (aider) 2025-02-11 09:54:08 -08:00
parent a0a90a8b9c
commit 882d6dff46
2 changed files with 69 additions and 0 deletions

View File

@ -2,3 +2,4 @@ openpyxl>=3.0.0
msoffcrypto-tool>=5.0.0
tqdm>=4.65.0
streamlit>=1.24.0
tkinter

View File

@ -7,6 +7,8 @@ import tempfile
import zipfile
from pathlib import Path
import glob
import platform
import subprocess
from main import (
load_workbook_with_possible_passwords,
copy_excel_file,
@ -31,6 +33,43 @@ def save_uploaded_file(uploaded_file):
tmp_file.write(uploaded_file.getvalue())
return tmp_file.name
def get_directory_path(title="Select Directory"):
"""Get directory 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.askdirectory(title=title)
return path if path else None
elif platform.system() == "Linux":
# Use zenity for Linux systems
try:
result = subprocess.run(
['zenity', '--file-selection', '--directory', '--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 folder 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 create_zip_file(files_dict):
"""Create a zip file containing all processed files"""
zip_buffer = BytesIO()
@ -176,9 +215,21 @@ if uploaded_files:
with col1:
source_dir = st.text_input("Source Directory Path",
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:
source_dir = path
st.session_state['source_dir'] = path # Persist the selection
dest_dir = st.text_input("Destination Directory Path",
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:
dest_dir = path
st.session_state['dest_dir'] = path # Persist the selection
with col2:
if file_type == "Excel":
@ -191,6 +242,23 @@ if uploaded_files:
if password_option == "Password File":
password_path = st.text_input("Password File Path",
help="Enter the full path to the text file containing passwords")
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:
password_path = file_path
st.session_state['password_path'] = file_path
except Exception as e:
st.error(f"Error opening file dialog: {str(e)}")
if password_path and os.path.exists(password_path):
with open(password_path, 'r', encoding='utf-8') as pf:
passwords = [line.strip() for line in pf if line.strip()]