refactor: Improve directory processing UI with session state and rerun

This commit is contained in:
Bobby Abellana (aider) 2025-02-11 09:58:00 -08:00
parent 79b2bb0835
commit 6b5c851a00

View File

@ -207,118 +207,121 @@ if uploaded_files:
mime="application/zip",
)
else: # Directory Processing
st.header("Directory Processing")
else: # Directory Processing
st.header("Directory Processing")
col1, col2 = st.columns(2)
with col1:
source_dir = st.text_input("Source Directory Path",
help="Enter the full path to the directory containing files to process",
value=st.session_state.get('source_dir', ''))
source_browse = st.button("Browse Source Directory")
if source_browse:
path = get_directory_path("Select Source Directory")
if path:
st.session_state['source_dir'] = path
st.experimental_rerun()
col1, col2 = st.columns(2)
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",
value=st.session_state.get('dest_dir', ''))
dest_browse = st.button("Browse Destination Directory")
if dest_browse:
path = get_directory_path("Select Destination Directory")
if path:
st.session_state['dest_dir'] = path
st.experimental_rerun()
with col2:
if file_type == "Excel":
password_option = st.radio(
"Password Option:",
("No Password", "Password File")
)
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:
passwords = []
if password_option == "Password File":
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', ''))
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)}")
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()]
st.info(f"Loaded {len(passwords)} passwords from file")
if source_dir and dest_dir and st.button("Process Directory", type="primary"):
if not os.path.exists(source_dir):
st.error(f"Source directory does not exist: {source_dir}")
elif not os.path.exists(os.path.dirname(dest_dir)):
st.error(f"Parent of destination directory does not exist: {os.path.dirname(dest_dir)}")
else:
os.makedirs(dest_dir, exist_ok=True)
# Get all files recursively
if file_type == "Excel":
password_option = st.radio(
"Password Option:",
("No Password", "Password File")
)
passwords = []
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()]
st.info(f"Loaded {len(passwords)} passwords from file")
if source_dir and dest_dir and st.button("Process Directory", type="primary"):
if not os.path.exists(source_dir):
st.error(f"Source directory does not exist: {source_dir}")
elif not os.path.exists(os.path.dirname(dest_dir)):
st.error(f"Parent of destination directory does not exist: {os.path.dirname(dest_dir)}")
files = glob.glob(os.path.join(source_dir, "**/*.xlsx"), recursive=True)
files.extend(glob.glob(os.path.join(source_dir, "**/*.xlsm"), recursive=True))
else: # Word
files = glob.glob(os.path.join(source_dir, "**/*.docx"), recursive=True)
files.extend(glob.glob(os.path.join(source_dir, "**/*.docm"), recursive=True))
if not files:
st.warning(f"No {file_type} files found in the source directory")
else:
os.makedirs(dest_dir, exist_ok=True)
progress_bar = st.progress(0)
status_text = st.empty()
# Get all files recursively
if file_type == "Excel":
files = glob.glob(os.path.join(source_dir, "**/*.xlsx"), recursive=True)
files.extend(glob.glob(os.path.join(source_dir, "**/*.xlsm"), recursive=True))
else: # Word
files = glob.glob(os.path.join(source_dir, "**/*.docx"), recursive=True)
files.extend(glob.glob(os.path.join(source_dir, "**/*.docm"), recursive=True))
for idx, source_path in enumerate(files):
try:
# Create a container for each file
relative_path = os.path.relpath(source_path, source_dir)
dest_path = os.path.join(dest_dir, relative_path)
with st.expander(f"Processing {relative_path}", expanded=True):
st.write(f"📝 Processing {relative_path}...")
# Create destination directory if needed
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
# Process based on file type
if file_type == "Excel":
copy_excel_file(source_path, dest_path, passwords)
else: # Word
remove_all_protection_tags(source_path, dest_path)
st.success("✅ Processing complete!")
# Update progress
progress = (idx + 1) / len(files)
progress_bar.progress(progress)
status_text.text(f"Processed {idx + 1} of {len(files)} files")
except Exception as e:
st.error(f"❌ Error processing {relative_path}: {str(e)}")
if not files:
st.warning(f"No {file_type} files found in the source directory")
else:
progress_bar = st.progress(0)
status_text = st.empty()
for idx, source_path in enumerate(files):
try:
# Create a container for each file
relative_path = os.path.relpath(source_path, source_dir)
dest_path = os.path.join(dest_dir, relative_path)
with st.expander(f"Processing {relative_path}", expanded=True):
st.write(f"📝 Processing {relative_path}...")
# Create destination directory if needed
os.makedirs(os.path.dirname(dest_path), exist_ok=True)
# Process based on file type
if file_type == "Excel":
copy_excel_file(source_path, dest_path, passwords)
else: # Word
remove_all_protection_tags(source_path, dest_path)
st.success("✅ Processing complete!")
# Update progress
progress = (idx + 1) / len(files)
progress_bar.progress(progress)
status_text.text(f"Processed {idx + 1} of {len(files)} files")
except Exception as e:
st.error(f"❌ Error processing {relative_path}: {str(e)}")
progress_bar.empty()
status_text.text("✨ All processing complete!")
# Show the output directory
st.success(f"Processed files are saved in: {dest_dir}")
progress_bar.empty()
status_text.text("✨ All processing complete!")
# Show the output directory
st.success(f"Processed files are saved in: {dest_dir}")
# Footer
st.sidebar.markdown("---")