Skip to content

Commit

Permalink
Merge pull request #4456 from tacaswell/fix_nbagg_close
Browse files Browse the repository at this point in the history
FIX : first pass at fixing nbagg close issue
  • Loading branch information
jenshnielsen committed Aug 17, 2015
2 parents 681bfd0 + 3aa3909 commit 3520e94
Show file tree
Hide file tree
Showing 4 changed files with 684 additions and 579 deletions.
49 changes: 40 additions & 9 deletions lib/matplotlib/backends/backend_nbagg.py
Expand Up @@ -78,9 +78,10 @@ def connection_info():
for manager in Gcf.get_all_fig_managers():
fig = manager.canvas.figure
result.append('{0} - {0}'.format((fig.get_label() or
"Figure {0}".format(manager.num)),
manager.web_sockets))
result.append('Figures pending show: {0}'.format(len(Gcf._activeQue)))
"Figure {0}".format(manager.num)),
manager.web_sockets))
if not is_interactive():
result.append('Figures pending show: {0}'.format(len(Gcf._activeQue)))
return '\n'.join(result)


Expand Down Expand Up @@ -166,13 +167,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 not socket.is_open()])
if socket.is_open()])

if len(self.web_sockets) == 0:
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 @@ -231,13 +241,22 @@ def new_figure_manager_given_figure(num, figure):
"""
Create a new figure manager instance for the given figure.
"""
from .._pylab_helpers import Gcf

def closer(event):
Gcf.destroy(num)

canvas = FigureCanvasNbAgg(figure)
if rcParams['nbagg.transparent']:
figure.patch.set_alpha(0)
manager = FigureManagerNbAgg(canvas, num)

if is_interactive():
manager.show()
figure.canvas.draw_idle()

canvas.mpl_connect('close_event', closer)

return manager


Expand Down Expand Up @@ -266,16 +285,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()
self.manager.clearup_closed()
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 @@ -298,6 +328,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
1 change: 1 addition & 0 deletions lib/matplotlib/backends/backend_webagg_core.py
Expand Up @@ -329,6 +329,7 @@ def handle_event(self, event):
self.send_event('figure_label', label=figure_label)
self._force_full = True
self.draw_idle()

else:
handler = getattr(self, 'handle_{0}'.format(e_type), None)
if handler is None:
Expand Down
16 changes: 14 additions & 2 deletions lib/matplotlib/backends/web_backend/nbagg_mpl.js
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

0 comments on commit 3520e94

Please sign in to comment.