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 platform
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 (
load_workbook_with_possible_passwords,
copy_excel_file,
@ -176,21 +168,14 @@ with col1:
key='source_dir_input',
help="Enter the full path to the directory containing files to process")
col1_browse1, col1_browse2 = st.columns([1, 4])
with col1_browse1:
if st.button("Browse", key='source_browse'):
try:
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
root.destroy()
except Exception as e:
st.error(f"Error opening directory dialog: {str(e)}")
# Use a file uploader as directory selector
source_dir_files = st.file_uploader("Or select any file from the source directory",
key='source_dir_selector',
help="Select any file from the directory you want to process")
if source_dir_files:
dir_path = os.path.dirname(source_dir_files.name)
st.session_state['source_dir'] = dir_path
st.session_state['source_dir_input'] = dir_path
# Destination Directory
dest_dir = st.text_input("Destination Directory Path",
@ -198,21 +183,14 @@ with col1:
key='dest_dir_input',
help="Enter the full path where processed files will be saved")
col2_browse1, col2_browse2 = st.columns([1, 4])
with col2_browse1:
if st.button("Browse", key='dest_browse'):
try:
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
root.destroy()
except Exception as e:
st.error(f"Error opening directory dialog: {str(e)}")
# Use a file uploader as directory selector
dest_dir_files = st.file_uploader("Or select any file from the destination directory",
key='dest_dir_selector',
help="Select any file from the directory where you want to save processed files")
if dest_dir_files:
dir_path = os.path.dirname(dest_dir_files.name)
st.session_state['dest_dir'] = dir_path
st.session_state['dest_dir_input'] = dir_path
with col2:
if file_type == "Excel":
@ -237,25 +215,17 @@ with col2:
password_path = st.text_input("Password File Path",
help="Enter the full path to the text file containing passwords",
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:
passwords = [line.strip() for line in pf if line.strip()]
st.info(f"Loaded {len(passwords)} passwords from file")