Skip to content

Commit

Permalink
feat: deprecate use_thread
Browse files Browse the repository at this point in the history
  • Loading branch information
iisakkirotko committed Dec 27, 2024
1 parent 202ce98 commit f16af91
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 16 deletions.
1 change: 1 addition & 0 deletions solara/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def _using_solara_server():
from .autorouting import generate_routes, generate_routes_directory, RenderPage, RoutingProvider, DefaultLayout
from .checks import check_jupyter
from .scope import get_kernel_id, get_session_id
from .tasks import task, use_task, Task, TaskResult


def display(*objs, **kwargs):
Expand Down
3 changes: 2 additions & 1 deletion solara/hooks/use_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@

import solara
from solara.datatypes import Result, ResultState
from solara.util import cancel_guard, nullcontext
from solara.util import cancel_guard, nullcontext, deprecated

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


@deprecated("`use_thread` is deprecated, use the more modern and performant `use_task` or `Task` instead", category=FutureWarning)
def use_thread(
callback: Union[
Callable[[threading.Event], T],
Expand Down
23 changes: 22 additions & 1 deletion solara/lab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
)
from .utils import cookies, headers
from ..lifecycle import on_kernel_start
from ..tasks import task, use_task, Task, TaskResult
from ..tasks import task as _task, use_task as _use_task, Task as _Task, TaskResult as _TaskResult
from ..toestand import computed
from ..util import deprecated


__all__ = [
Expand Down Expand Up @@ -49,6 +50,26 @@
]


@deprecated("solara.lab.task has been moved out of the lab namespace, use solara.task instead")
def task(*args, **kwargs):
_task(*args, **kwargs)


@deprecated("solara.lab.use_task has been moved out of the lab namespace, use solara.use_task instead")
def use_task(*args, **kwargs):
return _use_task(*args, **kwargs)


@deprecated("solara.lab.Task has been moved out of the lab namespace, use solara.Task instead")
class Task(_Task):
pass


@deprecated("solara.lab.TaskResult has been moved out of the lab namespace, use solara.TaskResult instead")
class TaskResult(_TaskResult):
pass


def __getattr__(name):
# for backwards compatibility
from solara.components.cross_filter import ( # noqa: F401
Expand Down
16 changes: 8 additions & 8 deletions solara/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def task(
```solara
import asyncio
import solara
from solara.lab import task
from solara import task
@task
async def fetch_data():
Expand Down Expand Up @@ -561,7 +561,7 @@ def Page():
```solara
import time
import solara
from solara.lab import task
from solara import task
@task
def fetch_data():
Expand Down Expand Up @@ -600,7 +600,7 @@ def Page():
```solara
import time
import solara
from solara.lab import task
from solara import task
@task
Expand Down Expand Up @@ -637,7 +637,7 @@ def Page():
```solara
import time
import solara
from solara.lab import task
from solara import task
@task
Expand Down Expand Up @@ -739,7 +739,7 @@ def use_task(
def use_task(
f: Union[None, Callable[[], R]] = None,
*,
dependencies: Union[None, List] = [],
dependencies: Union[None, List] = None,
raise_error=True,
prefer_threaded=True,
) -> Union[Callable[[Callable[[], R]], "Task[[], R]"], "Task[[], R]"]:
Expand All @@ -760,7 +760,7 @@ def use_task(
```solara
import time
import solara
from solara.lab import use_task, Task
from solara import use_task, Task
@solara.component
Expand All @@ -787,7 +787,7 @@ def square():
```solara
import asyncio
import solara
from solara.lab import use_task, Task
from solara import use_task, Task
@solara.component
Expand All @@ -809,7 +809,7 @@ async def square():
## Arguments
- `f`: The function or coroutine to run as a task.
- `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`
- `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`.
- `raise_error`: If true, an error in the task will be raised. If false, the error should be handled by the
user and is available in the `.exception` attribute of the task result object.
- `prefer_threaded` - bool: Will run coroutine functions as a task in a thread when threads are available.
Expand Down
31 changes: 31 additions & 0 deletions solara/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union
from urllib.parse import urlencode
import warnings

if TYPE_CHECKING:
import numpy as np
Expand Down Expand Up @@ -346,3 +347,33 @@ def wrapper():
return return_value

return wrapper


def deprecated(reason: str, category=DeprecationWarning):
"""
Mark functions as deprecated. When the function is called, a warning is shown with the provided reason.
Parameters
----------
reason : str
The message to display when the deprecated function is used.
category : type, optional
The warning category to use, defaults to DeprecationWarning. Use DeprecationWarning when users do not need to see
the warning, and FutureWarning when users should see the warning.
"""

def decorator(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
warnings.warn(
reason,
category=category,
stacklevel=2, # this way we show the line where the function is called
)
return func(*args, **kwargs)

return wrapped

return decorator
2 changes: 2 additions & 0 deletions solara/website/pages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@
"/api/widget": "/documentation/api/utilities/widget",
"/api/default_layout": "/documentation/components/layout",
"/api/title": "/documentation/components/page/title",
"/documentation/components/lab/use_task": "/documentation/api/hooks/use_task",
"/documentation/components/lab/task": "/documentation/api/utilities/task",
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import solara
import solara.autorouting
import solara.lab
from solara.website.components import NoPage
from solara.website.utils import apidoc

title = "use_task"
Page = NoPage
__doc__ += apidoc(solara.lab.use_task) # type: ignore
__doc__ += apidoc(solara.use_task) # type: ignore
4 changes: 3 additions & 1 deletion solara/website/pages/documentation/api/hooks/use_thread.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# use_thread
# use_thread (deprecated)

### use_thread is deprecated, use [use_task](/documentation/api/hooks/use_task) or [Task](/documentation/api/utilities/task) instead

```python
def use_thread(
Expand Down
2 changes: 1 addition & 1 deletion solara/website/pages/documentation/api/hooks/use_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from solara.alias import rw

HERE = Path(__file__).parent
title = "use_thread"
title = "use_thread (deprecated)"
__doc__ = open(HERE / "use_thread.md").read()


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import solara
import solara.autorouting
import solara.lab
from solara.website.components import NoPage
from solara.website.utils import apidoc

title = "Task"
Page = NoPage
__doc__ += apidoc(solara.lab.task) # type: ignore
__doc__ += apidoc(solara.task) # type: ignore

0 comments on commit f16af91

Please sign in to comment.