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
3 Likes

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