Multiselect base on precedent selection not working

Background: I want to create a survey form which required a checkbox-tree selection where a checkbox is checked, then it will expand for more check-boxes until 4 levels. I found a contribution (It is what I want it to look like) but it unable to add input text. The post is here. Now, I have to find another way to achieve the same objectives due to deadline.

Problem: With the code without a submit form, it works as intended. The multiselect will then open “Sector” multiselect and if “Others” is selected, it will open the text input for user to key in.
However, it will not work in a form. I try to understand session.state but seems the state will not change, How can I let the create a form but able to the next level selection base on precedent selection?

Without form submit button (able to select base on precedent selection)

st.write(st.session_state)
attributes = st.multiselect('Choose attributes', ['Sector', 'Revenue'])
st.write(st.session_state)

if "Sector" in attributes:
    sector = st.multiselect('Choose sectors', ["Service", "Manufacturing","Consulting","Others"])
    if "Others" in sector:
        st.text_input(label="key in others sector")

if "Revenue" in attributes:
    revenue = st.multiselect('Choose revenues', ["over 1 mil", "below 1 mil","no revenue"])
    if "no revenue" in revenue:
        st.text_input(label="key in the reason")
st.write(st.session_state)

Not able to select base on precedence selection

with st.form('my_form'):
    st.write(st.session_state)
    attributes = st.multiselect('Choose attributes', ['Sector', 'Revenue'])
    st.write(st.session_state)
    if 'Sector' in st.session_state:
        st.write("sector not in")
        st.write(st.session_state)
    else:   
        st.write("sector is in")
    if "Sector" in attributes:
        sector = st.multiselect('Choose sectors', ["Service", "Manufacturing","Consulting","Others"])
        if "Others" in sector:
            st.text_input(label="key in others sector")

    if "Revenue" in attributes:
        revenue = st.multiselect('Choose revenues', ["over 1 mil", "below 1 mil","no revenue"])
        if "no revenue" in revenue:
            st.text_input(label="key in the reason")
    st.form_submit_button('Submit')        
    st.write(st.session_state)

Please advise how to make it work. I also open if checkbox is able to work in my case. Thanks!

Hi @bkcollection

  1. If you want to create any interdependent logic between widgets, don’t put them into a form, as a form only batch-process all the widgets when the form submit button is pressed.
  2. Why do you need a form specifically? If it is just for the border, you could put your widgets into a container and style the container - refer to other threads on this subject on this forum
  3. Based on the contents of your code, you could also reference this link: streamlit-dynamic-filters · PyPI

Cheers

I need a form as it is a survey form and the data will store in database. Is any of your solution able to add, edit and delete data in database? Form is what I can think of as for now. Kindly advice

Hi @bkcollection, I generally use sqllite to store application data, so I speak from that knowledge. For that you don’t need a form to store data into the database. If you also plan to use sqllite, you can go through the documentation. There are youtube videos on the same subject too.

Cheers

My meaning is I have a solution for database. Because I need to add, edit and delete data from database, I need to use a form. That is the reason why I stuck because I need a solution of multiselect which base on the precedent selection.Sorry if it is not clear from my last reply

No, you don’t need a form for that.

Can you elaborate further on this?Thanks.
How can I achieved this without a form. Beginner which I follow an example with form. My understanding is with a form, we can update the database with submit_form.

Just do the same thing but without a form. Or you can still use a form, but not all the widgets have to be inside the form. I don’t understand what the problem is.

You usually update the database by executing SQL statements. Forms are a UI thing, the database doesn’t care about that.