Skip to content

Commit 1c1a375

Browse files
committed
feat: deprecate use_thread
1 parent 6328ea7 commit 1c1a375

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
@@ -65,6 +65,7 @@ def _using_solara_server():
6565
from .autorouting import generate_routes, generate_routes_directory, RenderPage, RoutingProvider, DefaultLayout
6666
from .checks import check_jupyter
6767
from .scope import get_kernel_id, get_session_id
68+
from .tasks import task, use_task, Task, TaskResult
6869

6970

7071
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
@@ -2,8 +2,29 @@
22
from .components import * # noqa: F401, F403
33
from .utils import cookies, headers # noqa: F401, F403
44
from ..lifecycle import on_kernel_start # noqa: F401
5-
from ..tasks import task, use_task, Task, TaskResult # noqa: F401, F403
5+
from ..tasks import task as _task, use_task as _use_task, Task as _Task, TaskResult as _TaskResult
66
from ..toestand import computed # noqa: F401
7+
from ..util import deprecated
8+
9+
10+
@deprecated("solara.lab.task has been moved out of the lab namespace, use solara.task instead")
11+
def task(*args, **kwargs):
12+
_task(*args, **kwargs)
13+
14+
15+
@deprecated("solara.lab.use_task has been moved out of the lab namespace, use solara.use_task instead")
16+
def use_task(*args, **kwargs):
17+
return _use_task(*args, **kwargs)
18+
19+
20+
@deprecated("solara.lab.Task has been moved out of the lab namespace, use solara.Task instead")
21+
class Task(_Task):
22+
pass
23+
24+
25+
@deprecated("solara.lab.TaskResult has been moved out of the lab namespace, use solara.TaskResult instead")
26+
class TaskResult(_TaskResult):
27+
pass
728

829

930
def __getattr__(name):

solara/tasks.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def task(
532532
```solara
533533
import asyncio
534534
import solara
535-
from solara.lab import task
535+
from solara import task
536536
537537
@task
538538
async def fetch_data():
@@ -561,7 +561,7 @@ def Page():
561561
```solara
562562
import time
563563
import solara
564-
from solara.lab import task
564+
from solara import task
565565
566566
@task
567567
def fetch_data():
@@ -600,7 +600,7 @@ def Page():
600600
```solara
601601
import time
602602
import solara
603-
from solara.lab import task
603+
from solara import task
604604
605605
606606
@task
@@ -637,7 +637,7 @@ def Page():
637637
```solara
638638
import time
639639
import solara
640-
from solara.lab import task
640+
from solara import task
641641
642642
643643
@task
@@ -739,7 +739,7 @@ def use_task(
739739
def use_task(
740740
f: Union[None, Callable[[], R]] = None,
741741
*,
742-
dependencies: Union[None, List] = [],
742+
dependencies: Union[None, List] = None,
743743
raise_error=True,
744744
prefer_threaded=True,
745745
) -> Union[Callable[[Callable[[], R]], "Task[[], R]"], "Task[[], R]"]:
@@ -760,7 +760,7 @@ def use_task(
760760
```solara
761761
import time
762762
import solara
763-
from solara.lab import use_task, Task
763+
from solara import use_task, Task
764764
765765
766766
@solara.component
@@ -787,7 +787,7 @@ def square():
787787
```solara
788788
import asyncio
789789
import solara
790-
from solara.lab import use_task, Task
790+
from solara import use_task, Task
791791
792792
793793
@solara.component
@@ -809,7 +809,7 @@ async def square():
809809
## Arguments
810810
811811
- `f`: The function or coroutine to run as a task.
812-
- `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`
812+
- `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`.
813813
- `raise_error`: If true, an error in the task will be raised. If false, the error should be handled by the
814814
user and is available in the `.exception` attribute of the task result object.
815815
- `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)