Programatically determine if a user is calling code from the notebook
Question: I'm writing some software that creates matplotlib plots of simulation data.
Since these plotting routines are often running in a headless environment,
I've chosen to use the matplotlib object oriented interface explicitly assign
canvases to figures only just before they are saved. This means I cannot use
pylab or pyplot based solutions for this issue.

I've added some special sauce so the plots show up inline either by invoking a
`display` method on the plot object or by invoking `__repr__`. However, the
check I'm doing to determine if a user is running under IPython (checking for
`"__IPYTHON__" in dir(__builtin__)`) cannot discriminate whether the user is
in a notebook or just a regular terminal session where inline figures won't
work.

Is there some way to programatically check whether a code snippet has been
executed in a notebook, qt console, or terminal IPython session? Am I doing
something silly here - I haven't looked too closely at the display semantics
so perhaps I'm ignorant about some portion of the IPython internal API that
will take care of this for me.


Answer: Answerd many time : No you cant.

[How can I check if code is executed in the IPython
notebook?](http://stackoverflow.com/questions/15411967/how-can-i-check-if-
code-is-executed-in-the-ipython-notebook)

Same kernel can be connected to notebook, qtconsole and terminal at the same
time, even to many at once.

Your question is like a TV star asking "how can I know if the person watching
me on tv is male of female? ". It does not make sens.

Don't invoke `_repr_*_` yourself. Try to import display, make it no-op if
import fail. that should be sufficient to both in python or IPython not.

Better return object instead of displaying. The display hook will work by
itself in IPython if the object has a `_repr_png_` or equivalent.

