Skip to content

Commit

Permalink
Allow decoding buffers in comm message (#108)
Browse files Browse the repository at this point in the history
* Allow decoding buffers in comm message

* Fix decoding
  • Loading branch information
philippjfr authored May 24, 2023
1 parent 4f3319f commit abc8f4d
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions pyviz_comms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,40 +295,34 @@ 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):
"""
Decode incoming message, e.g. by parsing json.
"""
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
Expand Down Expand Up @@ -413,7 +407,6 @@ def init(self):
if self._on_open:
self._on_open({})


@classmethod
def decode(cls, msg):
"""
Expand All @@ -422,15 +415,13 @@ def decode(cls, msg):
"""
return msg['content']['data']


def close(self):
"""
Closes the comm connection
"""
if self._comm:
self._comm.close()


def send(self, data=None, metadata=None, buffers=[]):
"""
Pushes data across comm socket.
Expand All @@ -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,
Expand All @@ -460,6 +450,13 @@ class JupyterCommJS(JupyterComm):
</script>
"""

@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
Expand Down

0 comments on commit abc8f4d

Please sign in to comment.