feat: Add environment-aware directory and file browsing support

This commit is contained in:
Bobby Abellana (aider) 2025-02-11 11:12:12 -08:00
parent 336541fcae
commit c224424e04

View File

@ -19,6 +19,14 @@ from main import (
setup_logging setup_logging
) )
def is_running_locally():
"""Check if the app is running locally or in cloud environment"""
try:
import tkinter
return True
except ImportError:
return False
# Configure page # Configure page
st.set_page_config( st.set_page_config(
page_title="Office Protection Remover", page_title="Office Protection Remover",
@ -166,19 +174,20 @@ 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")
if st.button("Browse Source Directory", key='source_browse'): if is_running_locally():
try: if st.button("Browse Source Directory", key='source_browse'):
root = tk.Tk() try:
root.withdraw() root = tk.Tk()
root.wm_attributes('-topmost', 1) root.withdraw()
path = filedialog.askdirectory(title="Select Source Directory") root.wm_attributes('-topmost', 1)
root.destroy() path = filedialog.askdirectory(title="Select Source Directory")
if path: root.destroy()
st.session_state['source_dir'] = path if path:
st.session_state['source_dir_input'] = path st.session_state['source_dir'] = path
st.experimental_rerun() st.session_state['source_dir_input'] = path
except Exception as e: st.experimental_rerun()
st.error(f"Error opening directory dialog: {str(e)}") 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",
@ -186,19 +195,20 @@ 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")
if st.button("Browse Destination Directory", key='dest_browse'): if is_running_locally():
try: if st.button("Browse Destination Directory", key='dest_browse'):
root = tk.Tk() try:
root.withdraw() root = tk.Tk()
root.wm_attributes('-topmost', 1) root.withdraw()
path = filedialog.askdirectory(title="Select Destination Directory") root.wm_attributes('-topmost', 1)
root.destroy() path = filedialog.askdirectory(title="Select Destination Directory")
if path: root.destroy()
st.session_state['dest_dir'] = path if path:
st.session_state['dest_dir_input'] = path st.session_state['dest_dir'] = path
st.experimental_rerun() st.session_state['dest_dir_input'] = path
except Exception as e: st.experimental_rerun()
st.error(f"Error opening directory dialog: {str(e)}") 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":
@ -223,12 +233,20 @@ 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', ''))
password_browse = st.button("Browse Password File") if is_running_locally():
if password_browse: if st.button("Browse Password File"):
path = get_file_path("Select Password File", [("Text Files", "*.txt")]) try:
if path: root = tk.Tk()
st.session_state['password_path'] = path root.withdraw()
st.experimental_rerun() root.wm_attributes('-topmost', 1)
path = filedialog.askopenfilename(title="Select Password File",
filetypes=[("Text Files", "*.txt")])
root.destroy()
if path:
st.session_state['password_path'] = 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): if 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: