I read somewhere, if there’s frequent updates to something and you have a loading indicator show up every time for 0.1 seconds, it might be really distracting.
Here, I implemented something that makes the spinner show up only if some duration threshold has been exceeded
import asyncio
import panel as pn
pn.extension()
async def long_running_task():
# Simulate a long-running task
await asyncio.sleep(4)
return "Task completed!"
async def run_task(clicked,):
if not clicked:
return
task = asyncio.create_task(long_running_task())
# only show the spinner 0.5 seconds after
duration_threshold = 0.5
start = asyncio.get_event_loop().time()
while not task.done():
duration = asyncio.get_event_loop().time() - start
if duration > duration_threshold:
yield pn.indicators.LoadingSpinner(value=True, name="Loading...")
break
await asyncio.sleep(0.05)
await task
yield task.result()
run = pn.widgets.Button(name="Run Task")
result = pn.bind(run_task, run)
row = pn.Row(run, result)
row
Initially I have it at 4 seconds so spinner shows; after, I change it to 0.25 seconds so spinner doesn’t show