Cache gets lost on switching pages with experimental_allow_widgets.
Steps to reproduce
page1.py
import streamlit as st
import time
@st.cache_data(experimental_allow_widgets=True)
def calculate():
time.sleep(1)
if st.sidebar.checkbox("Are you awesome 1?"):
st.write("Hello awesome page 1")
else:
st.write("Hello page 1")
st.write("I am page 1")
calculate()
page2.py
import streamlit as st
import time
@st.cache_data(experimental_allow_widgets=True)
def calculate():
time.sleep(1)
if st.sidebar.checkbox("Are you awesome 2?"):
st.write("Hello awesome page 2")
else:
st.write("Hello page 2")
st.write("I am page 2")
calculate()
pageA.py
import streamlit as st
import time
@st.cache_data()
def calculate(checked):
time.sleep(1)
if checked:
st.write("Hello awesome page A")
else:
st.write("Hello page A")
st.write("I'am page A")
checked = st.sidebar.checkbox("Are you awesome A?")
calculate(checked)
pageB.py
import streamlit as st
import time
@st.cache_data()
def calculate(checked):
time.sleep(1)
if checked:
st.write("Hello awesome Page B")
else:
st.write("Hello Page B")
st.write("I'am Page B")
checked = st.sidebar.checkbox("Are you awesome B?")
calculate(checked)
Steps to reproduce:
Switch between page1 and page2 multiple times. Observe Caching.
Switch between pageA and pageB multiple times. Observe Caching.
Expected behavior:
page1 and page2 use caching.
(page1 and page2 should behave as pageA and PageB)
Actual behavior:
Caching on page1 and page2 does not work. Even without using the Widget.
Caching on pageA and pageB does work as expected.
Debug info
Streamlit version: 1.22.0
Python version: 3.10.8
Using Conda
Additional Info
Caching does work if you stay on that page.
(page1 and page 2 do cache the button state as long as you don’t switch to an other page.)
Thanks for sharing the question and implementations. This seems to be a recurring issue that the Streamlit team is looking into. If you’d like to join in on the discussion (there are a few workarounds that you may find helpful), you can find it in the link below:
Do you mean that the current implementation of “Keyed widget state persistence” is the direct cause of “Experimental_allow_widgets breaking caching for multipage apps”?