App refreshes each time a value is entered in number input widget element

I am building an app to run on Streamlit in Snowflake. And I’ve ran into issues with the app completely refreshing when a value is entered into a number input widget element.

The app refreshes when input is entered to the second number input field in the tab “An owl” tab.

This is my code snippet

import streamlit as st

# set up session state vars
if "searchOrder" not in st.session_state:
    st.session_state["searchOrder"] = False
if "searchUser" not in st.session_state:
    st.session_state["searchUser"] = False

st.write(f"""Order init: """, st.session_state["searchOrder"])

# function to flip states of buttons clicked
def handleButton(attribute, state):
    st.session_state[attribute] = state

# accept order ID to search from user
orderSearchParam = st.number_input("Order ID", value=None, step=1, format="%i", placeholder="Enter order ID")

# Update button state with the call-back function
orderSearchBtn = st.button("Search", key="orderSearchBtn", on_click=handleButton, args=("searchOrder", True))

# if search input accepted, materialise tabs and other elements down below
if st.session_state["searchOrder"]:

    st.write(f"""Order: """, st.session_state["searchOrder"])

    tab1, tab2 = st.tabs(["Dog", "Owl"])

    with tab1:
        st.header("A dog")
        st.image("https://static.streamlit.io/examples/dog.jpg", width=200)


    with tab2:
        st.header("An owl")
        st.image("https://static.streamlit.io/examples/owl.jpg", width=200)

        orderSearchParam = int(orderSearchParam)
        st.write("Search submitted for Order ID: ", orderSearchParam)

        st.write(f"""User init: """, st.session_state["searchUser"])

        # Add a number input widget with a search button below
        userSearchParam = st.number_input("User ID", value=None, step=1, format="%i", placeholder="Enter User ID")
       #flip the button state with the call-back function
        userSearchBtn = st.button("Search", key="userSearchBtn", on_click=handleButton, args=("searchUser", True))

        # if search parameter entered by user do the following
        if st.session_state["searchUser"]:
            st.write(f"""User: """, st.session_state["searchUser"])
            userSearchParam = int(userSearchParam)
            st.write("Search submitted for User ID: ", userSearchParam)

        keys = list(st.session_state.keys())
        for key in keys:
            st.session_state.pop(key)

Hi @Darshan1, welcome to the community! :wave:

The app reset is caused by the last two lines of code:

for key in keys:
  st.session_state.pop(key)

Once the userSearchBtn is clicked on the owl tab, you’re clearing the session state variable searchOrder. Since the tabs, images, and number input widget are conditionally displayed based on searchOrder being True, they are not displayed after the script rerun cased by the widget interaction (in this case, a button click), since the variable is deleted from state and reinitialized to False in your code.

Commenting out the for loop at the bottom prevents this undesired behavior:

pop-state

Happy Streamlit-ing! :balloon:

1 Like

Thanks @snehankekre. This works.

This app extends to add a form which users interact with and make a submission when they click on the form_submit_button. What’s the best way I can make sure the session_state variable are cleared after each form submission? This could be within the same session.

Do I call session_state.clear() from using the on_click parameter of form_submit_button?

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