Update tab_gallery_sorter.py

This commit is contained in:
2026-01-18 21:19:06 +01:00
parent 8d3ef470fa
commit 7db73f8855

View File

@@ -5,91 +5,85 @@ from engine import SorterEngine
def render(quality): def render(quality):
st.subheader("🖼️ Gallery Staging Sorter") st.subheader("🖼️ Gallery Staging Sorter")
# 1. Fetch Profile Data with Fallbacks # 1. Profile & Path Loading
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, {})
# 2. Path Inputs (Placed at the top) # Path Inputs
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"), key="t5_s_in")
value=p_data.get("tab5_source", "/storage"), path_o = c2.text_input("🎯 Final Output Root", value=p_data.get("tab5_out", "/storage"), key="t5_o_in")
key="t5_path_s_input")
path_o = c2.text_input("🎯 Final Output Root",
value=p_data.get("tab5_out", "/storage"),
key="t5_path_o_input")
# 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"): 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() st.rerun()
# --- SETTINGS --- # --- 2. SIDEBAR: CATEGORY MANAGEMENT ---
col_opt1, col_opt2 = st.columns(2) with st.sidebar:
recursive = col_opt1.toggle("🔍 Search Subfolders", value=True) st.divider()
cleanup = col_opt2.radio("Cleanup Unmarked Files:", st.subheader("🏷️ Category Manager")
["Keep", "Move to Unused", "Delete"],
horizontal=True)
# Safety check: if path doesn't exist, stop here but keep the UI visible # Add new category
if not path_s or not os.path.exists(path_s): new_cat = st.text_input("New Category Name", key="t5_new_cat")
st.warning("Waiting for a valid Source Path...") if st.button(" Add Category", use_container_width=True):
return if new_cat:
SorterEngine.add_category(new_cat)
st.rerun()
# --- SIDEBAR CATEGORIES --- st.divider()
cats = SorterEngine.get_categories() # Selection of active tag
if not cats: cats = SorterEngine.get_categories()
st.error("No categories found in database. Please add some in Tab 4.") if not cats:
return st.warning("No categories found. Add one above!")
return
selected_cat = st.sidebar.radio("🏷️ Current Tag", cats, key="gallery_active_cat") selected_cat = st.radio("Active Tagging Label:", cats, key="t5_active_tag")
# --- GALLERY RENDERING --- # --- 3. GALLERY LOGIC ---
images = SorterEngine.get_images(path_s, recursive=recursive) images = SorterEngine.get_images(path_s, recursive=True)
staged = SorterEngine.get_staged_data() staged = SorterEngine.get_staged_data()
st.write(f"Images: **{len(images)}** | Staged for Rename: **{len(staged)}**") st.write(f"Found: **{len(images)}** | Staged: **{len(staged)}**")
# Grid Display (using 4 columns) # Grid Rendering
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]: is_staged = img_path in staged
is_staged = img_path in staged
# Show the new name if tagged # IMPROVED FEEDBACK: Show the specific tag and new filename
label = f"{staged[img_path]['name']}" if is_staged else os.path.basename(img_path) if is_staged:
info = staged[img_path]
st.success(f"TAGGED: {info['cat']}")
label = f"New Name: {info['name']}"
else:
label = os.path.basename(img_path)
# Process image through engine st.image(SorterEngine.compress_for_web(img_path, quality), caption=label)
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}"): # Action Buttons
if not is_staged: if not is_staged:
ext = os.path.splitext(img_path)[1] if st.button(f"Tag as {selected_cat}", key=f"tag_{idx}"):
# Count items already staged for this category to determine suffix ext = os.path.splitext(img_path)[1]
cat_count = len([v for v in staged.values() if v['cat'] == selected_cat]) + 1 # Generate suffix based on existing tags for this category
new_name = f"{selected_cat}_{cat_count:03d}{ext}" count = len([v for v in staged.values() if v['cat'] == selected_cat]) + 1
SorterEngine.stage_image(img_path, selected_cat, new_name) new_name = f"{selected_cat}_{count:03d}{ext}"
else: SorterEngine.stage_image(img_path, selected_cat, new_name)
# Untagging logic: simply remove from the staging table st.rerun()
# (Requires a delete_staged_image method in your engine) else:
pass if st.button("❌ Remove Tag", key=f"untag_{idx}"):
st.rerun() # Calls the new method we need to add to engine
else: SorterEngine.clear_staged_item(img_path)
st.error("Failed to load image.") st.rerun()
st.divider() st.divider()
# --- APPLY CHANGES --- # --- 4. APPLY CHANGES ---
cleanup = st.radio("Unmarked Files:", ["Keep", "Move to Unused", "Delete"], horizontal=True)
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("Files renamed and moved!")
st.success("Disk sync complete!") st.rerun()
st.rerun()
else:
st.error("Nothing is staged. Tag some images first!")