Updating st.expander title based on its contents

Hi! I am working with latest Streamlit version (deployed only locally at this point) and would like to understand if it is possible to update st.expander() title after it has been created.

My use case is something along these lines:

expander = st.expander("Default title")
selected_option = expander.selectbox(
  "Select an option", 
  options=['one', 'two', 'three']
#now, I would like to update the expander title
#something like
# expander.title = selected_option

Is this possible?

Yes, it is possible combining st.session_state, the key parameter of st.selectbox, and pythonโ€™s or operator.

import streamlit as st

if "title_expander" not in st.session_state:
    st.session_state.title_expander = None

with st.expander(st.session_state.title_expander or "Default"):
    option = st.selectbox("Select an option", ["One", "Two", "Tres"], key="title_expander")


1 Like

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