Skip to content

Commit fad4247

Browse files
committed
Restructured Jupyter widget
1 parent d8e23ea commit fad4247

15 files changed

Lines changed: 289 additions & 394 deletions

algorithmx/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from .jupyter import JupyterWidget
21
from ._version import __version__, version_info
32

43
from .nbextension import _jupyter_nbextension_paths
54

6-
from .main import http_server, jupyter_widget
5+
from .main import http_server, jupyter_client, jupyter_canvas

algorithmx/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version_info = (1, 0, 0, 'beta', 6)
1+
version_info = (1, 0, 0, 'beta', 7)
22
__version__ = '.'.join(map(str, version_info))

algorithmx/graphics/CanvasSelection.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,11 @@ def svgattr(self: S, key: str, value: ElementArg[Union[str, int, float, None]]):
220220
return self
221221

222222

223-
def canvas_selection(canvas: str, handler: EventHandler) -> CanvasSelection:
223+
def canvas_selection(canvas: str, handler: EventHandler,
224+
canvas_class: CanvasSelection = CanvasSelection) -> CanvasSelection:
224225
context = SelectionContext(handler)
225226
context.name = 'canvas'
226227
context.ids = [canvas]
227228
context.data = [canvas]
228229
handler.subscribe(lambda event: receive_handler(event, context.listeners))
229-
return CanvasSelection(context)
230+
return canvas_class(context)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from ..graphics import CanvasSelection, canvas_selection
2+
from IPython.display import display
3+
4+
class JupyterCanvas(CanvasSelection):
5+
def _ipython_display_(self):
6+
display(self._context.client)
7+
Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@
44
from ._frontend import module_name, module_version
55
import json
66

7-
from ..graphics import CanvasSelection, canvas_selection, DispatchEvent, ReceiveEvent
7+
from .JupyterCanvas import JupyterCanvas
8+
from ..graphics import CanvasSelection, canvas_selection, EventHandler, DispatchEvent, ReceiveEvent
89

9-
10-
class JupyterWidget(DOMWidget):
11-
_model_name = Unicode('CanvasModel').tag(sync=True)
10+
class JupyterClient(DOMWidget, EventHandler):
11+
_model_name = Unicode('AlgorithmxModel').tag(sync=True)
1212
_model_module = Unicode(module_name).tag(sync=True)
1313
_model_module_version = Unicode(module_version).tag(sync=True)
14-
_view_name = Unicode('CanvasView').tag(sync=True)
14+
_view_name = Unicode('AlgorithmxView').tag(sync=True)
1515
_view_module = Unicode(module_name).tag(sync=True)
1616
_view_module_version = Unicode(module_version).tag(sync=True)
1717

18-
_dispatch_events: SyncList = SyncList(Unicode, []).tag(sync=True)
19-
_show_buttons: SyncBool = SyncBool(False).tag(sync=True)
20-
2118
_subscriptions: List[Callable[[ReceiveEvent], Any]]
2219

20+
events: SyncList = SyncList(Unicode, []).tag(sync=True)
21+
show_buttons: SyncBool = SyncBool(False).tag(sync=True)
22+
2323
def __init__(self, **kwargs):
2424
super().__init__(**kwargs)
2525

2626
self._subscriptions = []
2727
if 'buttons' in kwargs:
28-
self._show_buttons = kwargs['buttons']
28+
self.show_buttons = kwargs['buttons']
2929

3030
def on_msg(widget, content, buffers):
3131
event = json.loads(content)
@@ -38,17 +38,10 @@ def on_msg(widget, content, buffers):
3838

3939
def dispatch(self, event: DispatchEvent):
4040
str_event = json.dumps(event)
41-
self._dispatch_events = self._dispatch_events + [str_event]
41+
self.events = self.events + [str_event]
4242

4343
def subscribe(self, listener: Callable[[ReceiveEvent], Any]):
4444
self._subscriptions.append(listener)
4545

46-
def canvas(self) -> CanvasSelection:
47-
"""
48-
Creates a new :class:`~graphics.CanvasSelection` which will dispatch and receive events through the Jupyter
49-
widget. The default canvas size is (400, 250).
50-
51-
Note that by default, you need to hold down the ``ctrl``/``cmd`` key to zoom in
52-
(see :meth:`~graphics.CanvasSelection.zoomkey`).
53-
"""
54-
return canvas_selection('_jupyter', self)
46+
def canvas(self) -> JupyterCanvas:
47+
return canvas_selection('_jupyter', self, JupyterCanvas)

algorithmx/jupyter/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from ..graphics import EventHandler, ReceiveEvent, DispatchEvent
22
from ..graphics import CanvasSelection
33

4-
from .JupyterWidget import JupyterWidget
4+
from .JupyterClient import JupyterClient
5+
from .JupyterCanvas import JupyterCanvas

algorithmx/jupyter/_frontend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
"""
44

55
module_name = 'algorithmx-jupyter'
6-
module_version = '^1.0.0'
6+
module_version = '^2.0.0'

algorithmx/main.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from .server import Server
2-
from .jupyter import JupyterWidget
2+
from .jupyter import JupyterClient, JupyterCanvas
3+
from .graphics import CanvasSelection
34

45
def http_server(file: str = None, port: int = 5050) -> Server:
56
"""
@@ -18,11 +19,14 @@ def http_server(file: str = None, port: int = 5050) -> Server:
1819
"""
1920
return Server(file, port)
2021

21-
def jupyter_widget(buttons: bool = False) -> JupyterWidget:
22+
def jupyter_client(buttons: bool = False) -> JupyterClient:
23+
return JupyterClient(buttons=buttons)
24+
25+
def jupyter_canvas(buttons: bool = False) -> JupyterCanvas:
2226
"""
23-
Creates a new Jupyter widget for displaying the network.
27+
Creates a new :class:`~graphics.CanvasSelection` which will dispatch and receive events through a Jupyter
28+
widget, and which can be displayed using the IPython ``display`` function.
2429
25-
:param buttons: Whether or not the widget should include buttons for starting/stopping/restarting events on the
26-
network (useful for algorithm simulations). Disabled by default.
30+
By default, the canvas size is (400, 250), and requires the ``ctrl``/``cmd`` to be held down while zooming.
2731
"""
28-
return JupyterWidget(buttons=buttons)
32+
return jupyter_client(buttons=buttons).canvas()

0 commit comments

Comments
 (0)