I am modifying a python module that plots some special graphs using matplotlib.
Right now, this module just saves all figures as files.
I would like to make it possible to import the module while working in ipython notebook and see the results "inline", on the other hand I would like to keep the default functionality of saving the figures as files when the module is imported in all other cases.
So I need somehow to check if the module is imported in ipython notebook and the pylab is operating inline or not.
How can I check this?
This got me searching, and I think I've found a solution. Not sure if this is actually documented or even intended, but it may very well work:
get_ipython()
appears to be a method only defined when running IPython; it returns what I assume is the current IPython session. Then, you can access theconfig
attribute, which is a dictionary, that includes the 'IPKernelApp' element. The latter is a dictionary in itself that can contain a keypylab
, which can be the string'inline'
.I haven't tried extensively, but I'm guessing that the above line of code will evaluate to False if you're not running pylab inline.
More importantly, it will raise a KeyError when you're not running the notebook or the pylab option, so you'll need to catch that and take that raised exception as "no" for running a notebook with pylab inline.
Finally,
get_ipython()
may throw aNameError
, and similar to the above, that of course also means you're not running IPython.I've only tested this minimally, but importing this in my IPython notebook, and then on the default Python cmdline does show it to work.
Let us know if this works for you.
What about trying:
You can check the matplotlib backend with:
To check for inline matplotlib in particular:
Note that with the IPython notebook, you can always display inline figures, regardless of the active matplotlib backend, with: