These are chat archives for ipython/ipython

3rd
Jun 2015
Sylvain Corlay
@SylvainCorlay
Jun 03 2015 00:01
I was thinking of eerie.
Sylvain Corlay
@SylvainCorlay
Jun 03 2015 00:13
Damian Avila
@damianavila
Jun 03 2015 00:27
:sparkles:
Remi Rampin
@remram44
Jun 03 2015 19:11
I have an API that I want to generate different results depending of whether it's used from an IPython notebook or a batch script
basically, would create an object for embedding, or write to a file
is there a way to detect if it's called from the notebook or not?
Anton Akhmerov
@akhmerov
Jun 03 2015 19:36
@remram44 I think something like this should do:
from IPython import get_ipython
from IPython.kernel.zmq.zmqshell import ZMQInteractiveShell
ip = get_ipython()
if ip is None:
     print("Not ipython")
elif isinstance(ip, ZMQInteractiveShell):
     print("Looks like a notebook or a QT console")
else:
     print("ipython terminal")
I don't know of a more selective test.
Remi Rampin
@remram44
Jun 03 2015 19:39
I see
should I use that code?
Anton Akhmerov
@akhmerov
Jun 03 2015 19:41
Unless somebody has a better idea...
Remi Rampin
@remram44
Jun 03 2015 19:45
matplotlib uses an explicit %matplotlib inline so maybe opting in is the right way to go
Thomas Kluyver
@takluyver
Jun 03 2015 22:15
There is, by (current) design, no way to tell that you're specifically in a notebook, because multiple frontends can be connected to one kernel. @akhmerov 's code is fine to test if you're in a kernel.