Skip to content

Commit

Permalink
make use_task with overload
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels authored and iisakkirotko committed Feb 5, 2024
1 parent c96bd2c commit 27b701b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 10 deletions.
1 change: 1 addition & 0 deletions solara/lab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .components import * # noqa: F401, F403
from ..server.kernel_context import on_kernel_start # noqa: F401
from ..tasks import task, use_task, Task # noqa: F401, F403
from ..toestand import computed # noqa: F401


def __getattr__(name):
Expand Down
43 changes: 33 additions & 10 deletions solara/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,23 @@ def create_task():
return wrapper(function)


def use_task(f: Callable[P, R], dependencies=[], *, raise_error=True) -> Union[Task[P, R], solara.Result[R]]:
@overload
def use_task(
f: None = None,
) -> Callable[[Callable[[], R]], solara.Result[R]]:
...


@overload
def use_task(
f: Callable[P, R],
) -> solara.Result[R]:
...


def use_task(
f: Union[None, Callable[[], R]] = None, dependencies=[], *, raise_error=True
) -> Union[Callable[[Callable[[], R]], solara.Result[R]], solara.Result[R]]:
"""Run a function or coroutine as a task and return the result.
## Example
Expand Down Expand Up @@ -398,14 +414,21 @@ async def square():
"""
task_instance = solara.use_memo(lambda: task(f), dependencies=dependencies)

def run():
task_instance()
return task_instance.cancel
def wrapper(f):
task_instance = solara.use_memo(lambda: task(f), dependencies=dependencies)

def run():
task_instance()
return task_instance.cancel

solara.use_effect(run, dependencies=dependencies)
if raise_error:
if task_instance.state == solara.ResultState.ERROR and task_instance.error is not None:
raise task_instance.error
return task_instance.result.value
solara.use_effect(run, dependencies=dependencies)
if raise_error:
if task_instance.state == solara.ResultState.ERROR and task_instance.error is not None:
raise task_instance.error
return task_instance.result.value

if f is None:
return wrapper
else:
return wrapper(f)
66 changes: 66 additions & 0 deletions solara/toestand.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,72 @@ def __repr__(self):
return "<Computed" + value[len("<Reactive") : -1]


@overload
def computed(
f: None,
*,
key: Optional[str] = ...,
) -> Callable[[Callable[[], T]], Reactive[T]]:
...


@overload
def computed(
f: Callable[[], T],
*,
key: Optional[str] = ...,
) -> Reactive[T]:
...


def computed(
f: Union[None, Callable[[], T]],
*,
key: Optional[str] = None,
) -> Union[Callable[[Callable[[], T]], Reactive[T]], Reactive[T]]:
"""Creates a reactive variable that is set to the return value of the function.
The value will be updated when any of the reactive variables used in the function
change.
## Example
```solara
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)
```
"""

def wrapper(f: Callable[[], T]):
return Computed(f, key=key)

if f is None:
return wrapper
else:
return wrapper(f)


class ValueSubField(ValueBase[T]):
def __init__(self, field: "FieldBase"):
super().__init__() # type: ignore
Expand Down

0 comments on commit 27b701b

Please sign in to comment.