How to hide pages in a streamlit multiple page app?

732 Views Asked by At

I have developed a Streamlit app with multiple pages using the sidebar. My objective is to implement a mechanism where clicking on one page in the sidebar will hide the other pages. I'm seeking guidance on how to achieve this behavior effectively. Could you please provide assistance or suggestions for implementing this functionality?

I've created a Streamlit app with multiple pages, but currently, when I click on one page, the sidebar persists on other pages. I want to modify this behavior so that clicking on one page hides the sidebar on other pages. This is the code:

enter code here

def run():
    with st.sidebar:
        app = option_menu(
            menu_title="pondering",
            options=['Account','Trending','Your Posts','About'],
                    default_index=1)
    if app == 'Account':
        account.app()
    if app == 'home':
        home.app()
    if app == 'Trending':
        trending.app()
    if app == 'Your Posts':
        your_posts.app()
    if app == 'About':
        about.app()
    run()
   
1

There are 1 best solutions below

2
Jamiu S. On

You can use the following custom function and call it in the first line of the pages functions where you want to hide the sidebar.

def hide_sidebar():
    st.markdown("""
    <style>
        section[data-testid="stSidebar"][aria-expanded="true"]{
            display: none;
        }
    </style>
    """, unsafe_allow_html=True)

Example:

def home():
    hide_sidebar() 
    ... 

If you don't want to use a function, you will have to copy the markdown and paste it in all your pages.