Fix path navigator by replacing on_change with inline check

The on_change callback had timing issues with Streamlit's session state,
causing user input to be discarded. Now checks the widget value inline
after render and triggers rerun on valid directory change.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-22 16:53:06 +01:00
parent 0cfe9c9d4b
commit 907e7efd68

31
app.py
View File

@@ -47,25 +47,24 @@ with st.sidebar:
st.header("📂 Navigator") st.header("📂 Navigator")
# --- Path Navigator --- # --- Path Navigator ---
# Sync widget only when dir changed externally (favorites, first load) if "nav_path_input" not in st.session_state:
if st.session_state.get("_sync_nav_path", True): st.session_state.nav_path_input = str(st.session_state.current_dir)
if st.session_state.get("_sync_nav_path"):
st.session_state.nav_path_input = str(st.session_state.current_dir) st.session_state.nav_path_input = str(st.session_state.current_dir)
st.session_state._sync_nav_path = False st.session_state._sync_nav_path = False
def _on_path_change(): nav_path = st.text_input("Current Path", key="nav_path_input")
new_path = st.session_state.nav_path_input if nav_path != str(st.session_state.current_dir):
if new_path != str(st.session_state.current_dir): p = Path(nav_path).resolve()
p = Path(new_path).resolve() if p.exists() and p.is_dir():
if p.exists() and p.is_dir(): st.session_state.current_dir = p
st.session_state.current_dir = p st.session_state.config['last_dir'] = str(p)
st.session_state.config['last_dir'] = str(p) save_config(st.session_state.current_dir, st.session_state.config['favorites'])
save_config(st.session_state.current_dir, st.session_state.config['favorites']) st.session_state.loaded_file = None
st.session_state.loaded_file = None st.rerun()
else: else:
# Revert to current dir on invalid path st.session_state.nav_path_input = str(st.session_state.current_dir)
st.session_state.nav_path_input = str(st.session_state.current_dir) st.rerun()
st.text_input("Current Path", key="nav_path_input", on_change=_on_path_change)
# --- Favorites System --- # --- Favorites System ---
if st.button("📌 Pin Folder", use_container_width=True): if st.button("📌 Pin Folder", use_container_width=True):