Part of my code uses @st.cache.
I would like to print a message when cached data is being used.
Part of my code uses @st.cache.
I would like to print a message when cached data is being used.
Following code sample and explanation from streamlit doc can be useful to tweak into a value that is only printed first time. Vice versa can be more tricky.
import streamlit as st
import time
@st.cache(suppress_st_warning=True) # Changed this
def expensive_computation(a, b):
# Added this
st.write("Cache miss: expensive_computation(", a, ",", b, ") ran")
time.sleep(2) # This makes the function take 2s to run
return a * b
a = 2
b = 21
res = expensive_computation(a, b)
st.write("Result:", res)
Explanation from Streamlit: Now when you rerun the app the text "Cache miss" appears on the first run, but not on any subsequent runs. That's because the cached function is only being executed once, and every time after that you're actually hitting the cache.
Source: https://docs.streamlit.io/library/advanced-features/caching