Skip to content

Commit 6bfa775

Browse files
committed
feat: deprecate use_thread
1 parent 5b8ba9c commit 6bfa775

File tree

10 files changed

+72
-16
lines changed

10 files changed

+72
-16
lines changed

solara/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def _using_solara_server():
6363
from .autorouting import generate_routes, generate_routes_directory, RenderPage, RoutingProvider, DefaultLayout
6464
from .checks import check_jupyter
6565
from .scope import get_kernel_id, get_session_id
66+
from .tasks import task, use_task, Task, TaskResult
6667

6768

6869
def display(*objs, **kwargs):

solara/hooks/use_thread.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77

88
import solara
99
from solara.datatypes import Result, ResultState
10-
from solara.util import cancel_guard, nullcontext
10+
from solara.util import cancel_guard, nullcontext, deprecated
1111

1212
SOLARA_ALLOW_OTHER_TRACER = os.environ.get("SOLARA_ALLOW_OTHER_TRACER", False) in (True, "True", "true", "1")
1313
T = TypeVar("T")
1414
logger = logging.getLogger("solara.hooks.use_thread")
1515

1616

17+
@deprecated("`use_thread` is deprecated, use the more modern and performant `use_task` or `Task` instead", category=FutureWarning)
1718
def use_thread(
1819
callback: Union[
1920
Callable[[threading.Event], T],

solara/lab/__init__.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
)
1919
from .utils import cookies, headers
2020
from ..lifecycle import on_kernel_start
21-
from ..tasks import task, use_task, Task, TaskResult
21+
from ..tasks import task as _task, use_task as _use_task, Task as _Task, TaskResult as _TaskResult
2222
from ..toestand import computed
23+
from ..util import deprecated
2324

2425

2526
__all__ = [
@@ -49,6 +50,26 @@
4950
]
5051

5152

53+
@deprecated("solara.lab.task has been moved out of the lab namespace, use solara.task instead")
54+
def task(*args, **kwargs):
55+
_task(*args, **kwargs)
56+
57+
58+
@deprecated("solara.lab.use_task has been moved out of the lab namespace, use solara.use_task instead")
59+
def use_task(*args, **kwargs):
60+
return _use_task(*args, **kwargs)
61+
62+
63+
@deprecated("solara.lab.Task has been moved out of the lab namespace, use solara.Task instead")
64+
class Task(_Task):
65+
pass
66+
67+
68+
@deprecated("solara.lab.TaskResult has been moved out of the lab namespace, use solara.TaskResult instead")
69+
class TaskResult(_TaskResult):
70+
pass
71+
72+
5273
def __getattr__(name):
5374
# for backwards compatibility
5475
from solara.components.cross_filter import ( # noqa: F401

solara/tasks.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ def task(
543543
```solara
544544
import asyncio
545545
import solara
546-
from solara.lab import task
546+
from solara import task
547547
548548
@task
549549
async def fetch_data():
@@ -572,7 +572,7 @@ def Page():
572572
```solara
573573
import time
574574
import solara
575-
from solara.lab import task
575+
from solara import task
576576
577577
@task
578578
def fetch_data():
@@ -611,7 +611,7 @@ def Page():
611611
```solara
612612
import time
613613
import solara
614-
from solara.lab import task
614+
from solara import task
615615
616616
617617
@task
@@ -648,7 +648,7 @@ def Page():
648648
```solara
649649
import time
650650
import solara
651-
from solara.lab import task
651+
from solara import task
652652
653653
654654
@task
@@ -750,7 +750,7 @@ def use_task(
750750
def use_task(
751751
f: Union[None, Callable[[], R]] = None,
752752
*,
753-
dependencies: Union[None, List] = [],
753+
dependencies: Union[None, List] = None,
754754
raise_error=True,
755755
prefer_threaded=True,
756756
) -> Union[Callable[[Callable[[], R]], "Task[[], R]"], "Task[[], R]"]:
@@ -771,7 +771,7 @@ def use_task(
771771
```solara
772772
import time
773773
import solara
774-
from solara.lab import use_task, Task
774+
from solara import use_task, Task
775775
776776
777777
@solara.component
@@ -798,7 +798,7 @@ def square():
798798
```solara
799799
import asyncio
800800
import solara
801-
from solara.lab import use_task, Task
801+
from solara import use_task, Task
802802
803803
804804
@solara.component
@@ -820,7 +820,7 @@ async def square():
820820
## Arguments
821821
822822
- `f`: The function or coroutine to run as a task.
823-
- `dependencies`: A list of dependencies that will trigger a rerun of the task when changed, the task will run automatically execute when the `dependencies=None`
823+
- `dependencies`: A list of dependencies that will trigger a rerun of the task when changed, the task will not automatically execute when the `dependencies=None`.
824824
- `raise_error`: If true, an error in the task will be raised. If false, the error should be handled by the
825825
user and is available in the `.exception` attribute of the task result object.
826826
- `prefer_threaded` - bool: Will run coroutine functions as a task in a thread when threads are available.

solara/util.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from pathlib import Path
1212
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union
1313
from urllib.parse import urlencode
14+
import warnings
1415

1516
if TYPE_CHECKING:
1617
import numpy as np
@@ -346,3 +347,33 @@ def wrapper():
346347
return return_value
347348

348349
return wrapper
350+
351+
352+
def deprecated(reason: str, category=DeprecationWarning):
353+
"""
354+
Mark functions as deprecated. When the function is called, a warning is shown with the provided reason.
355+
356+
Parameters
357+
----------
358+
359+
reason : str
360+
The message to display when the deprecated function is used.
361+
category : type, optional
362+
The warning category to use, defaults to DeprecationWarning. Use DeprecationWarning when users do not need to see
363+
the warning, and FutureWarning when users should see the warning.
364+
365+
"""
366+
367+
def decorator(func):
368+
@functools.wraps(func)
369+
def wrapped(*args, **kwargs):
370+
warnings.warn(
371+
reason,
372+
category=category,
373+
stacklevel=2, # this way we show the line where the function is called
374+
)
375+
return func(*args, **kwargs)
376+
377+
return wrapped
378+
379+
return decorator

solara/website/pages/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@
209209
"/api/widget": "/documentation/api/utilities/widget",
210210
"/api/default_layout": "/documentation/components/layout",
211211
"/api/title": "/documentation/components/page/title",
212+
"/documentation/components/lab/use_task": "/documentation/api/hooks/use_task",
213+
"/documentation/components/lab/task": "/documentation/api/utilities/task",
212214
}
213215

214216

solara/website/pages/documentation/components/lab/use_task.py renamed to solara/website/pages/documentation/api/hooks/use_task.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import solara
44
import solara.autorouting
5-
import solara.lab
65
from solara.website.components import NoPage
76
from solara.website.utils import apidoc
87

98
title = "use_task"
109
Page = NoPage
11-
__doc__ += apidoc(solara.lab.use_task) # type: ignore
10+
__doc__ += apidoc(solara.use_task) # type: ignore

solara/website/pages/documentation/api/hooks/use_thread.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# use_thread
1+
# use_thread (deprecated)
2+
3+
### use_thread is deprecated, use [use_task](/documentation/api/hooks/use_task) or [Task](/documentation/api/utilities/task) instead
24

35
```python
46
def use_thread(

solara/website/pages/documentation/api/hooks/use_thread.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from solara.alias import rw
77

88
HERE = Path(__file__).parent
9-
title = "use_thread"
9+
title = "use_thread (deprecated)"
1010
__doc__ = open(HERE / "use_thread.md").read()
1111

1212

solara/website/pages/documentation/components/lab/task.py renamed to solara/website/pages/documentation/api/utilities/task.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import solara
44
import solara.autorouting
5-
import solara.lab
65
from solara.website.components import NoPage
76
from solara.website.utils import apidoc
87

98
title = "Task"
109
Page = NoPage
11-
__doc__ += apidoc(solara.lab.task) # type: ignore
10+
__doc__ += apidoc(solara.task) # type: ignore

0 commit comments

Comments
 (0)