St.spinner: 'cache' Parameter

Hey everyone,

I just stumbeld upon the cache parameter in the st.spinner function.

I looked into the documentation and found no explaination about what it does. By looking at older version of Streamlit I concluded that it was added with the release of verision 1.28

Does someone know what the parameter does?

Best regards
Fabian

Thank you for raising this. Iโ€™m looking into it right now to get the documentation updated. Be right backโ€ฆ

There is a note/clue in the code:

My understanding is that it overwrites the default spinner that the cache_data decorator puts by default.

cache = False
image

cache = True
image

Code:
import streamlit as st
from time import sleep

@st.cache_data
def spinning_long_running_function(t: float, _cache: bool):
    with st.spinner("Sleeping...", cache=_cache):
        sleep(t)

_cache = st.checkbox('cache kwarg')

f"### `cache = {_cache}`"
t = st.number_input("Time (s)", 1, 10, 3, 1)
if st.button("Sleep"): 
    spinning_long_running_function(t, _cache)

I just confirmed: itโ€™s currently something used internally, but you can set it manually if you want. If you set cache=True the spinner widget will not be inline and instead float above page content, which prevents elements that follow the spinner from jumping down and back up if you use st.spinner on a script rerun.

2024-02-06_11-36-05 (1)

import streamlit as st
import time

a,b = st.columns(2)

with a:
    st.markdown("Before")
    with st.spinner("Running"):
        time.sleep(2)
    st.markdown("After")
    st.markdown("After")
    st.markdown("After")

with b:
    st.markdown("Before")
    with st.spinner("Running", cache=True):
        time.sleep(2)
    st.markdown("After")
    st.markdown("After")
    st.markdown("After")
1 Like