Form and submit button in sidebar

Hi everyone,

I noticed that changing the value of an number_input inside a form, inside the sidebar, triggers a rerun. Is this behaviour avoidable?

with st.sidebar:

    with st.form(key='my_form'):
        input_1 = st.number_input('input_1', min_value=0.0, max_value=1000.0, value=5.0, step=1.0)

        input_2 = st.number_input('input_2', min_value=0.0, max_value=1000.0, value=5.0, step=1.0)

        submit_button = st.form_submit_button(label='Submit parameters')
1 Like

Hey @dp3456789 and welcome to the forum :wave:

Unfortunately, I wasn’t able to replicate the issue. What version are you on?
Also, does the whole script rerun and does the input value change in Python?

Please tell me more about the issue and I’ll be happy to help you out.
And thanks for using Streamlit!

2 Likes

@dp3456789 I am also experiencing this issue. Hoping someone can help :slight_smile:

with st.form(key ='Form1'):
    with st.sidebar:
        user_word = st.sidebar.text_input("Enter a keyword", "habs")    
        select_language = st.sidebar.radio('Tweet language', ('All', 'English', 'French'))
        include_retweets = st.sidebar.checkbox('Include retweets in data')
        num_of_tweets = st.sidebar.number_input('Maximum number of tweets', 100)
        submitted1 = st.form_submit_button(label = 'Search Twitter 🔎')

The button and UI show-up as intended, however, the app re-runs whenever any of the input parameters are changed. The intention is to batch any changes with a click of the submit button.

I am running the latest version of streamlit (0.83.0).

Hi @dmf95, welcome to the Streamlit community!! :wave: :partying_face:

Happy to help! As you are declaring widgets in the sidebar using the with syntax, you need to call them via st.widget instead of st.sidebar.widget:

import streamlit as st

with st.form(key ='Form1'):
    with st.sidebar:
        user_word = st.text_input("Enter a keyword", "habs")    
        select_language = st.radio('Tweet language', ('All', 'English', 'French'))
        include_retweets = st.checkbox('Include retweets in data')
        num_of_tweets = st.number_input('Maximum number of tweets', 100)
        submitted1 = st.form_submit_button(label = 'Search Twitter 🔎')

Alternatively, you can use st.sidebar.form and dispense with the nested with statement altogether. This has an added benefit of making your code easier to read:

import streamlit as st

with st.sidebar.form(key ='Form1'):
    user_word = st.text_input("Enter a keyword", "habs")    
    select_language = st.radio('Tweet language', ('All', 'English', 'French'))
    include_retweets = st.checkbox('Include retweets in data')
    num_of_tweets = st.number_input('Maximum number of tweets', 100)
    submitted1 = st.form_submit_button(label = 'Search Twitter 🔎')

Does this help?

Happy Streamlit’ing! :balloon:
Snehan

1 Like

Worked like a charm - thanks Snehan!

1 Like