From abc8f4dfc06c3981aeb1749af91b506dd3e095fc Mon Sep 17 00:00:00 2001 From: Philipp Rudiger Date: Wed, 24 May 2023 20:10:25 +0200 Subject: [PATCH] Allow decoding buffers in comm message (#108) * Allow decoding buffers in comm message * Fix decoding --- pyviz_comms/__init__.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/pyviz_comms/__init__.py b/pyviz_comms/__init__.py index 6467bf5..bac6ce5 100644 --- a/pyviz_comms/__init__.py +++ b/pyviz_comms/__init__.py @@ -295,25 +295,21 @@ def __init__(self, id=None, on_msg=None, on_error=None, on_stdout=None, on_open= self._comm = None super(Comm, self).__init__(id = id if id else uuid.uuid4().hex) - def init(self, on_msg=None): """ Initializes comms channel. """ - def close(self): """ Closes the comm connection """ - def send(self, data=None, metadata=None, buffers=[]): """ Sends data to the frontend """ - @classmethod def decode(cls, msg): """ @@ -321,14 +317,12 @@ def decode(cls, msg): """ return msg - @property def comm(self): if not self._comm: raise ValueError('Comm has not been initialized') return self._comm - def _handle_msg(self, msg): """ Decode received message before passing it to on_msg callback @@ -413,7 +407,6 @@ def init(self): if self._on_open: self._on_open({}) - @classmethod def decode(cls, msg): """ @@ -422,7 +415,6 @@ def decode(cls, msg): """ return msg['content']['data'] - def close(self): """ Closes the comm connection @@ -430,7 +422,6 @@ def close(self): if self._comm: self._comm.close() - def send(self, data=None, metadata=None, buffers=[]): """ Pushes data across comm socket. @@ -440,7 +431,6 @@ def send(self, data=None, metadata=None, buffers=[]): self.comm.send(data, metadata=metadata, buffers=buffers) - class JupyterCommJS(JupyterComm): """ JupyterCommJS provides a comms channel for the Jupyter notebook, @@ -460,6 +450,13 @@ class JupyterCommJS(JupyterComm): """ + @classmethod + def decode(cls, msg): + decoded = dict(msg['content']['data']) + if 'buffers' in msg: + decoded['_buffers'] = {i: v for i, v in enumerate(msg['buffers'])} + return decoded + def __init__(self, id=None, on_msg=None, on_error=None, on_stdout=None, on_open=None): """ Initializes a Comms object