I’m using the st.sidebar functionality to split my app up into pages. Within a given page, the states of any interactive widgets are preserved (e.g., text entered into a st.text_input field) even if I navigate to a different page and then come back later. However, variables/states that are set on one page aren’t accessible on other pages.
As a simple example, here’s some code with three pages trying to define “A”, define “B”, and then add them together:
import streamlit as st
st.sidebar.title("Pages")
radio = st.sidebar.radio(label="", options=["Set A", "Set B", "Add them"])
if radio == "Set A":
a = float(st.text_input(label="What is a?", value="0"))
st.write(f"You set a to {a}")
elif radio == "Set B":
b = float(st.text_input(label="What is b?", value="0"))
st.write(f"You set b to {b}")
elif radio == "Add them":
st.write(f"a={a} and b={b}")
button = st.button("Add a and b")
if button:
st.write(f"a+b={a+b}")
When I run this code in Streamlit, I can set and reset A and B but when I flip to the “Add them” page, I get an exception NameError : name 'a' is not defined
.
Any suggestions on how to preserve these variables/states across multiple sidebar pages?