Skip to content

nbagg: Closing a figure from the notebook does not close the python figure #18447

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

Closed
sbitzer opened this issue Sep 10, 2020 · 2 comments · Fixed by #18454
Closed

nbagg: Closing a figure from the notebook does not close the python figure #18447

sbitzer opened this issue Sep 10, 2020 · 2 comments · Fixed by #18454
Milestone

Comments

@sbitzer
Copy link

sbitzer commented Sep 10, 2020

I'm reporting a digression from previous behaviour of the nbagg backend in Jupyter notebooks. Until matplotlib 3.2.2, when I refreshed an interactive figure in a notebook cell by re-executing the cell, the existing figure would be closed and a new one created using the old figure number (when the cell also created the original figure). In matplotlib 3.3.1 the old figure is replaced by the new figure (with a new figure number) in the notebook output, but the old figure still exists in the kernel.

I guess this is related to "NbAgg and WebAgg no longer use jQuery & jQuery UI" from the release notes of matplotlib 3.3.0. Also, ipympl has the same issue: matplotlib/ipympl#4.

This can be tested in a notebook with:

%matplotlib notebook
import matplotlib.pyplot as plt
fig = plt.figure()
plt.get_fignums()

I saw this behaviour in recent versions of Firefox and Google Chrome.

I have manually applied 2c9a85b in case this would reintroduce the old behaviour (which I prefer), but it didn't.

@jklymak
Copy link
Member

jklymak commented Sep 10, 2020

Is this related to #17086 #17053? @QuLogic ? This seems a pretty major regression - I certainly have notebooks with dozens of figures that I will then execute dozens of times.

@jklymak jklymak added this to the v3.3.2 milestone Sep 10, 2020
@QuLogic
Copy link
Member

QuLogic commented Sep 11, 2020

I think this is because jQuery has a wrapped remove event, but it doesn't actually exist in HTML/JS, so this change is never triggered to close the figure.

QuLogic added a commit to QuLogic/matplotlib that referenced this issue Sep 11, 2020
When converting from jQuery to vanilla JavaScript, the trigger for the
figure closure was set to a non-existent event name, as the 'remove'
event is jQuery-specific.

As there is no DOM node removal event (unless using MutationObserver),
connect this handler to the notebook's `Cell` `OutputArea`'s 'cleared'
event. This is triggered both by re-running a cell, and by using the
Clear Output menu option.

Fixes matplotlib#18447.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants