Nested selectbox in st.form?

python==3.9.0
streamlit==1.11.1

How to interact with st.selectbox in a st.form?
In st.form,select a value from b1 selectbox,then i want to display a selectbox base on b1 value.
Thanks.

import streamlit as st

with st.form("first_form2"):
    b1 = st.selectbox(label='b1', options=['a', 'b'])
    if b1 == 'a':
        b2 = st.selectbox(label='b2', options=['a1', 'a2'])
    else:
        b3 = st.selectbox(label='b3', options=['b1', 'b2'])
    st.form_submit_button(label='submit')

1 Like

Hi @ji_haoran, I have used a work around for this, you have to add another form_submit_button after the select box b1. It would be something like this:

import streamlit as st

with st.form("first_form2"):
      b1 =st.selectbox(label='b1', options=['a','b'])
      change = st.form_submit_button("Change")
      if b1 == 'a':
            b2 = st.selectbox(label='b2', options=['a1','a2'])
      else:
            b3 = st.selectbox(label='b3',options=['b1','b2'])
      submit = st.form_submit_button("Submit")

To make the second select box change we have to manually press the change button. Since we can’t have an on_change function within st.form. I have used something like this for my use case. Hopefully, this helps :v:.

2 Likes

Thank you very much,it works :grinning:

1 Like