Hiding pages in sidebar

I’ve been struggling to find a way to hide a page on the sidebar. From what I have been discovering online, st.pages is the way to go. Although examples of code using “hide_pages” seemed non-existent, I did find the following somewhere but somehow I cant find the source of this anymore. Regardless I created a project with the following as the main app.py. I created a “pages” folder and put in sample pages called “Example One”, “Example Two” and “Other apps”.

The UI comes up and works fine but no pages are ever hidden. Any help finding a way to hide pages would be greatly appreciated.

import streamlit as st
from st_pages import add_page_title, hide_pages

add_page_title()

st.write(“This is just a sample page!”)

selection = st.radio(
“Test page hiding”,
[“Show all pages”, “Hide pages 1 and 2”, “Hide Other apps Section”],
)

if selection == “Show all pages”:
hide_pages()
elif selection == “Hide pages 1 and 2”:
hide_pages([“Example One”, “Example Two”])
elif selection == “Hide Other apps Section”:
hide_pages([“Other apps”])

st.selectbox(“test_select”, options=[“1”, “2”, “3”])

You can define a function to hide selected pages. Here is a little code that should help you get started

import streamlit as st

# Create a sidebar selection
selection = st.sidebar.radio(
    "Test page hiding",
    ["Show all pages", "Hide pages 1 and 2", "Hide Other apps Section"],
)

# Define a list of pages
pages = ["Example One", "Example Two", "Other apps"]

# Define a function to hide selected pages
def hide_pages(pages_to_hide):
    for page in pages_to_hide:
        st.sidebar.markdown(f"## {page}")
        st.sidebar.markdown("This page is hidden.")

# Main app content
if selection == "Show all pages":
    # Display all pages in the sidebar
    for page in pages:
        st.sidebar.markdown(f"## {page}")
        st.sidebar.markdown("This is a sample page.")
elif selection == "Hide pages 1 and 2":
    # Hide pages 1 and 2
    pages_to_hide = ["Example One", "Example Two"]
    hide_pages(pages_to_hide)
elif selection == "Hide Other apps Section":
    # Hide the "Other apps" section
    pages_to_hide = ["Other apps"]
    hide_pages(pages_to_hide)

# Add some content to the main area of the app
st.title("Main Content")
st.selectbox("test_select", options=["1", "2", "3"])

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.