computed
Creates a reactive variable that is set to the return value of the function.
def computed(
f: Union[None, Callable[[], T]],
*,
key: Optional[str] = None,
) -> Union[Callable[[Callable[[], T]], Reactive[T]], Reactive[T]]:
...
The value will be updated when any of the reactive variables used in the function change.
Example
import solara
import solara.lab
a = solara.reactive(1)
b = solara.reactive(2)
@solara.lab.computed
def total():
return a.value + b.value
def reset():
a.value = 1
b.value = 2
@solara.component
def Page():
print(a, b, total)
solara.IntSlider("a", value=a)
solara.IntSlider("b", value=b)
solara.Text(f"a + b = {total.value}")
solara.Button("reset", on_click=reset)
1
2
Live output