Streamlit Possible to Not Reload on Changing Input

Hi, I currently have a simple streamlit app. The goal is to take two parameters as input, then generate some text with them.

This is my app right now:

import streamlit as st


input1 = st.number_input('Input 1', min_value=1, max_value=20, value=5)
input2 = st.text_input('Input 2')

if st.button('Generate Some Text'):
   text = generate_text(input1, input2)
   st.write(text)

This is mostly working great. It takes in the two parameters, and generates text when the button is pressed.

There’s a couple things I would want to change if possible.

  1. Can I make it so the script will only rerun on pressing the button, and not when an input is changed?
  2. Is it possible to remove the ‘Press Enter to Apply’ popup text from text/number inputs?

Thanks.

2 Likes

Hi Christian,

Welcome to the Streamlit forum. As far as I know, its not possible to make the script run only when the button is pressed, and not on every input change. Making the whole thing run on every input change is integral to how Streamlit works. There is st.stop(), but I don’t believe it does what you’re asking for.

I don’t know the answer to your second ask.

Dinesh

2 Likes

Thanks. That’s what I figured, just wanted to confirm.

Check this:
https://docs.streamlit.io/en/stable/advanced_concepts.html#batch-elements-and-input-widgets

you can associate some inputs to a single form, changes to these inputs won’t trigger rerun except after pressing the submit button → called “Batch modifications”

1 Like

I have one question for backend,

I created the frontend in streamlit, and now I am confused with backend part. how’s the backend part work with streamlit. Do you have any idea for it?

Hi @milenge_dubara - can you explain what you’re implementing? Is it a custom component backend, an external API server backend, or database backend?

The solution is @OmarMAmin 's answer. Documentation can be found here.

So, the OP’s code will be like this:

import streamlit as st

form = st.form(key="user_form")
input1 = form.number_input('Input 1', min_value=1, max_value=20, value=5)
input2 = form.text_input('Input 2')

if form .form_submit_button('Generate Some Text'):
   text = generate_text(input1, input2)
   st.write(text)