Navigation

Hello,

I have the following code:

Visible pages

home_page = st.Page(page=“pages/landing_page.py”, title=“Home”, icon=“:material/home:”, default=True)
add_forecast_page = st.Page(page=“pages/berth_planning_location.py”, title=“Choose Location”, icon=“:material/location_on:”)
signal_deletion = st.Page(page=“pages/signal_deletion.py”, title=“Signal Conversion”, icon=“:material/contract_delete:”)

Navigation

pg = st.navigation({
“Info”: [home_page],
“Forecast”: [add_forecast_page, signal_deletion]
})

pg.run()

My problem is that during navigation (e.g., navigating from add_forecast_page to landing_page and then back to add_forecast_page ), how can I add a condition (session state) to ensure the page starts loading from the beginning instead of retaining the previous state?

It would be great to use the st.navigation and utilising the default side bar instead of building a separate one

We can use an if statement to detect page changes and reset the session states accordingly.

import streamlit as st


# Visible pages
home_page = st.Page(page="pages/landing_page.py",
                    title="Home", icon=":material/home:", default=True)
add_forecast_page = st.Page(page="pages/berth_planning_location.py",
                            title="Choose Location", icon=":material/location_on:")
signal_deletion = st.Page(page="pages/signal_deletion.py",
                          title="Signal Conversion", icon=":material/contract_delete:")

# Navigation
pg = st.navigation({
    "Info": [home_page],
    "Forecast": [add_forecast_page, signal_deletion]
})

if pg:
    print(f'Changing the page to {pg.title}')

    # Reset the session states
    # This is just an example
    st.session_state.location = None
    st.session_state.signal = None
    st.session_state.forecast = None

    # Finally, open the page
    pg.run()