Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 436dfdb

Browse files
committed
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
1 parent 6678e02 commit 436dfdb

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

lib/matplotlib/backends/backend_nbagg.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,22 @@ def _create_comm(self):
166166

167167
def destroy(self):
168168
self._send_event('close')
169-
for comm in self.web_sockets.copy():
169+
# need to copy comms as callbacks will modify this list
170+
for comm in list(self.web_sockets):
170171
comm.on_close()
172+
self.clearup_closed()
171173

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

177179
if len(self.web_sockets) == 0:
178-
self.manager.canvas.close_event()
180+
self.canvas.close_event()
181+
182+
def remove_comm(self, comm_id):
183+
self.web_sockets = set([socket for socket in self.web_sockets
184+
if not socket.comm.comm_id == comm_id])
179185

180186

181187
class TimerTornado(TimerBase):
@@ -278,15 +284,27 @@ def __init__(self, manager):
278284
self.comm.on_msg(self.on_message)
279285

280286
manager = self.manager
281-
self.comm.on_close(lambda close_message: manager.clearup_closed())
287+
self._ext_close = False
288+
289+
def _on_close(close_message):
290+
self._ext_close = True
291+
manager.remove_comm(close_message['content']['comm_id'])
292+
manager.clearup_closed()
293+
294+
self.comm.on_close(_on_close)
282295

283296
def is_open(self):
284-
return not self.comm._closed
297+
return not (self._ext_close or self.comm._closed)
285298

286299
def on_close(self):
287300
# When the socket is closed, deregister the websocket with
288301
# the FigureManager.
289-
self.comm.close()
302+
if self.is_open():
303+
try:
304+
self.comm.close()
305+
except KeyError:
306+
# apparently already cleaned it up?
307+
pass
290308

291309
def send_json(self, content):
292310
self.comm.send({'data': json.dumps(content)})
@@ -309,6 +327,7 @@ def on_message(self, message):
309327
message = json.loads(message['content']['data'])
310328
if message['type'] == 'closing':
311329
self.on_close()
330+
self.manager.clearup_closed()
312331
elif message['type'] == 'supports_binary':
313332
self.supports_binary = message['value']
314333
else:

lib/matplotlib/backends/web_backend/nbagg_mpl.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,21 @@ mpl.mpl_figure_comm = function(comm, msg) {
5555
};
5656

5757
mpl.figure.prototype.handle_close = function(fig, msg) {
58+
fig.root.unbind('remove')
59+
5860
// Update the output cell to use the data from the current canvas.
5961
fig.push_to_output();
6062
var dataURL = fig.canvas.toDataURL();
6163
// Re-enable the keyboard manager in IPython - without this line, in FF,
6264
// the notebook keyboard shortcuts fail.
6365
IPython.keyboard_manager.enable()
6466
$(fig.parent_element).html('<img src="' + dataURL + '">');
65-
fig.send_message('closing', {});
66-
fig.ws.close()
67+
fig.close_ws(fig, msg);
68+
}
69+
70+
mpl.figure.prototype.close_ws = function(fig, msg){
71+
fig.send_message('closing', msg);
72+
// fig.ws.close()
6773
}
6874

6975
mpl.figure.prototype.push_to_output = function(remove_interactive) {
@@ -126,6 +132,12 @@ mpl.figure.prototype._init_toolbar = function() {
126132
titlebar.prepend(buttongrp);
127133
}
128134

135+
mpl.figure.prototype._root_extra_style = function(el){
136+
var fig = this
137+
el.on("remove", function(){
138+
fig.close_ws(fig, {});
139+
});
140+
}
129141

130142
mpl.figure.prototype._canvas_extra_style = function(el){
131143
// this is important to make the div 'focusable

0 commit comments

Comments
 (0)