I wish to show different fields in a form, depending on what values a radio button takes. The radio button must be shown after some of the text entry fields and before some of the others.
This runs, but the radio button gets put at the bottom of the page:
form = st.form("form", border = False)
first = form.text_input("First")
second = form.text_input("Second")
show_questions = st.radio("Show questions", ("No", "Yes"))
if show_questions == "Yes":
third = form.text_input("Third")
fourth = form.text_input("Fourth")
submit_button = form.form_submit_button("Submit")
I do not want that. I want the radio buttons to show up between the second and third text inputs.
This runs, but changing the value in the radio button does not change what fields are chosen.
form = st.form("form", border = False)
first = form.text_input("First")
second = form.text_input("Second")
show_questions = form.radio("Show questions", ("No", "Yes"))
if show_questions == "Yes":
third = form.text_input("Third")
fourth = form.text_input("Fourth")
submit_button = form.form_submit_button("Submit")
I do not want that. Changing what value is selected in the radio button must immediately change what fields are displayed.
This runs, but changing any text box makes the page rerun:
first = st.text_input("First")
second = st.text_input("Second")
show_questions = st.radio("Show questions", ("No", "Yes"))
if show_questions == "Yes":
third = st.text_input("Third")
fourth = st.text_input("Fourth")
submit_button = st.button("Submit")
I do not want that. Typing in the text entry boxes must not rerun the page.
This causes an error saying āMissing Submit Button: This form has no submit button, which means that user interactions will never be sent to your Streamlit app. To create a submit button, use the st.form_submit_button()
function.ā
form_top_half = st.form("form_top_half ", border = False)
first = form_top_half.text_input("First")
second = form_top_half.text_input("Second")
show_questions = st.radio("Show questions", ("No", "Yes"))
form_bottom_half = st.form("form_bottom_half", border = False)
if show_questions == "Yes":
third = form_bottom_half.text_input("Third")
fourth = form_bottom_half.text_input("Fourth")
submit_button = form_bottom_half.form_submit_button("Submit")
But I do not want to have a separate submit button for the top half of the form.
What should I do? How can I have a radio button in the middle of the form, that changes the contents of the form when toggled, while still using forms to avoid rerunning the page when the text in a text box is changed?