Change st.beta_expander parameter based on specific value

Hello guys,
With st.beta_expander we can manually expand or colapse
How to change the parameter (True/False) with code based on specific value?
For example:
if the value is >10 then the expanded parameter change to False
if the value is <=10 the the expanded parameter change to True

1 Like

Due to the execution model of Streamlit the solution is not very elegant. This is what I do when I want the expanded state and label of the expander to change based on certain session state values (in my case they are the user’s authentication status).

    AUTH_LABEL = 'Authenticate'

    label = AUTH_LABEL
    if (check_token(session_state.token)):
        label = f'{session_state.user} ({session_state.email})'
    with st.beta_expander(label):
        auth_component_handler.init()
        # force a rerun to flip the expander label
        logged_in_but_showing_login_label = (check_token(session_state.token) and label == AUTH_LABEL)
        logged_out_but_showing_logged_in_label = (not check_token(session_state.token) and label != AUTH_LABEL)
        if (logged_in_but_showing_login_label or logged_out_but_showing_logged_in_label):
            st.experimental_rerun()

Here’s a demo: