I am trying to create a simple app using Streamlit which essentially boils down to three steps:
- A user specifies what data to load.
- The data is loaded and cached.
- Some statistics for this data is displayed.
A simple toy example:
x1 = st.sidebar.text_input('Input x1') x2 = st.sidebar.text_input('Input x2') if st.button('Load data'): data = load_data(x1, x2) if st.checkbox('Show mean'): st.text(data.mean()) if st.checkbox('Show median'): st.text(data.median())
load_data function is decorated with
The problem is that when I switch on a checkbox, I get:
NameError: name 'data' is not defined.
I do realise that it happens because Streamlit reads the whole file from top to bottom each time I change something (e.g. I switch on/off a checkbox) and when I use one of the checkboxes,
data is lost. However, there must be a way to get this data somehow since it’s been cached.
Is there some way how I can get a reference to the cached data apart from duplicating
data = load_data(x1, x2) within each
Note, I am new to Streamlit and might be missing some basic functionality.
Any help is appreciated.