Created
April 27, 2017 14:58
-
-
Save jovianlin/b1678fa61848cc7008609f1ef9802487 to your computer and use it in GitHub Desktop.
Determine if we're in an IPython notebook session
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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