Update tab_gallery_sorter.py

This commit is contained in:
2026-01-18 21:15:37 +01:00
parent 8777e418c6
commit 8d3ef470fa

View File

@@ -3,14 +3,15 @@ import os
from engine import SorterEngine from engine import SorterEngine
def render(quality): def render(quality):
# 1. Fetch Profile Data from DB st.subheader("🖼️ Gallery Staging Sorter")
# 1. Fetch Profile Data with Fallbacks
profiles = SorterEngine.load_profiles() profiles = SorterEngine.load_profiles()
# Use session state to find the right profile, fallback to 'Default'
active_profile = st.session_state.get('active_profile', 'Default') active_profile = st.session_state.get('active_profile', 'Default')
p_data = profiles.get(active_profile, {}) p_data = profiles.get(active_profile, {})
st.subheader("🖼️ Gallery Staging Sorter") # 2. Path Inputs (Placed at the top)
# --- PATH INPUTS (This is what was missing) ---
c1, c2 = st.columns(2) c1, c2 = st.columns(2)
path_s = c1.text_input("📁 Source Gallery Folder", path_s = c1.text_input("📁 Source Gallery Folder",
value=p_data.get("tab5_source", "/storage"), value=p_data.get("tab5_source", "/storage"),
@@ -19,9 +20,11 @@ def render(quality):
value=p_data.get("tab5_out", "/storage"), value=p_data.get("tab5_out", "/storage"),
key="t5_path_o_input") key="t5_path_o_input")
# Save paths to DB immediately if they change # Save logic moved to a button or specific condition to prevent "Infinite Refresh"
if path_s != p_data.get("tab5_source") or path_o != p_data.get("tab5_out"): if path_s != p_data.get("tab5_source") or path_o != p_data.get("tab5_out"):
if st.button("💾 Save Gallery Paths"):
SorterEngine.save_tab_paths(active_profile, t5_s=path_s, t5_o=path_o) SorterEngine.save_tab_paths(active_profile, t5_s=path_s, t5_o=path_o)
st.rerun()
# --- SETTINGS --- # --- SETTINGS ---
col_opt1, col_opt2 = st.columns(2) col_opt1, col_opt2 = st.columns(2)
@@ -30,12 +33,17 @@ def render(quality):
["Keep", "Move to Unused", "Delete"], ["Keep", "Move to Unused", "Delete"],
horizontal=True) horizontal=True)
# Safety check: if path doesn't exist, stop here but keep the UI visible
if not path_s or not os.path.exists(path_s): if not path_s or not os.path.exists(path_s):
st.warning("Please enter a valid Source Path to load the gallery.") st.warning("Waiting for a valid Source Path...")
return return
# --- SIDEBAR CATEGORIES --- # --- SIDEBAR CATEGORIES ---
cats = SorterEngine.get_categories() cats = SorterEngine.get_categories()
if not cats:
st.error("No categories found in database. Please add some in Tab 4.")
return
selected_cat = st.sidebar.radio("🏷️ Current Tag", cats, key="gallery_active_cat") selected_cat = st.sidebar.radio("🏷️ Current Tag", cats, key="gallery_active_cat")
# --- GALLERY RENDERING --- # --- GALLERY RENDERING ---
@@ -44,7 +52,8 @@ def render(quality):
st.write(f"Images: **{len(images)}** | Staged for Rename: **{len(staged)}**") st.write(f"Images: **{len(images)}** | Staged for Rename: **{len(staged)}**")
# Grid Display # Grid Display (using 4 columns)
if images:
cols = st.columns(4) cols = st.columns(4)
for idx, img_path in enumerate(images): for idx, img_path in enumerate(images):
with cols[idx % 4]: with cols[idx % 4]:
@@ -53,21 +62,34 @@ def render(quality):
# Show the new name if tagged # Show the new name if tagged
label = f"{staged[img_path]['name']}" if is_staged else os.path.basename(img_path) label = f"{staged[img_path]['name']}" if is_staged else os.path.basename(img_path)
st.image(SorterEngine.compress_for_web(img_path, quality), caption=label) # Process image through engine
img_display = SorterEngine.compress_for_web(img_path, quality)
if img_display:
st.image(img_display, caption=label)
if st.button("Tag" if not is_staged else "Untag", key=f"gal_btn_{idx}"): if st.button("Tag" if not is_staged else "Untag", key=f"gal_btn_{idx}"):
if not is_staged: if not is_staged:
ext = os.path.splitext(img_path)[1] ext = os.path.splitext(img_path)[1]
# Logic to count current category items for the suffix # Count items already staged for this category to determine suffix
cat_count = len([v for v in staged.values() if v['cat'] == selected_cat]) + 1 cat_count = len([v for v in staged.values() if v['cat'] == selected_cat]) + 1
new_name = f"{selected_cat}_{cat_count:03d}{ext}" new_name = f"{selected_cat}_{cat_count:03d}{ext}"
SorterEngine.stage_image(img_path, selected_cat, new_name) SorterEngine.stage_image(img_path, selected_cat, new_name)
else:
# Untagging logic: simply remove from the staging table
# (Requires a delete_staged_image method in your engine)
pass
st.rerun() st.rerun()
else:
st.error("Failed to load image.")
st.divider() st.divider()
# --- APPLY CHANGES ---
if st.button("🚀 APPLY ALL CHANGES TO DISK", type="primary", use_container_width=True): if st.button("🚀 APPLY ALL CHANGES TO DISK", type="primary", use_container_width=True):
if staged: if staged:
with st.spinner("Moving and renaming files..."):
SorterEngine.commit_staging(path_o, cleanup, source_root=path_s) SorterEngine.commit_staging(path_o, cleanup, source_root=path_s)
st.success("Disk sync complete!") st.success("Disk sync complete!")
st.rerun() st.rerun()
else:
st.error("Nothing is staged. Tag some images first!")