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

Skip to content

Commit 990c3b1

Browse files
authored
Merge pull request #18583 from QuLogic/nbagg-binary
ENH: Support binary comms in nbagg.
2 parents 2d97de2 + 9dc4046 commit 990c3b1

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

lib/matplotlib/backends/backend_nbagg.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,14 @@ def send_json(self, content):
197197
self.comm.send({'data': json.dumps(content)})
198198

199199
def send_binary(self, blob):
200-
# The comm is ascii, so we always send the image in base64
201-
# encoded data URL form.
202-
data = b64encode(blob).decode('ascii')
203-
data_uri = "data:image/png;base64,{0}".format(data)
204-
self.comm.send({'data': data_uri})
200+
if self.supports_binary:
201+
self.comm.send({'blob': 'image/png'}, buffers=[blob])
202+
else:
203+
# The comm is ASCII, so we send the image in base64 encoded data
204+
# URL form.
205+
data = b64encode(blob).decode('ascii')
206+
data_uri = "data:image/png;base64,{0}".format(data)
207+
self.comm.send({'data': data_uri})
205208

206209
def on_message(self, message):
207210
# The 'supports_binary' message is relevant to the

lib/matplotlib/backends/web_backend/js/mpl.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -499,11 +499,14 @@ mpl.figure.prototype.updated_canvas_event = function () {
499499
mpl.figure.prototype._make_on_message_function = function (fig) {
500500
return function socket_on_message(evt) {
501501
if (evt.data instanceof Blob) {
502-
/* FIXME: We get "Resource interpreted as Image but
503-
* transferred with MIME type text/plain:" errors on
504-
* Chrome. But how to set the MIME type? It doesn't seem
505-
* to be part of the websocket stream */
506-
evt.data.type = 'image/png';
502+
var img = evt.data;
503+
if (img.type !== 'image/png') {
504+
/* FIXME: We get "Resource interpreted as Image but
505+
* transferred with MIME type text/plain:" errors on
506+
* Chrome. But how to set the MIME type? It doesn't seem
507+
* to be part of the websocket stream */
508+
img.type = 'image/png';
509+
}
507510

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

515518
fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(
516-
evt.data
519+
img
517520
);
518521
fig.updated_canvas_event();
519522
fig.waiting = false;

lib/matplotlib/backends/web_backend/js/nbagg_mpl.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var comm_websocket_adapter = function (comm) {
66
// socket, so there is still some room for performance tuning.
77
var ws = {};
88

9+
ws.binaryType = comm.kernel.ws.binaryType;
10+
911
ws.close = function () {
1012
comm.close();
1113
};
@@ -16,8 +18,14 @@ var comm_websocket_adapter = function (comm) {
1618
// Register the callback with on_msg.
1719
comm.on_msg(function (msg) {
1820
//console.log('receiving', msg['content']['data'], msg);
21+
var data = msg['content']['data'];
22+
if (data['blob'] !== undefined) {
23+
data = {
24+
data: new Blob(msg['buffers'], { type: data['blob'] }),
25+
};
26+
}
1927
// Pass the mpl event to the overridden (by mpl) onmessage function.
20-
ws.onmessage(msg['content']['data']);
28+
ws.onmessage(data);
2129
});
2230
return ws;
2331
};

0 commit comments

Comments
 (0)