pythonpandasstreamlit

st.status spinner keeps showing unnecessarily even though my function is cached


This is my code:

@st.cache_data(show_spinner=False)
def fetch_data():
    with st.status("Buscando datos...", expanded=True) as status:
        time.sleep(2)
        st.write("Partidos encontrados...")
        time.sleep(2)
        st.write("Descargando datos...")
        time.sleep(2)
        st.write("Dibujando tabla...")
        game_data = scraper.get_data(game_list_input=games_id)
        data_df = scraper.get_data_df(data_list=game_data)
        games_data = data_df.to_pandas()
        time.sleep(3)
        status.update(
            label="¡Descarga completada!", state="complete", expanded=False
        )
        return games_data

    
games_data = fetch_data()

The cache is working fine, fetch_data() function is not executing every time I reload the page, but the spinner keeps showing. I am using show_spinner=False but it doesn't work either as you can see in the next picture:

enter image description here

The table is showing but as you can see spinner is still there.

If you need to know, I am using an API Scrapper for the MLB API. get_data and get_data_df are methods of a class from API Scrapper that return a polars dataframe.


Solution

  • This is because show_spinner=False will only work if you are using st.spinner() and not st.status().

    You can recreate the same thing with a spinner as follows:

    @st.cache_data(show_spinner=False)
    def fetch_data():
        with st.spinner("Buscando datos..."):
            time.sleep(2)
            st.write("Partidos encontrados...")
            time.sleep(2)
            st.write("Descargando datos...")
            time.sleep(2)
            st.write("Dibujando tabla...")
            game_data = scraper.get_data(game_list_input=games_id)
            data_df = scraper.get_data_df(data_list=game_data)
            games_data = data_df.to_pandas()
            time.sleep(3)
            st.success("¡Descarga completada!")        
            return games_data
    
        
    games_data = fetch_data()
    

    You can tweak it a little to add an expander like we have in status, if required.