Skip to main content
Solved

Streamlit spinner is not working

  • November 25, 2024
  • 4 replies
  • 37 views

Hi,

I am using the streamlit provided by CDF for app deployment, I have added the spinner for long running tasks in my application, but the spinner is not coming in the CDF streamlit app.  Do you have any input on this?

 

Thanks

Best answer by Anders Hafreager

No that won’t help I think, but inside the `long_running_function()`, if you have a for loop over many things, you can do a small 

await asyncio.sleep(0.01)

inside the loop to allow just a little bit of time to do something else (e.g. render spinner etc). If you can share a full app that shows this problem, I’m happy to take a look!

View original
Did this topic help you find an answer to your question?

4 replies

Anders Hafreager
Practitioner

Hi, thanks for using Streamlit!

The reason this happens is that the thread is blocking the update and the spinner will not work I think. Could you try to do the following in between blocking work?

import asyncio
await asyncio.sleep(0.1)

and see if that helps? (import on top of course)


  • Author
  • Seasoned
  • 22 replies
  • November 25, 2024

Hi ​@Anders Hafreager,

Do you mean something like this- 

with st.spinner("loading...."):
    await asyncio.sleep(0.5)
    res = long_running_function()
    st.write(res)

 


Anders Hafreager
Practitioner

No that won’t help I think, but inside the `long_running_function()`, if you have a for loop over many things, you can do a small 

await asyncio.sleep(0.01)

inside the loop to allow just a little bit of time to do something else (e.g. render spinner etc). If you can share a full app that shows this problem, I’m happy to take a look!


  • Author
  • Seasoned
  • 22 replies
  • November 25, 2024

I tried this code - 

import streamlit as st
import asyncio
import time
from cognite.client import CogniteClient
st.title("An example app in CDF")
client = CogniteClient()

@st.cache_data
def get_assets():
  assets = client.assets.list(limit=None).to_pandas()
  assets = assets.fillna(0)
  return assets

with st.spinner("loading...."):
    await asyncio.sleep(0.5)
    time.sleep(2)
    res = get_assets()
    st.write(res)

and it worked for me.
 


 I tried on my local machine without the `await asyncio.sleep(0.01)` I can see the spinner. Could you please help me to understand why samethings doesn’t work after integrate in CDF streamlit app development space?
 

Thanks!


Reply


Cookie Policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie Settings