Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

FIX : first pass at fixing nbagg close issue #4456

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 6 commits into from
Aug 17, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
FIX: destroy figs in python when destroyed in js
 - only actually closes the figure (in the mpl sense) if all of
   the managers active comms are dead
 - closes figure when all visible copies are removed from dom
 - eliminates many of the [IPKernelApp] ERROR | No such comm: XXXX
   errors

closes #4841
  • Loading branch information
tacaswell committed Aug 17, 2015
commit 436dfdbc4cb6b4a9cd9868b4dd36d60cdcc7da97
29 changes: 24 additions & 5 deletions 29 lib/matplotlib/backends/backend_nbagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,22 @@ def _create_comm(self):

def destroy(self):
self._send_event('close')
for comm in self.web_sockets.copy():
# need to copy comms as callbacks will modify this list
for comm in list(self.web_sockets):
comm.on_close()
self.clearup_closed()

def clearup_closed(self):
"""Clear up any closed Comms."""
self.web_sockets = set([socket for socket in self.web_sockets
if socket.is_open()])

if len(self.web_sockets) == 0:
self.manager.canvas.close_event()
self.canvas.close_event()

def remove_comm(self, comm_id):
self.web_sockets = set([socket for socket in self.web_sockets
if not socket.comm.comm_id == comm_id])


class TimerTornado(TimerBase):
Expand Down Expand Up @@ -278,15 +284,27 @@ def __init__(self, manager):
self.comm.on_msg(self.on_message)

manager = self.manager
self.comm.on_close(lambda close_message: manager.clearup_closed())
self._ext_close = False

def _on_close(close_message):
self._ext_close = True
manager.remove_comm(close_message['content']['comm_id'])
manager.clearup_closed()

self.comm.on_close(_on_close)

def is_open(self):
return not self.comm._closed
return not (self._ext_close or self.comm._closed)

def on_close(self):
# When the socket is closed, deregister the websocket with
# the FigureManager.
self.comm.close()
if self.is_open():
try:
self.comm.close()
except KeyError:
# apparently already cleaned it up?
pass

def send_json(self, content):
self.comm.send({'data': json.dumps(content)})
Expand All @@ -309,6 +327,7 @@ def on_message(self, message):
message = json.loads(message['content']['data'])
if message['type'] == 'closing':
self.on_close()
self.manager.clearup_closed()
elif message['type'] == 'supports_binary':
self.supports_binary = message['value']
else:
Expand Down
16 changes: 14 additions & 2 deletions 16 lib/matplotlib/backends/web_backend/nbagg_mpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,21 @@ mpl.mpl_figure_comm = function(comm, msg) {
};

mpl.figure.prototype.handle_close = function(fig, msg) {
fig.root.unbind('remove')

// Update the output cell to use the data from the current canvas.
fig.push_to_output();
var dataURL = fig.canvas.toDataURL();
// Re-enable the keyboard manager in IPython - without this line, in FF,
// the notebook keyboard shortcuts fail.
IPython.keyboard_manager.enable()
$(fig.parent_element).html('<img src="' + dataURL + '">');
fig.send_message('closing', {});
fig.ws.close()
fig.close_ws(fig, msg);
}

mpl.figure.prototype.close_ws = function(fig, msg){
fig.send_message('closing', msg);
// fig.ws.close()
}

mpl.figure.prototype.push_to_output = function(remove_interactive) {
Expand Down Expand Up @@ -126,6 +132,12 @@ mpl.figure.prototype._init_toolbar = function() {
titlebar.prepend(buttongrp);
}

mpl.figure.prototype._root_extra_style = function(el){
var fig = this
el.on("remove", function(){
fig.close_ws(fig, {});
});
}

mpl.figure.prototype._canvas_extra_style = function(el){
// this is important to make the div 'focusable
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.