Sidebar Duplication Issue in Multipage

Hi,

I’m facing an issue with sidebar duplication in my multipage Streamlit app. The sidebar renders correctly on the first page load,

but when I navigate to another page using st.page_link, the sidebar temporarily duplicates itself, appearing twice on the screen.

After navigating back and forth between pages, the sidebar eventually returns to normal (only one instance), but this duplication happens consistently on the first click when switching between pages when visiting the app.

I have tried to do some ‘st.rerun’ here and there but it didn’t fix the issue.

Here is the code for the ‘navigation.py’:

import streamlit as st
from streamlit.runtime.scriptrunner import get_script_run_ctx
from streamlit.source_util import get_pages

def get_current_page_name():
    ctx = get_script_run_ctx()
    if ctx is None:
        raise RuntimeError("Couldn't get script context")
    
    pages = get_pages("")
    return pages[ctx.page_script_hash]["page_name"]


def make_sidebar():
    with st.sidebar:
        st.title("Navigation")
        st.markdown("")
        
        # Check if session and token are already in session_state
        session = st.session_state.get("session", None)
        bearer_token = st.session_state.get("bearer_token", None)

        if session and bearer_token:
            st.session_state["logged_in"] = True
        else:
            st.session_state["logged_in"] = False

        # Check if the user is logged in
        if st.session_state.get("logged_in", False):
            # Show page links if logged in
            st.page_link("pages/CFN_Search.py", label="Item Search")
            st.page_link("pages/Items_Data_Fetcher.py", label="Item Data Fetcher")
            st.divider()
        else:
            st.warning("You need to log in.")
            # Redirect to Home page if not logged in
            if get_current_page_name() != "Home":
                st.experimental_set_page("Home")

Any suggestions would be appreciated.