Automatically updating options in the selectbox after modifying options

Hello,

I would like to update options in the selectbox using an add or remove button. The problem in the following code is options are not updated until you re-render it by changing choice in the selectbox or hitting a button once again. Is it possible to update the options automatically after modifying the options? Thanks.

Here are codes.

choices = [“A”, “B”, “C”]
if ‘choices’ not in st.session_state:
st.session_state[‘choices’] = choices

choices = st.session_state[‘choices’]
selected = st.selectbox(“Select an object”, choices)

if ‘number’ not in st.session_state:
st.session_state[‘number’] = 0

st.write(f"You selected {selected}")

if st.button(‘add’):
st.session_state[‘choices’] = choices + list(f’‘’{st.session_state[‘number’]}‘’')
st.session_state[‘number’] +=1

1 Like

The button has an on_click parameter that you can use to update the choices.

1 Like

Thanks, ferdy!

1 Like

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