-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from davidbrochart/base-class
Create base Widget class, CommProvider and CommWidget
- Loading branch information
Showing
8 changed files
with
127 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,6 @@ authors = [ | |
keywords = [ | ||
"widgets", | ||
"jupyter", | ||
"ypy", | ||
"yjs", | ||
] | ||
dependencies = [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
from .ypywidgets import Widget # noqa | ||
from .reactive import reactive # noqa | ||
from .widget import Widget as Widget | ||
from .reactive import reactive as reactive | ||
|
||
|
||
__version__ = "0.5.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
from __future__ import annotations | ||
|
||
import comm | ||
from pycrdt import Doc, Text, TransactionEvent | ||
|
||
from .utils import ( | ||
YMessageType, | ||
YSyncMessageType, | ||
create_update_message, | ||
process_sync_message, | ||
sync, | ||
) | ||
from .widget import Widget | ||
|
||
|
||
def create_widget_comm( | ||
data: dict | None = None, | ||
metadata: dict | None = None, | ||
comm_id: str | None = None, | ||
) -> comm.base_comm.BaseComm: | ||
_comm = comm.create_comm( | ||
comm_id=comm_id, | ||
target_name="ywidget", | ||
data=data, | ||
metadata=metadata, | ||
) | ||
return _comm | ||
|
||
|
||
class CommProvider: | ||
def __init__( | ||
self, | ||
ydoc: Doc, | ||
comm: comm.base_comm.BaseComm, | ||
) -> None: | ||
self._ydoc = ydoc | ||
self._comm = comm | ||
msg = sync(ydoc) | ||
self._comm.send(**msg) | ||
self._comm.on_msg(self._receive) | ||
|
||
def _receive(self, msg): | ||
message = bytes(msg["buffers"][0]) | ||
if message[0] == YMessageType.SYNC: | ||
reply = process_sync_message(message[1:], self._ydoc) | ||
if reply: | ||
self._comm.send(buffers=[reply]) | ||
if message[1] == YSyncMessageType.SYNC_STEP2: | ||
self._ydoc.observe(self._send) | ||
|
||
def _send(self, event: TransactionEvent): | ||
update = event.get_update() | ||
message = create_update_message(update) | ||
self._comm.send(buffers=[message]) | ||
|
||
|
||
class CommWidget(Widget): | ||
def __init__( | ||
self, | ||
ydoc: Doc | None = None, | ||
comm_data: dict | None = None, | ||
comm_metadata: dict | None = None, | ||
comm_id: str | None = None, | ||
): | ||
super().__init__(ydoc) | ||
model_name = self.__class__.__name__ | ||
_model_name = self.ydoc["_model_name"] = Text() | ||
_model_name += model_name | ||
if comm_metadata is None: | ||
comm_metadata = dict( | ||
ymodel_name=model_name, | ||
create_ydoc=not ydoc, | ||
) | ||
self._comm = create_widget_comm(comm_data, comm_metadata, comm_id) | ||
CommProvider(self.ydoc, self._comm) | ||
|
||
def _repr_mimebundle_(self, **kwargs): | ||
plaintext = repr(self) | ||
if len(plaintext) > 110: | ||
plaintext = plaintext[:110] + '…' | ||
data = { | ||
"text/plain": plaintext, | ||
"application/vnd.jupyter.ywidget-view+json": { | ||
"version_major": 2, | ||
"version_minor": 0, | ||
"model_id": self._comm.comm_id, | ||
} | ||
} | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from __future__ import annotations | ||
|
||
from pycrdt import Doc, Map, Text | ||
|
||
|
||
class Widget: | ||
_initialized = False | ||
_attrs: Map | ||
ydoc: Doc | ||
|
||
def __init__(self, ydoc: Doc | None = None) -> None: | ||
if self._initialized: | ||
return | ||
self._initialized = True | ||
self.ydoc = Doc() if ydoc is None else ydoc | ||
self.ydoc["_attrs"] = self._attrs = Map() | ||
self.ydoc["_model_name"] = Text() | ||
self._attrs.observe(self._set_attr) | ||
|
||
def _set_attr(self, event): | ||
for k, v in event.keys.items(): | ||
new_value = v["newValue"] | ||
if getattr(self, k) != new_value: | ||
setattr(self, k, new_value) |
This file was deleted.
Oops, something went wrong.