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”])
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"])
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.