Skip to content

Send binary data as binary instead of string from Python to JS #288

Answered by manzt
domoritz asked this question in General
Discussion options

You must be logged in to vote

Ok, so in order to send binary data for custom messages, you must use the buffers argument of the self.send.

self.send(msg, buffers=[msg.pop("result")])

Then on the JS side, the handler for msg:custom event receives a msg (result of JSON.parse) and buffers (your binary data serialized as a JS DataView instance):

view.model.on("msg:custom", (msg, buffers) => {
  let dataView = buffers[0]; // DataView
  let buffer = dataView.buffer; // ArrayBuffer
})

(This is all probably some information that should find its way into our docs. My first time learning some of this, and not very well documented in the Jupyter Widgets itself. Sorry for the confusion/frustration.)

Replies: 8 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by manzt
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #58 on September 17, 2023 16:39.