Search code examples
streamlit

Simplest way to hide a page from Streamlit sidebar


I have two pages, page1,py and page2.py in my pages folder. page1.py calls page2.py, so I want to hide page2.py from the sidebar/side navigation. What is the simplest way to do this?

  1. There’s st_pages, but that doesn’t work in a docker container for some reason
  2. There are multiple posts on hiding the entire sidebar and recreating it in a custom way, but these are brittle. Sometimes both sidebars show up.
  3. I’ve tried mucking around with the CSS + Javascript to hide a single element, but no success so far.

Happy to share more details on my attempts, but there ought to be a very simple way to hide a page. Or if I can move page2.py to a subfolder in page and then have page1.py call it via st.switch_page(“pages/subpages/page2.py”) that would work too, but seems like you can only switch_page to whatever is within the pages folder.


Solution

  • In a multipage app, you can hide the side bar by the following procedure.

    1. Create a .streamlit folder under your app main folder.

    enter image description here

    • The hide_sidebar folder is your main app folder.
    • The app.py is the main streamlit file.
    1. Under the .streamlit folder create a config.toml file and the content of this file is this.
    [client]
    showSidebarNavigation = false
    

    enter image description here

    1. Rerun streamlit to see its effect.

    How to show a page in a sidebar

    Now if you want to show a single page in the sidebar, for example the basketball page, you can create a page link widget and show it in the sidebar.

    app.py

    import streamlit as st
    
    
    st.header('Home')
    
    # Sidebar navigation
    st.sidebar.page_link('app.py', label='Home')
    st.sidebar.page_link('pages/basketball.py', label='Basketball')
    

    basketball.py

    import streamlit as st
    
    
    st.header('Basketball')
    
    # Sidebar navigation
    st.sidebar.page_link('app.py', label='Home')
    st.sidebar.page_link('pages/basketball.py', label='Basketball')
    
    

    Sample output

    enter image description here

    The soccer page is not shown.