Fix StreamlitAPIException when jumping to a favorite folder

Set nav_path_input before the widget renders and use on_change callbacks
instead of modifying widget state after instantiation.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-02 12:52:05 +01:00
parent b02bf124fb
commit a1bda9a979

52
app.py
View File

@@ -48,42 +48,42 @@ with st.sidebar:
st.header("📂 Navigator") st.header("📂 Navigator")
# --- Path Navigator --- # --- Path Navigator ---
# Sync widget key with current_dir so the text input always reflects the actual path # Always sync the widget value to current_dir BEFORE the widget renders
if "nav_path_input" not in st.session_state: st.session_state.nav_path_input = str(st.session_state.current_dir)
st.session_state.nav_path_input = str(st.session_state.current_dir)
new_path = st.text_input("Current Path", key="nav_path_input") def _on_path_change():
if new_path != str(st.session_state.current_dir): new_path = st.session_state.nav_path_input
p = Path(new_path).resolve() if new_path != str(st.session_state.current_dir):
if p.exists() and p.is_dir(): p = Path(new_path).resolve()
st.session_state.current_dir = p if p.exists() and p.is_dir():
st.session_state.config['last_dir'] = str(p) st.session_state.current_dir = p
save_config(st.session_state.current_dir, st.session_state.config['favorites']) st.session_state.config['last_dir'] = str(p)
st.rerun() save_config(st.session_state.current_dir, st.session_state.config['favorites'])
elif new_path.strip():
st.error(f"Path does not exist or is not a directory: {new_path}") st.text_input("Current Path", key="nav_path_input", on_change=_on_path_change)
# --- Favorites System --- # --- Favorites System ---
pin_col, unpin_col = st.columns(2) if st.button("📌 Pin Folder", use_container_width=True):
with pin_col: if str(st.session_state.current_dir) not in st.session_state.config['favorites']:
if st.button("📌 Pin Folder", use_container_width=True): st.session_state.config['favorites'].append(str(st.session_state.current_dir))
if str(st.session_state.current_dir) not in st.session_state.config['favorites']: save_config(st.session_state.current_dir, st.session_state.config['favorites'])
st.session_state.config['favorites'].append(str(st.session_state.current_dir)) st.rerun()
save_config(st.session_state.current_dir, st.session_state.config['favorites'])
st.rerun()
favorites = st.session_state.config['favorites'] favorites = st.session_state.config['favorites']
if favorites: if favorites:
fav_selection = st.radio( def _on_fav_jump():
sel = st.session_state._fav_radio
if sel != "Select..." and sel != str(st.session_state.current_dir):
st.session_state.current_dir = Path(sel)
st.radio(
"Jump to:", "Jump to:",
["Select..."] + favorites, ["Select..."] + favorites,
index=0, index=0,
label_visibility="collapsed" key="_fav_radio",
label_visibility="collapsed",
on_change=_on_fav_jump,
) )
if fav_selection != "Select..." and fav_selection != str(st.session_state.current_dir):
st.session_state.current_dir = Path(fav_selection)
st.session_state.nav_path_input = fav_selection
st.rerun()
# Unpin buttons for each favorite # Unpin buttons for each favorite
for fav in favorites: for fav in favorites: