Multipage App saving variables between pages and want them to reset to default for each page

Summary

I am having an issue where the variable that is set on one page with a number input box seems to feed into other pages of the multipage app, even though the same variable is re-defined on each page with a set default value.

Steps to reproduce

Code snippet (test page 1):

import streamlit as st

st.set_page_config(page_title="TEST PAGE 1", page_icon="πŸ”Œ")

HOUSE_NUMBER = st.number_input(
    "Number of houses",
    value=36,
    min_value=1,
    max_value=100,
)

st.metric("Number of houses", HOUSE_NUMBER)

Code snippet (test page 2 - same as test page 1):

# page config to stay at top of page
import streamlit as st

st.set_page_config(page_title="TEST PAGE 2", page_icon="πŸ”Œ")

HOUSE_NUMBER = st.number_input(
    "Number of houses",
    value=36,
    min_value=1,
    max_value=100,
)

st.metric("Number of houses", HOUSE_NUMBER)

Then when I run the app for the first time - the value is correctly set to the default on the first page. Then if I change the value from the default of 36 to 10, it updates on that page correctly (see figure1 below).
However the issue arises then when I switch to the second test page, the number input box has the correct value, but the metric printing out the value of the variable HOUSE_NUMBER has saved the value from the first page. see figure 2 below.

FIGURE1 - changing the number on test page one


FIGURE2 - number from first page has fed into the second test page?

Expected behavior:

I would expect the second page to reset the value back to the default.

Actual behavior:

The default value shows correctly in the number input box but seems not to be when you display the value.

Debug info

  • Streamlit version: 1.18.1
  • Python version: 3.8.5
  • pip
  • Browser version: both local host and heroku deployment

Requirements file

numpy==1.23
streamlit==1.18.1
pyarrow==10.0.1
snowflake-connector-python==3.0.0
pandas==1.5.2
numba==0.56.4
plotly==5.12.0

Additional information

I have tried clearing the session state but it seems to already be empty.

This is an interesting example of an issue with widget state and multipage apps. We’re working on a way to make this behavior more intuitive, but in your case there is a simple workaround, which is to set a key on the st.number_input on your second page, and then streamlit will consistently treat them differently.

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