App still re-runs with every widget interaction when using st.cache_data

I have an app that connects to BigQuery using st.cache_data like so:

def bigquery_connection():
	@st.cache_data(ttl=600)
	def run_query(query):
		query_job = client.query(query)
		raw_data = query_job.result()
		# Convert to list of dicts. Required for st.experimental_memo to hash the return value.
		data = [dict(data) for data in raw_data]
		return data

However, if I include a widget (i.e., a radio option or select box) and interact with it, it re-runs the app every time.

From the docs, it states that st.cache_data will alleviate this:

See st.cache_data above? Without it, Streamlit would run the query every time the app reruns (e.g. on a widget interaction). With st.cache_data, it only runs when the query changes or after 10 minutes (that’s what ttl is for). Watch out: If your database updates more frequently, you should adapt ttl or remove caching so viewers always see the latest data. Learn more in Caching.

Maybe I am not understanding this correctly or understanding how connections work in Streamlit but shouldn’t using st.cache_data not re-run the app each time?

I also tried st.cache_resource but the same thing happens.

1 Like

Following :slight_smile:

@digitalghost-dev

When a function is cached using @st.cache_data, if you interact with any widget, the values corresponding to the function will come from the cache. The app will still rerun on every interaction, but the cache function will not execute and cached results will be used. If you place a widget inside a cached function, every interaction with the widget will cause the cache to invalidate and force a re-run of the function.

Hope this helps.

–Carlos

@CarlosSerrano
I see what’s going on. My app may seem to be getting too big. When I run a small demo with nothing but the widget, I can see the “Running…” icon in the top left for a split second. I guess when the app is small enough, you don’t really notice it (at least I didn’t). That is why I thought something was bugged. It’s just how Streamlit works. I’ll have to play around with my app and see what I can do but with so many things to load, I’m not sure what I can do. Thanks for your explanation!

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