Cannot expand st.sidebar after user action to collapse

With this simple code:

        with st.sidebar:
            st.subheader('Hello ' + self.__user)

The sidebar is properly rendered but after the user collapsed the sidebar, the expand icon seems disabled and the user unable to expand it again.

The state of the sidebar is controlled by the user. The only thing that you can control through page configuration is the initial state of the sidebar. This is true for both sidebar and expander.

If you need to force the state of the sidebar, you’ll have to rerun the app a couple times, toggling the initial first to the opposite of what you want, before returning it to what you actually want. This forces Streamlit to recognize “something different” and and discard the user’s chosen state.

import streamlit as st

SIDEBAR_STATE = {True: "expanded", False: "collapsed"}

if "open" not in st.session_state:
    st.session_state.open = True
    st.session_state.rerun = False

def open():
    st.session_state.open = True
    st.session_state.rerun = True

def close():
    st.session_state.open = False
    st.session_state.rerun = True

if st.session_state.rerun:
    st.set_page_config(initial_sidebar_state=SIDEBAR_STATE[not st.session_state.open])
    st.session_state.rerun = False
    st.rerun()

st.set_page_config(initial_sidebar_state=SIDEBAR_STATE[st.session_state.open])
st.sidebar.write("foo")

st.button("Open", on_click=open)
st.button("Close", on_click=close)
st.button("Rerun")
1 Like

Thanks @mathcatsand . The problem I have is that the user itself is unable to control the state of the sidebar. After the user collapsed the sidebar, the user is unable to expand it.

The simple code you posted cannot cause that, the problem lies somewhere in the rest of the code.

1 Like

Thanks Goyo. I retraced all the styles I implemented and found out a conflict. You can replicate it by having this style:

styles = {
“nav”: {
“background-color”: “white”,
“display”: “flex”,
},
}
options = {
‘show_menu’: False
}
pages = [‘’]
page = st_navbar(
pages,
styles=styles,
options=options
)
It is now behaving as expected after removing these lines.

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