I have a streamlit app with two pages. How could I make both of my pages’ sidebar collasped at first after clicking into?
I tried initial_sidebar_state=‘collapsed’ on both page. However it seems only the first one is collapsed, and when I moved to page 2, the sidebar for page 2 is expanded.
import streamlit as st
if 'sbstate' not in st.session_state:
st.session_state.sbstate = 'collapsed'
def Page01():
st.set_page_config(page_title='page1', page_icon='📋', layout='wide', initial_sidebar_state=st.session_state.sbstate)
st.subheader('Page01')
st.sidebar.write("I am in the sidebar pg01")
if st.button("Return to Main Page"):
st.session_state.runpage = Page_Main
st.experimental_rerun()
def Page02():
st.set_page_config(page_title='page2', page_icon='📋', layout='wide', initial_sidebar_state=st.session_state.sbstate)
st.subheader('Page02')
st.sidebar.write("I am in the sidebar pg02")
if st.button("Return to Main Page"):
st.session_state.runpage = Page_Main
st.experimental_rerun()
def Page_Main():
st.set_page_config(layout='wide', initial_sidebar_state=st.session_state.sbstate)
st.subheader('Main Page')
sc1, sc2 = st.columns(2)
if sc1.button("Goto Page1"):
st.session_state.runpage = Page01
st.experimental_rerun()
if sc2.button("Goto Page2"):
st.session_state.runpage = Page02
st.experimental_rerun()
if 'runpage' not in st.session_state:
st.session_state.runpage = Page_Main
st.session_state.runpage()
Hi @Shawn_Pereira , thanks for your code. However I want the user to be able to switch page and have the page set as collapsed when they click on the default streamlit’s side bar settings instead of defining two buttons to achieve it. Materials under my page are also stored in two seperate python files as 1_page01.py, 2_page02.py as suggest from streamlit website instead of in two functions. Is there any possibility we do it that way?
I currently write application pages with functions only - I haven’t tried the page option because I have complete flexibility with functions. Maybe someone who is using something like st.pages can comment.
Maybe you can use a similar logic to get your application running
Hi @Shawn_Pereira ,
I think your code also fails if I move the button(‘Return to main page’) for both pages in to st.sidebar.button.(So page 1 changes to collapsed, and when you go back to the main page, it goes to ‘expanded’ again). Do you know why this happend or how to fix it?
//For both page1 and 2,
def Page01():
st.set_page_config(page_title='page1', page_icon='📋', layout='wide', initial_sidebar_state=st.session_state.sbstate)
st.subheader('Page01')
st.sidebar.write("I am in the sidebar pg02")
if st.sidebar.button("Return to Main Page"):
st.session_state.runpage = Page_Main
st.experimental_rerun()
I don’t need the user to switch pages or see text messages on the side bar when the side bar is collapsed. The page switch and any text display should only be done when the side bar is opened. And when they jump to a new page, the side bar is closed by default. All my sbstate is set to ‘collapsed’ as I tested when a new page opens, however, the side bar still shows up as expanded.
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.