Collapsing expander after code has executed

Hello,

I have an expander of the type:

import streamlit as st
container = st.expander("XXX", expanded=True)
with container:
    [DO STUFF]

Is there a way to collapse the container once the STUFF has been executed?

Thanks in advance,

Zeno

You can make the expanded variable same as a session state value:

if 'is_expanded' not in st.session_state:
    st.session_state['is_expanded'] = True
container = st.expander("XXX", expanded=st.session_state['is_expanded'])
with container:
    [DO STUFF]
    st.session_state['is_expanded'] = False
2 Likes