Streamlit form : variable value is not passing to the other variable of the input widgets

Hi ,

I just started using st.form , I did not find this question anywhere. Please bear with me.

This is my code.

with st.form('form1'):
    parameter1 , parameter2 = st.columns(2)
    with parameter1:
        grps = ['ABC','DEF']
        grp= st.selectbox('Account',grps )
    with parameter2:
        if grp == 'ABC':
            items = ['ABC1','ABC2','ABC3']
        else:
            items = ['DEF1','DEF2']
        items = st.multiselect('Items',items)
    submit_form = st.form_submit_button('Submit')
    st.write(grp,items)

When I select the first select box to DEF , it should automatically pick the next multiselect based upon the first variable value but here it is not considering the grp input.
It is still showing ABC1 , ABC2 , ABc3 values , even if I select DEF grp.
when I hit submit , it is only displaying the grp value but not the multi select one.

Please help

The expected output should be…
If I select ABC group then in the Multiselect only ABC items should be displayed for selection.

This is how it looks when selecting the options.
[Basically the Items should show DEF1 , DEF2 but here it is not happening please see above question]


After I hit submit

@Santosh I tried debugging it. It seems like unless you don’t press the submit button inside the form, no activity happens.

grps = ['ABC','DEF']
grp = st.selectbox('Account', grps)
print(f"GRP Value = {grp}")

If you try to change the value in Account, nothing prints.

Maybe it is a restriction in form.

1 Like

Hi @Santosh, A form is meant to batch-process inputs; if you have conditional logic for any widget, dont put those widgets in a form. Check your revised code below:

parameter1 , parameter2 = st.columns(2)
with parameter1:
    grps = ['ABC','DEF']
    grp= st.selectbox('Account', grps )
with parameter2:
    if grp == 'ABC':
        items = ['ABC1','ABC2','ABC3']
    else:
        items = ['DEF1','DEF2']
    items = st.multiselect('Items',items)
submit = st.button('Submit')
st.write(grp,items)

Cheers

1 Like

Thanks! @gaganmanku96. I was thinking since since Streamlit runs from top to bottom inside the forms.
So I thought it would pass the first input widget value to the second input widget. Well a good thing to know :slight_smile:

Thanks! @Shawn_Pereira. I’m trying to create a operational report so form is much needed for me. I’ll do some trial and error methods and post it here if I found any work around :slight_smile:

Hi ,

I can able to achieve this by placing the parameter1 , parameter2 columns outside of the form. :slight_smile: thanks much for your time.

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