Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Support binary comms in nbagg. #18583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/matplotlib/backends/backend_nbagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,14 @@ 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).decode('ascii')
data_uri = "data:image/png;base64,{0}".format(data)
self.comm.send({'data': data_uri})
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
Expand Down
15 changes: 9 additions & 6 deletions lib/matplotlib/backends/web_backend/js/mpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,14 @@ mpl.figure.prototype.updated_canvas_event = function () {
mpl.figure.prototype._make_on_message_function = function (fig) {
return function socket_on_message(evt) {
if (evt.data instanceof Blob) {
/* FIXME: We get "Resource interpreted as Image but
* transferred with MIME type text/plain:" errors on
* Chrome. But how to set the MIME type? It doesn't seem
* to be part of the websocket stream */
evt.data.type = 'image/png';
var img = evt.data;
if (img.type !== 'image/png') {
/* FIXME: We get "Resource interpreted as Image but
* transferred with MIME type text/plain:" errors on
* Chrome. But how to set the MIME type? It doesn't seem
* to be part of the websocket stream */
img.type = 'image/png';
}

/* Free the memory for the previous frames */
if (fig.imageObj.src) {
Expand All @@ -513,7 +516,7 @@ mpl.figure.prototype._make_on_message_function = function (fig) {
}

fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(
evt.data
img
);
fig.updated_canvas_event();
fig.waiting = false;
Expand Down
10 changes: 9 additions & 1 deletion lib/matplotlib/backends/web_backend/js/nbagg_mpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ var comm_websocket_adapter = function (comm) {
// socket, so there is still some room for performance tuning.
var ws = {};

ws.binaryType = comm.kernel.ws.binaryType;

ws.close = function () {
comm.close();
};
Expand All @@ -16,8 +18,14 @@ var comm_websocket_adapter = function (comm) {
// Register the callback with on_msg.
comm.on_msg(function (msg) {
//console.log('receiving', msg['content']['data'], msg);
var data = msg['content']['data'];
if (data['blob'] !== undefined) {
data = {
data: new Blob(msg['buffers'], { type: data['blob'] }),
};
}
// Pass the mpl event to the overridden (by mpl) onmessage function.
ws.onmessage(msg['content']['data']);
ws.onmessage(data);
});
return ws;
};
Expand Down