-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add _repr_png_ for figures with no configured IPython backend #17891
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
Conversation
46e810e
to
1f94c51
Compare
We talked about this on the phone at the end of day 1 scipy sprints when my brain was turning to mush. The quandary (quagmire?) we are trying to get our selves out of is that a) we want to use the standard IPython/jupyter protocols as much as possible The things we want to work in notebooks / jlab fig = plt.figure() fig, ax = plt.subplots() should show the figure even though the fig = plt.figure()
fig This should show exactly once so that the implicit display from the first case and the automatic display from the second case do not conflict with each other. fig # assuming this is defined in another cell should also show the "correct" version of the Figure in the notebook. Tangentially, we probably also want a clean way for ipympl / notebook to throw a snapshot of the current figure into the notebook. This can be very useful if you have a notebook that is progressively building a complex figure to get "snapshots" of the intermediate state. We are also using this to work around the issue with ipympl rendering a an empty figure when exported. We also want to make sure that we do not render the figure N times (for example always including both a html and a png version which both have the png embedded) both for run time and payload size reasons (or find a way to manage the caching / data sharing). Tools we have at our disposal
I think the question with this PR is not if it solves all of the problems, but if it moves us forward along any direction (which I think it does in the case of a no-pyplot workflow) and if it paints us into an API corner that we will not be able to get out of (which I don't think it does because this is the lowest priority code path and all of the current special-casing will still win). |
@tacaswell I have confirmed that the following code cell renders a figure twice in Jupyter Lab, but the new code for
To the best of my knowledge / testing, this PR meets @tacaswell's stated goal:
|
For reference, the function we are checking for an attribute on is https://github.com/ipython/ipython/blob/8ad12b0f8fbb2af5386ab31972fbc9889c3d1eb4/IPython/core/pylabtools.py#L363-L419 which is called from https://github.com/ipython/ipython/blob/8ad12b0f8fbb2af5386ab31972fbc9889c3d1eb4/IPython/core/interactiveshell.py#L3396-L3439 which is in turn called from https://github.com/ipython/ipython/blob/8ad12b0f8fbb2af5386ab31972fbc9889c3d1eb4/IPython/core/magics/pylab.py#L47-L100 so what the condition is checking is "has the user typed It looks like there is some figure-level registration at https://github.com/ipython/ipython/blob/8ad12b0f8fbb2af5386ab31972fbc9889c3d1eb4/IPython/core/pylabtools.py#L209-L257 |
I'm happy to have a matplotlib version check there and do nothing if matplotlib.version >= whatever. I can even backport such a change to 7.x which is released monthly. |
For further details, ipympl works because ipywidgets implements
|
Great job @bdice for picking this up! This is probably an unpopular opinion, but I think there should be a clean split between ipython and matplotlib. As far as I know, matplotlib is the only library that gets special cased. Seems it would be much simpler if users just had to place |
Do you mean always, or only if the |
This needs a rebase. It probably also needs a project manager... @tacaswell you are probably the best person to lead the charge on this? |
I'm not sure how to help this move forward. I am going to close this as a stale PR, but feel free to upstream/rebase anything you want from here! |
PR Summary
This PR continues work started by @tdpetrou in #16788. I applied some suggested changes from the previous PR review and changed from
_repr_html_
to_repr_png_
after conversation with @tacaswell and @Carreau.Resolves #16782.
PR Checklist