Skip to content

Commit

Permalink
Handle non-binary image streams
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom committed Jan 4, 2013
1 parent 914e525 commit 0c99763
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
19 changes: 16 additions & 3 deletions lib/matplotlib/backends/backend_webagg.py
Expand Up @@ -375,6 +375,8 @@ def get(self, fignum, format):
self.write(buffer.getvalue()) self.write(buffer.getvalue())


class WebSocket(tornado.websocket.WebSocketHandler): class WebSocket(tornado.websocket.WebSocketHandler):
supports_binary = True

def open(self, fignum): def open(self, fignum):
self.fignum = int(fignum) self.fignum = int(fignum)
manager = Gcf.get_fig_manager(self.fignum) manager = Gcf.get_fig_manager(self.fignum)
Expand All @@ -388,8 +390,14 @@ def on_close(self):


def on_message(self, message): def on_message(self, message):
message = json.loads(message) message = json.loads(message)
canvas = Gcf.get_fig_manager(self.fignum).canvas # The 'supports_binary' message is on a client-by-client
canvas.handle_event(message) # basis. The others affect the (shared) canvas as a
# whole.
if message['type'] == 'supports_binary':
self.supports_binary = message['value']
else:
canvas = Gcf.get_fig_manager(self.fignum).canvas
canvas.handle_event(message)


def send_event(self, event_type, **kwargs): def send_event(self, event_type, **kwargs):
payload = {'type': event_type} payload = {'type': event_type}
Expand All @@ -399,7 +407,12 @@ def send_event(self, event_type, **kwargs):
def send_image(self): def send_image(self):
canvas = Gcf.get_fig_manager(self.fignum).canvas canvas = Gcf.get_fig_manager(self.fignum).canvas
diff = canvas.get_diff_image() diff = canvas.get_diff_image()
self.write_message(diff, binary=True) if self.supports_binary:
self.write_message(diff, binary=True)
else:
data_uri = "data:image/png;base64,{0}".format(
diff.encode('base64').replace('\n', ''))
self.write_message(data_uri)


def __init__(self): def __init__(self):
super(WebAggApplication, self).__init__([ super(WebAggApplication, self).__init__([
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/web_static/mpl.css
@@ -1,6 +1,6 @@
.mpl-message { .mpl-message {
float:right; float:right;
vertical-align:text-center; vertical-align:middle;
} }


.mpl-toolbar-spacer { .mpl-toolbar-spacer {
Expand Down
50 changes: 36 additions & 14 deletions lib/matplotlib/backends/web_static/mpl.js
Expand Up @@ -17,28 +17,50 @@ window.onload = function() {
} else if (typeof(MozWebSocket) !== 'undefined') { } else if (typeof(MozWebSocket) !== 'undefined') {
this.WebSocket = MozWebSocket; this.WebSocket = MozWebSocket;
} else { } else {
alert('Your browser does not have WebSocket support, please try Chrome, Safari or Firefox ≥ 6. Firefox 4 and 5 are also supported by you have to enable WebSockets in about:config.'); alert('Your browser does not have WebSocket support.' +
'Please try Chrome, Safari or Firefox ≥ 6. ' +
'Firefox 4 and 5 are also supported but you ' +
'have to enable WebSockets in about:config.');
}; };


var message = document.getElementById("mpl-message"); var message = document.getElementById("mpl-message");
var canvas = document.getElementById("mpl-canvas"); var canvas = document.getElementById("mpl-canvas");


ws = new this.WebSocket(ws_url("ws")); ws = new this.WebSocket(ws_url("ws"));

var supports_binary = (ws.binaryType != undefined);

ws.onopen = function () {
ws.send(JSON.stringify(
{type: 'supports_binary',
value: supports_binary}));
}

ws.onmessage = function (evt) { ws.onmessage = function (evt) {
if (evt.data instanceof Blob) { console.log("message " + evt.data);
/* FIXME: We get "Resource interpreted as Image but
* transferred with MIME type text/plain:" errors on if (supports_binary) {
* Chrome. But how to set the MIME type? It doesn't seem if (evt.data instanceof Blob) {
* to be part of the websocket stream */ /* FIXME: We get "Resource interpreted as Image but
evt.data.type = "image/png"; * transferred with MIME type text/plain:" errors on

* Chrome. But how to set the MIME type? It doesn't seem
/* Free the memory for the previous frames */ * to be part of the websocket stream */
if (imageObj.src) { evt.data.type = "image/png";
(window.URL || window.webkitURL).revokeObjectURL(imageObj.src);
/* Free the memory for the previous frames */
if (imageObj.src) {
(window.URL || window.webkitURL).revokeObjectURL(imageObj.src);
}

imageObj.src = (window.URL || window.webkitURL).createObjectURL(evt.data);
return;
}
} else {
if (evt.data.slice(0, 21) == "data:image/png;base64") {
console.log("got image");
imageObj.src = evt.data;
return;
} }

imageObj.src = (window.URL || window.webkitURL).createObjectURL(evt.data);
return;
} }


var msg = JSON.parse(evt.data); var msg = JSON.parse(evt.data);
Expand Down

0 comments on commit 0c99763

Please sign in to comment.