Button will make file_uploader exec twice


When I want to use st.button and st.file_uploader, I found the click the button will make the file_uploader exec twice. Is this by designed? if so, how I can avoid it exec twice.

Steps to reproduce

uploaded_file = st.file_uploader(“Choose a file”)
if uploaded_file is not None:

if st.button(“test”):
Code snippet:

uploaded_file = st.file_uploader("Choose a file")
if uploaded_file is not None:

if st.button("test"):

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:
the print(test) will not exec two times.

Actual behavior:

Explain the undesired behavior or error you see when you run the code above.
If you’re seeing an error message, share the full contents of the error message here.

Debug info

  • Streamlit version: Streamlit, version 1.19.0
  • Python version: 3.8.9

From the documentation:

[…] Streamlit apps have a unique data flow: any time something must be updated on the screen, Streamlit reruns your entire Python script from top to bottom.

This can happen in two situations:

  • Whenever you modify your app’s source code.
  • Whenever a user interacts with widgets in the app. For example, when dragging a slider, entering text in an input box, or clicking a button.