Skip to content

Commit

Permalink
Performance improvements to WebAgg backend
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom committed Jul 30, 2013
1 parent 4b95f88 commit 25ff2f7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/matplotlib/backend_bases.py
Expand Up @@ -3125,7 +3125,7 @@ def draw(self):

for loc in locators:
loc.refresh()
self.canvas.draw()
self.canvas.draw_idle()

def _update_view(self):
"""Update the viewlim and position from the view and
Expand All @@ -3146,7 +3146,7 @@ def _update_view(self):
a.set_position(pos[i][0], 'original')
a.set_position(pos[i][1], 'active')

self.draw()
self.draw_idle()

def save_figure(self, *args):
"""Save the current figure"""
Expand Down
15 changes: 9 additions & 6 deletions lib/matplotlib/backends/backend_webagg.py
Expand Up @@ -253,7 +253,6 @@ def handle_event(self, event):
elif e_type == 'key_release':
self.key_release_event(key)
elif e_type == 'toolbar_button':
print('Toolbar button pressed: ', event['name'])
# TODO: Be more suspicious of the input
getattr(self.toolbar, event['name'])()
elif e_type == 'refresh':
Expand Down Expand Up @@ -296,8 +295,10 @@ def remove_web_socket(self, web_socket):
self.web_sockets.remove(web_socket)

def refresh_all(self):
for s in self.web_sockets:
s.send_image()
if len(self.web_sockets):
diff = self.canvas.get_diff_image()
for s in self.web_sockets:
s.send_image(diff)

def send_event(self, event_type, **kwargs):
for s in self.web_sockets:
Expand Down Expand Up @@ -473,6 +474,8 @@ def open(self, fignum):
_, _, w, h = manager.canvas.figure.bbox.bounds
manager.resize(w, h)
self.on_message('{"type":"refresh"}')
if hasattr(self, 'set_nodelay'):
self.set_nodelay(True)

def on_close(self):
Gcf.get_fig_manager(self.fignum).remove_web_socket(self)
Expand All @@ -484,6 +487,8 @@ def on_message(self, message):
# whole.
if message['type'] == 'supports_binary':
self.supports_binary = message['value']
elif message['type'] == 'ack':
pass
else:
canvas = Gcf.get_fig_manager(self.fignum).canvas
canvas.handle_event(message)
Expand All @@ -493,9 +498,7 @@ def send_event(self, event_type, **kwargs):
payload.update(kwargs)
self.write_message(json.dumps(payload))

def send_image(self):
canvas = Gcf.get_fig_manager(self.fignum).canvas
diff = canvas.get_diff_image()
def send_image(self, diff):
if self.supports_binary:
self.write_message(diff, binary=True)
else:
Expand Down
10 changes: 10 additions & 0 deletions lib/matplotlib/backends/web_backend/mpl.js
Expand Up @@ -66,6 +66,11 @@ figure.prototype.finalize = function (canvas_id_prefix, toolbar_id_prefix, messa
onload_creator = function(fig) {return function() {fig.context.drawImage(fig.imageObj, 0, 0);};};
this.imageObj.onload = onload_creator(fig);


this.imageObj.onunload = function() {
this.ws.close();
}

this.ws.onmessage = gen_on_msg_fn(this);
};

Expand All @@ -88,11 +93,13 @@ function gen_on_msg_fn(fig)
}
fig.imageObj.src = (window.URL || window.webkitURL).createObjectURL(
evt.data);
fig.ws.send('{"type": "ack"}')
return;
}
} else {
if (evt.data.slice(0, 21) == "data:image/png;base64") {
fig.imageObj.src = evt.data;
fig.ws.send('{"type": "ack"}')
return;
}
}
Expand Down Expand Up @@ -132,6 +139,9 @@ function gen_on_msg_fn(fig)
fig.rubberband_canvas.width = size[0];
fig.rubberband_canvas.height = size[1];
fig.ws.send(JSON.stringify({type: 'refresh'}));
fig.ws.send(JSON.stringify(
{type: 'supports_binary',
value: fig.supports_binary}));
}
break;

Expand Down

0 comments on commit 25ff2f7

Please sign in to comment.