Skip to content

Instantly share code, notes, and snippets.

@jovianlin
Created April 27, 2017 14:58
Show Gist options
  • Save jovianlin/b1678fa61848cc7008609f1ef9802487 to your computer and use it in GitHub Desktop.
Save jovianlin/b1678fa61848cc7008609f1ef9802487 to your computer and use it in GitHub Desktop.
Determine if we're in an IPython notebook session
import sys
def is_kernel():
"""
Determine if we're in an IPython notebook session
Source: Source: http://stackoverflow.com/a/34092072
-------------------------------------------------
You can't detect that the frontend is a notebook with perfect precision,
because an IPython Kernel can have one or more different Jupyter frontends
with different capabilities (terminal console, qtconsole, notebook, etc.).
You can, however, identify that it is a Kernel and not plain terminal IPython.
"""
if 'IPython' not in sys.modules:
# IPython hasn't been imported, definitely not
return False
from IPython import get_ipython
# check for `kernel` attribute on the IPython instance
return getattr(get_ipython(), 'kernel', None) is not None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment