refactor: Replace tkinter file dialogs with Streamlit file uploaders

This commit is contained in:
Bobby Abellana (aider) 2025-02-11 11:19:44 -08:00
parent 3e40b604bc
commit d6decf1382

View File

@ -10,14 +10,6 @@ from pathlib import Path
import glob import glob
import platform import platform
import subprocess import subprocess
# Conditionally import tkinter
try:
import tkinter as tk
from tkinter import filedialog
TKINTER_AVAILABLE = True
except ImportError:
TKINTER_AVAILABLE = False
from main import ( from main import (
load_workbook_with_possible_passwords, load_workbook_with_possible_passwords,
copy_excel_file, copy_excel_file,
@ -176,21 +168,14 @@ 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")
col1_browse1, col1_browse2 = st.columns([1, 4]) # Use a file uploader as directory selector
with col1_browse1: source_dir_files = st.file_uploader("Or select any file from the source directory",
if st.button("Browse", key='source_browse'): key='source_dir_selector',
try: help="Select any file from the directory you want to process")
import tkinter as tk if source_dir_files:
from tkinter import filedialog dir_path = os.path.dirname(source_dir_files.name)
root = tk.Tk() st.session_state['source_dir'] = dir_path
root.withdraw() st.session_state['source_dir_input'] = dir_path
path = filedialog.askdirectory(title="Select Source Directory")
if path:
st.session_state['source_dir'] = path
st.session_state['source_dir_input'] = path
root.destroy()
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",
@ -198,21 +183,14 @@ 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")
col2_browse1, col2_browse2 = st.columns([1, 4]) # Use a file uploader as directory selector
with col2_browse1: dest_dir_files = st.file_uploader("Or select any file from the destination directory",
if st.button("Browse", key='dest_browse'): key='dest_dir_selector',
try: help="Select any file from the directory where you want to save processed files")
import tkinter as tk if dest_dir_files:
from tkinter import filedialog dir_path = os.path.dirname(dest_dir_files.name)
root = tk.Tk() st.session_state['dest_dir'] = dir_path
root.withdraw() st.session_state['dest_dir_input'] = dir_path
path = filedialog.askdirectory(title="Select Destination Directory")
if path:
st.session_state['dest_dir'] = path
st.session_state['dest_dir_input'] = path
root.destroy()
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":
@ -237,25 +215,17 @@ with col2:
password_path = st.text_input("Password File Path", password_path = st.text_input("Password File Path",
help="Enter the full path to the text file containing passwords", help="Enter the full path to the text file containing passwords",
value=st.session_state.get('password_path', '')) value=st.session_state.get('password_path', ''))
col3_browse1, col3_browse2 = st.columns([1, 4])
with col3_browse1:
if st.button("Browse", key='password_browse'):
try:
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
path = filedialog.askopenfilename(
title="Select Password File",
filetypes=[("Text Files", "*.txt")]
)
if path:
st.session_state['password_path'] = path
root.destroy()
except Exception as e:
st.error(f"Error opening file dialog: {str(e)}")
if password_path and os.path.exists(password_path): # Use direct file uploader for password file
password_file_upload = st.file_uploader("Or upload password file",
type=["txt"],
key='password_file_selector',
help="Upload a text file containing passwords")
if password_file_upload:
content = password_file_upload.getvalue().decode()
passwords = [line.strip() for line in content.splitlines() if line.strip()]
st.info(f"Loaded {len(passwords)} passwords")
elif password_path and os.path.exists(password_path):
with open(password_path, 'r', encoding='utf-8') as pf: with open(password_path, 'r', encoding='utf-8') as pf:
passwords = [line.strip() for line in pf if line.strip()] passwords = [line.strip() for line in pf if line.strip()]
st.info(f"Loaded {len(passwords)} passwords from file") st.info(f"Loaded {len(passwords)} passwords from file")