8000 FIX : first pass at fixing nbagg close issue by tacaswell · Pull Request #4456 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

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 all commits
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
49 changes: 40 additions & 9 deletions lib/matplotlib/backends/backend_nbagg.py
Original file line number Diff line number Diff line change
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))
Copy link
Member

Choose a reason for hiding this comment

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

I assume you did this as a PEP8 change, but the second line still lacks a space of indent...

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
Original file line number Diff line number Diff line change
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
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
Loading
0