Skip to content
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

WebAgg performance improvements #2261

Merged
merged 2 commits into from Aug 1, 2013
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
22 changes: 16 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 self.web_sockets:
diff = self.canvas.get_diff_image()
for s in self.web_sockets:
s.send_diff_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,15 @@ def on_message(self, message):
# whole.
if message['type'] == 'supports_binary':
self.supports_binary = message['value']
elif message['type'] == 'ack':
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment here?

# Network latency tends to decrease if traffic is
# flowing in both directions. Therefore, the browser
# sends back an "ack" message after each image frame
# is received. This could also be used as a simple
# sanity check in the future, but for now the
# performance increase is enough to justify it, even
# if the server does nothing with it.
pass
else:
canvas = Gcf.get_fig_manager(self.fignum).canvas
canvas.handle_event(message)
Expand All @@ -493,9 +505,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_diff_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