Widget that is dependent on other widget doesn't update on rerun

I have a selectbox with varying options, depending on a preceding selectbox.

However, when one changes a different option on the first selectbox, triggering a rerun, the second selectbox options do not update. Is that the intended functionality, even outside an st.form?

In addition, if the (not updated) second option isnโ€™t included to the list of options of the other case, streamlit throws a silent error.

MWE included:

import streamlit as st

edit_section = st.selectbox('Section',
                           ['A', 'B'],
                           index = 0,
                           key = 'edit_section')

max_position = {'A': 5, 'B': 1}[edit_section]

edit_position = st.selectbox('Position',
                             range(1, max_position + 1),
                             index = 0,
                             key = 'edit_position')

Steps to reproduce:

  • Choose 5 as the position.
  • Choose B as the section.
1 Like

Hi @Gouyoku

You should be able to prevent this by using a combination of st.form and st.session_state

Please find the related blog posts for each:

Let us know how it goes!

Best,
Charly

Thank you for your swift reply!

Actually you are right, if the second widget is inside an st.form and the first is outside, the app doesnโ€™t throw a silent error. In fact, I had already used the same snippet at another place within my app to achieve a similar goal.

Unfortunately, Iโ€™m not quite satisfied with the appearance of it, but it works as intended, soโ€ฆ :slight_smile: