Skip to content

Commit

Permalink
Drop usage of NotifyOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Fleming committed Sep 16, 2024
1 parent f5c34d7 commit f33d2d0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 28 deletions.
8 changes: 4 additions & 4 deletions examples/widgets.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@
"metadata": {},
"outputs": [],
"source": [
"t = app.notification.notify(\"Updating soon\", ipylab.NotificationType.progress, autoClose=False)"
"t = app.notification.notify(\"Updating soon\", ipylab.NotificationType.progress, auto_close=False)"
]
},
{
Expand All @@ -737,7 +737,7 @@
"async def update():\n",
" for i in range(1, 11):\n",
" await n.update(f\"Updating {10-i}\")\n",
" await n.update(\"All done\", type=ipylab.NotificationType.success, autoClose=1000)\n",
" await n.update(\"All done\", type=ipylab.NotificationType.success, auto_close=1000)\n",
" await asyncio.sleep(2000)\n",
" n.dispose()\n",
"\n",
Expand All @@ -751,7 +751,7 @@
"source": [
"### Notification Actions\n",
"\n",
"Actions appear as a button in the notification. Each action has a callback to a python function. Here the callback executes a command in Jupyterlab."
"Actions appear as a button in the notification. Each action has a callback to a python function. Here each callback executes a command in Jupyterlab."
]
},
{
Expand Down Expand Up @@ -779,7 +779,7 @@
" },\n",
" ],\n",
" type=ipylab.NotificationType.info,\n",
" autoClose=False,\n",
" auto_close=False,\n",
")"
]
}
Expand Down
60 changes: 37 additions & 23 deletions ipylab/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from ipywidgets import TypedTuple
from traitlets import Container, Instance, Unicode

from ipylab._compat.typing import NotRequired, TypedDict, Unpack
from ipylab._compat.typing import NotRequired, TypedDict
from ipylab.asyncwidget import AsyncWidgetBase, Transform, pack, register
from ipylab.common import NotificationType
from ipylab.connection import Connection
Expand All @@ -30,11 +30,6 @@ class NotifyAction(TypedDict):
caption: NotRequired[str]


class NotifyOptions(TypedDict):
autoClose: float | Literal[False] # noqa: N815
actions: NotRequired[Iterable[NotifyAction | ActionConnection]]


class ActionConnection(Connection):
CID_PREFIX = "ipylab action"
callback = traitlets.Callable()
Expand All @@ -53,19 +48,26 @@ def update(
self,
message: str,
type: NotificationType | None = None, # noqa: A002
**options: Unpack[NotifyOptions],
*,
auto_close: float | Literal[False] | None = None,
actions: Iterable[NotifyAction | ActionConnection] = (),
) -> Task[bool]:
args = {"id": self.id, "message": message, "type": NotificationType(type) if type else None}
args = {
"id": self.id,
"message": message,
"type": NotificationType(type) if type else None,
"autoClose": auto_close,
}

async def update():
actions = [await self.app.notification._ensure_action(v) for v in options.get("actions", ())] # noqa: SLF001
if actions:
options["actions"] = list(map(pack, actions)) # type: ignore
to_object = [f"options.actions.{i}" for i in range(len(actions))]
actions_ = [await self.app.notification._ensure_action(v) for v in actions] # noqa: SLF001
if actions_:
args["actions"] = list(map(pack, actions_)) # type: ignore
to_object = [f"options.actions.{i}" for i in range(len(actions_))]
else:
to_object = None
result = await self.app.notification.execute_method("update", args | options, toObject=to_object)
for action in actions:
result = await self.app.notification.execute_method("update", args, toObject=to_object)
for action in actions_:
await self._add_to_tuple_trait("actions", action)
return result

Expand Down Expand Up @@ -102,20 +104,32 @@ def notify(
self,
message: str,
type: NotificationType = NotificationType.default, # noqa: A002
**options: Unpack[NotifyOptions],
*,
auto_close: float | Literal[False] | None = None,
actions: Iterable[NotifyAction | ActionConnection] = (),
) -> Task[NotificationConnection]:
"""Create a new notification.
To update a notification use the update method of the returned `NotificationConnection`.
NotifyAction:
label: str
display_type: Literal["default", "accent", "warn", "link"]
callback: Callable[[], Any]
keep_open: NotRequired[bool]
caption: NotRequired[str]
"""

options = {
"message": message,
"type": NotificationType(type) if type else None,
"autoClose": auto_close,
}

async def notify():
actions = [await self._ensure_action(v) for v in options.get("actions", ())]
if actions:
options["actions"] = list(map(pack, actions)) # type: ignore
to_object = [f"options.actions.{i}" for i in range(len(actions))]
else:
to_object = None
actions_ = [await self._ensure_action(v) for v in actions]
if actions_:
options["actions"] = list(map(pack, actions_)) # type: ignore
notification: NotificationConnection = await self.schedule_operation(
"notification",
type=NotificationType(type),
Expand All @@ -126,9 +140,9 @@ async def notify():
"cid": NotificationConnection.new_cid(),
"auto_dispose": True,
},
toObject=to_object,
toObject=[f"options.actions.{i}" for i in range(len(actions_))] if actions_ else None,
)
for action in actions:
for action in actions_:
await notification._add_to_tuple_trait("actions", action) # noqa: SLF001
await self._add_to_tuple_trait("notifications", notification)
return notification
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ipylab",
"version": "2.0.0-b2",
"version": "2.0.0-b3",
"description": "Control JupyterLab from Python notebooks",
"keywords": [
"jupyter",
Expand Down

0 comments on commit f33d2d0

Please sign in to comment.