class WidgetCommSocket(CommSocket): """ CustomCommSocket provides communication between the IPython kernel and a matplotlib canvas element in the notebook. A CustomCommSocket is required to delay communication between the kernel and the canvas element until the widget has been rendered in the notebook. """ def __init__(self, manager): self.supports_binary = None self.manager = manager self.uuid = str(uuid.uuid4()) self.html = "<div id=%r></div>" % self.uuid def start(self): try: # Jupyter/IPython 4.0 from ipykernel.comm import Comm except: # IPython <=3.0 from IPython.kernel.comm import Comm try: self.comm = Comm('matplotlib', data={'id': self.uuid}) except AttributeError: raise RuntimeError('Unable to create an IPython notebook Comm ' 'instance. Are you in the IPython notebook?') self.comm.on_msg(self.on_message) self.comm.on_close(lambda close_message: self.manager.clearup_closed())
class WidgetCommSocket(CommSocket): """ CustomCommSocket provides communication between the IPython kernel and a matplotlib canvas element in the notebook. A CustomCommSocket is required to delay communication between the kernel and the canvas element until the widget has been rendered in the notebook. """ def __init__(self, manager): self.supports_binary = None self.manager = manager self.uuid = str(uuid.uuid4()) self.html = "<div id=%r></div>" % self.uuid def start(self): try: # Jupyter/IPython 4.0 from ipykernel.comm import Comm except: # IPython <=3.0 from IPython.kernel.comm import Comm try: self.comm = Comm('matplotlib', data={'id': self.uuid}) except AttributeError: raise RuntimeError('Unable to create an IPython notebook Comm ' 'instance. Are you in the IPython notebook?') self.comm.on_msg(self.on_message) self.comm.on_close(lambda close_message: self.manager.clearup_closed())
class NbAggCommSocket(CommSocket): """ NbAggCommSocket subclasses the matplotlib CommSocket allowing the opening of a comms channel to be delayed until the plot is displayed. """ def __init__(self, manager, target=None): self.supports_binary = None self.manager = manager self.target = uuid.uuid4().hex if target is None else target self.html = "<div id=%r></div>" % self.target def start(self): try: # Jupyter/IPython 4.0 from ipykernel.comm import Comm except: # IPython <=3.0 from IPython.kernel.comm import Comm try: self.comm = Comm('matplotlib', data={'id': self.target}) except AttributeError: raise RuntimeError('Unable to create an IPython notebook Comm ' 'instance. Are you in the IPython notebook?') self.comm.on_msg(self.on_message) self.comm.on_close(lambda close_message: self.manager.clearup_closed())
class NbAggCommSocket(CommSocket): """ NbAggCommSocket subclasses the matplotlib CommSocket allowing the opening of a comms channel to be delayed until the plot is displayed. """ def __init__(self, manager, target=None): self.supports_binary = None self.manager = manager self.target = uuid.uuid4().hex if target is None else target self.html = "<div id=%r></div>" % self.target def start(self): try: # Jupyter/IPython 4.0 from ipykernel.comm import Comm except: # IPython <=3.0 from IPython.kernel.comm import Comm try: self.comm = Comm('matplotlib', data={'id': self.target}) except AttributeError: raise RuntimeError('Unable to create an IPython notebook Comm ' 'instance. Are you in the IPython notebook?') self.comm.on_msg(self.on_message) self.comm.on_close(lambda close_message: self.manager.clearup_closed())
def _open_comm(comm: Comm, msg): _set_name_hook(lambda display_name, display_id: _send_new_display_handle( comm, display_name, display_id)) _set_new_part_hook(lambda part_name, part_cls: _send_part( comm, part_name, part_cls.get_metadata())) comm.on_msg(lambda msg: _on_msg(comm, msg)) comm.on_close(_close_comm)
class CommSocket: """ Manages the Comm connection between IPython and the browser (client). Comms are 2 way, with the CommSocket being able to publish a message via the send_json method, and handle a message with on_message. On the JS side figure.send_message and figure.ws.onmessage do the sending and receiving respectively. """ def __init__(self, manager): self.supports_binary = None self.manager = manager self.uuid = str(uuid.uuid4()) # Publish an output area with a unique ID. The javascript can then # hook into this area. display(HTML("<div id=%r></div>" % self.uuid)) try: self.comm = Comm('matplotlib', data={'id': self.uuid}) except AttributeError as err: raise RuntimeError('Unable to create an IPython notebook Comm ' 'instance. Are you in the IPython ' 'notebook?') from err self.comm.on_msg(self.on_message) manager = self.manager self._ext_close = False def _on_close(close_message): self._ext_close = True manager.remove_comm(close_message['content']['comm_id']) manager.clearup_closed() self.comm.on_close(_on_close) def is_open(self): return not (self._ext_close or self.comm._closed) def on_close(self): # When the socket is closed, deregister the websocket with # the FigureManager. if self.is_open(): try: self.comm.close() except KeyError: # apparently already cleaned it up? pass def send_json(self, content): self.comm.send({'data': json.dumps(content)}) def send_binary(self, blob): if self.supports_binary: self.comm.send({'blob': 'image/png'}, buffers=[blob]) else: # The comm is ASCII, so we send the image in base64 encoded data # URL form. data = b64encode(blob).decode('ascii') data_uri = "data:image/png;base64,{0}".format(data) self.comm.send({'data': data_uri}) def on_message(self, message): # The 'supports_binary' message is relevant to the # websocket itself. The other messages get passed along # to matplotlib as-is. # Every message has a "type" and a "figure_id". message = json.loads(message['content']['data']) if message['type'] == 'closing': self.on_close() self.manager.clearup_closed() elif message['type'] == 'supports_binary': self.supports_binary = message['value'] else: self.manager.handle_json(message)
class CommSocket(object): """ Manages the Comm connection between IPython and the browser (client). Comms are 2 way, with the CommSocket being able to publish a message via the send_json method, and handle a message with on_message. On the JS side figure.send_message and figure.ws.onmessage do the sending and receiving respectively. """ def __init__(self, manager): self.supports_binary = None self.manager = manager self.uuid = str(uuid()) # Publish an output area with a unique ID. The javascript can then # hook into this area. display(HTML("<div id=%r></div>" % self.uuid)) try: self.comm = Comm('matplotlib', data={'id': self.uuid}) except AttributeError: raise RuntimeError('Unable to create an IPython notebook Comm ' 'instance. Are you in the IPython notebook?') self.comm.on_msg(self.on_message) manager = self.manager self._ext_close = False def _on_close(close_message): self._ext_close = True manager.remove_comm(close_message['content']['comm_id']) manager.clearup_closed() self.comm.on_close(_on_close) def is_open(self): return not (self._ext_close or self.comm._closed) def on_close(self): # When the socket is closed, deregister the websocket with # the FigureManager. if self.is_open(): try: self.comm.close() except KeyError: # apparently already cleaned it up? pass def send_json(self, content): self.comm.send({'data': json.dumps(content)}) def send_binary(self, blob): # The comm is ascii, so we always send the image in base64 # encoded data URL form. data = b64encode(blob) if six.PY3: data = data.decode('ascii') data_uri = "data:image/png;base64,{0}".format(data) self.comm.send({'data': data_uri}) def on_message(self, message): # The 'supports_binary' message is relevant to the # websocket itself. The other messages get passed along # to matplotlib as-is. # Every message has a "type" and a "figure_id". message = json.loads(message['content']['data']) if message['type'] == 'closing': self.on_close() self.manager.clearup_closed() elif message['type'] == 'supports_binary': self.supports_binary = message['value'] else: self.manager.handle_json(message)