Skip to content

A simple context manager for using matplotlib backends - #6439

Closed
adrn wants to merge 1 commit into
ipython:masterfrom
adrn:use-backend
Closed

adrn wants to merge 1 commit into
ipython:masterfrom
adrn:use-backend

Conversation

@adrn

@adrn adrn commented Sep 9, 2014

Copy link
Copy Markdown
Contributor

I'm often in the situation where most of the plots in my notebook are fine as static pngs (with inline), but it would be nice to interact with some (e.g., change view angle on 3D plots). The nbagg backend is nice, but a bit slow for interacting with some figures. I thought it would be nice to have a context manager that allowed code in a given cell to use a different backend.

This is a very simple implementation of something what works on my machine. Let me know if I overlooked something. Will add tests, but first want to make sure this simple approach is ok...

Also, curse my white-space nuker...

@adrn

adrn commented Sep 9, 2014

Copy link
Copy Markdown
Contributor Author

@takluyver

Copy link
Copy Markdown
Member

Seems reasonable to me, but can you make the commit again, configuring your editor not to strip whitespace? We don't like big whitespace changes.

@Carreau

Carreau commented Sep 9, 2014

Copy link
Copy Markdown
Member

I think the whitespace will be delt with :

Also, curse my white-space nuker...

+1 also on the context manager.
As the with will I guess mostly be always the same, could we also maybe export inline= use_backend('inline') qt= use_backend('qt') which simplify your example to:

with inline:
   blahhhh

Maybe it's a bad idea as it reuses the same context manager each time, but I guess you see what I mean.

@minrk

minrk commented Sep 9, 2014

Copy link
Copy Markdown
Member

I don't really view core.pylabtools as a public API. Do we want to consider making a public place for matplotlib-related things?

@adrn

adrn commented Sep 10, 2014

Copy link
Copy Markdown
Contributor Author

So revert the whitespace changes, or not?

@minrk did you want me to add that to this PR?

@minrk

minrk commented Sep 10, 2014

Copy link
Copy Markdown
Member

@adrn I might be alone in thinking that pylabtools isn't a place for public APIs, so let's wait to hear from someone else before making you do more stuff.

We always appreciate avoiding / removing whitespace changes if you are comfortable with the --amend/push -f bits of git, but if not it's no big deal. Adding another commit to rollback whitespace changes would actually be worse.

@takluyver

Copy link
Copy Markdown
Member

It contains bits that are meant to be public, like select_figure_formats, and bits that probably aren't. I think it makes sense to have a public module separate from IPython.display for features relating to our integration with matplotlib. I don't mind if that's pylabtools or a separate module containing just the public parts.

@minrk

minrk commented Sep 10, 2014

Copy link
Copy Markdown
Member

select_figure_formats isn't public, the public API for that is IPython.display.set_matplotlib_formats.

Should we add IPython.mplutil or similar?

@takluyver

Copy link
Copy Markdown
Member

Ah, OK, I didn't know (/had forgotten) that distinction.

My inclination would be to put a module for this under IPython.lib, but I don't really have an argument to back that up, it just feels like the right place.

@adrn

adrn commented Sep 11, 2014

Copy link
Copy Markdown
Contributor Author

Pulled out the whitespace changes

@takluyver

Copy link
Copy Markdown
Member

Marked needs decision for the API question: do we want a separate module for public API around mpl integration?

Also, naming question - to me, use_backend seems a bit too generic for something that temporarily switches backend and then switches back. Maybe temp_backend?

@Carreau

Carreau commented Sep 22, 2014

Copy link
Copy Markdown
Member

Also, naming question - to me, use_backend seems a bit too generic for something that temporarily switches backend and then switches back. Maybe temp_backend?

For the records (as I might not be here for the decision hangout):

  • I'm fine with this leaving where it is now.
  • naming it just 'backend(...)' is ok for me too (or anything) as it is meant to be a context manager, it seem pretty obvious that it will switch back.
  • I would be in favor of already having shortcut context manager, namely qtbackend,inlinebackend ... etc

And one last question, should check that old != new and do nothing otherwise ?

@ellisonbg

Copy link
Copy Markdown
Member
  • We are not sure that this actually works reliably. Need to review that.
  • We decided that we are +1 on creating separate public API module for mpl related things

@minrk

minrk commented Oct 1, 2014

Copy link
Copy Markdown
Member

After testing, this doesn't seem to work. When I use a GUI backend, it draws briefly and is dismissed. When I use inline temporarily, it just never draws. I think temporarily enabling a backend is significantly more complex than this suggests, because a lot of mpl events happen asynchronously, or in the post_execute hook, and thus after the context manager __exit__

@minrk

minrk commented Nov 13, 2014

Copy link
Copy Markdown
Member

Closing this one as stale. If you want to pick it up again and try to work out the tricky async bits, feel free to open a new PR.

@minrk minrk added this to the no action milestone Nov 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants