From b5cde861c9f8f08f3e0d3bb7d2b54607483c24f5 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 2 Oct 2020 19:12:34 -0400 Subject: [PATCH] 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. --- lib/matplotlib/backends/web_backend/js/nbagg_mpl.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/backends/web_backend/js/nbagg_mpl.js b/lib/matplotlib/backends/web_backend/js/nbagg_mpl.js index 0f538979d19d..688e1953ebe1 100644 --- a/lib/matplotlib/backends/web_backend/js/nbagg_mpl.js +++ b/lib/matplotlib/backends/web_backend/js/nbagg_mpl.js @@ -48,7 +48,7 @@ mpl.mpl_figure_comm = function (comm, msg) { console.error('Failed to find cell for figure', id, fig); return; } - fig.cell_info[0].output_area.element.one( + fig.cell_info[0].output_area.element.on( 'cleared', { fig: fig }, fig._remove_fig_handler @@ -181,6 +181,10 @@ mpl.figure.prototype._init_toolbar = function () { mpl.figure.prototype._remove_fig_handler = function (event) { var fig = event.data.fig; + if (event.target !== this) { + // Ignore bubbled events from children. + return; + } fig.close_ws(fig, {}); };