Skip to content

Commit b5cde86

Browse files
committed
nbagg: Don't close figures for bubbled events.
In JavaScript, the event handlers are called for both the element and also bubbled up from any children. If the Matplotlib figure is the only thing in the output, that's fine, as closing the figure when one of the `canvas` or `div`s clears is the same as when the `OutputArea` is cleared. However, if there are other outputs, such as widgets, we do not want to close the figure if one of them is cleared. As there's no way to clear just the figure output, only close the figure if the _entire_ `OutputArea` div is cleared (i.e., the element we've attached to). Fixes #18638.
1 parent 8699958 commit b5cde86

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

lib/matplotlib/backends/web_backend/js/nbagg_mpl.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ mpl.mpl_figure_comm = function (comm, msg) {
4848
console.error('Failed to find cell for figure', id, fig);
4949
return;
5050
}
51-
fig.cell_info[0].output_area.element.one(
51+
fig.cell_info[0].output_area.element.on(
5252
'cleared',
5353
{ fig: fig },
5454
fig._remove_fig_handler
@@ -181,6 +181,10 @@ mpl.figure.prototype._init_toolbar = function () {
181181

182182
mpl.figure.prototype._remove_fig_handler = function (event) {
183183
var fig = event.data.fig;
184+
if (event.target !== this) {
185+
// Ignore bubbled events from children.
186+
return;
187+
}
184188
fig.close_ws(fig, {});
185189
};
186190

0 commit comments

Comments
 (0)