import asyncio
import solara
import solara.lab
clicks = solara.reactive(0)
async def run():
await asyncio.sleep(1)
return 42
def use_call_on_change(f, reactive_var):
"""Reusable hook that will call f, when the value of reactive_var changes"""
def link():
def change_handler(new_value):
print("new value", new_value)
f()
print("subscribed")
return reactive_var.subscribe(change_handler)
solara.use_effect(link, [reactive_var])
@solara.component
def Page():
# pass depenendencies=None to not auto-run the task, see:
# https://solara.dev/documentation/components/lab/use_task
task = solara.lab.use_task(run, dependencies=None)
# start the task, which clicks changes
use_call_on_change(task, clicks)
def increment():
clicks.value += 1
print("clicks", clicks) # noqa
solara.Button(label=f"Clicked: {clicks}", on_click=increment)
if task.finished:
solara.Success(f"Result of task == {task.value}")
if task.not_called:
solara.Info("never called")
solara.ProgressLinear(task.pending)