Hi Streamlit community,
I’m building a multipage app using st.navigation where my pages are organized in a folder called “apps” (e.g., apps/app1.py, apps/app2.py). The navigation works fine with the default sidebar menu.
I have a “Favorites” feature in the sidebar: a selectbox that lists user favorites (stored in Cosmos DB). Each favorite is tied to a specific app page and includes filters (e.g., date ranges, selections) saved in a dict. When a favorite is selected, I update st.session_state with the filters and want to navigate to the corresponding app page.
I’ve tried several approaches, but none successfully switch the page while preserving the session_state updates:
-
st.switch_page(f"apps/{app_file}") - This navigates but doesn’t apply the session_state filters reliably, and sometimes throws errors about page not found.
-
st.page_link(f"apps/{app_file}", label=“Go to page”) - This creates a link, but I need automatic navigation on selectbox change, not a clickable link.
-
Setting a session_state key (e.g., st.session_state.selected_page = app_name) and calling st.rerun() - This reruns the script but doesn’t change the active page in st.navigation.
-
Using query params (e.g., st.query_params.page = app_name) - This updates the URL but doesn’t trigger page switch in multipage setup.
My goal is for the selectbox on_change (or equivalent) to:
-
Update session_state with filters.
-
Switch to the target page (e.g., app1).
-
Have the target page load with the applied filters.
Is there a recommended way to achieve dynamic navigation in multipage apps from a sidebar component? Any examples or workarounds would be appreciated!
Streamlit version: 1.50.0
Thanks!
Rich