Multiple forms in a page

I want to take multiple inputs from users. The inputs can be split into logical groups, and it makes sense to take all inputs of same group one at a time. So, I tried to take different groups as part of separate form.

However, I’m facing the issue that values of previous forms are being reset when new form is being submitted. Using clear_on_submit=False does not seem to have any effect.

Here’s a reproducible example:

import streamlit


def get_inputs(input_id: int):
    with streamlit.expander(f"Dummy Section {input_id}", expanded=True):
        with streamlit.form(f"form_{input_id}", clear_on_submit=False):
            value_1 = streamlit.number_input(f"Dummy Input {input_id} Value 1")
            value_2 = streamlit.number_input(f"Dummy Input {input_id} Value 2")
            value_3 = streamlit.number_input(f"Dummy Input {input_id} Value 3")

            submitted = streamlit.form_submit_button(label=f"Submit Input 1")

            if not submitted:
                streamlit.warning("Please enter valid inputs before proceeding")
                streamlit.stop()
            else:
                return {"value_1": value_1, "value_2": value_2, "value_3": value_3}


def dummy_main_logic(*args):
    print(args)


streamlit.title("Dummy Title")

inputs_1 = get_inputs(1)
inputs_2 = get_inputs(2)
inputs_3 = get_inputs(3)

dummy_main_logic(inputs_1, inputs_2, inputs_3)

If I run this with the following command, I never get the chance to provide inputs_3.

python -m streamlit run test.py

So, my question is whether this is by design, or is this a bug?

I really do not want to show all inputs simultaneously using one form or a lot of inputs without any form. The main reason is the final call is going to be expensive, and I do not want to trigger this for all intermediate stages. If anyone faced this before, is there any workaround for this?

A related question: is there a way to auto-collapse a section after form is submitted? or basically any way to collapse a section programmatically?

Thanks in advance.

1 Like

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

For future readers of this post, check out the example in this thread: