|
| 1 | +# Part of Spatial Math Toolbox for Python |
| 2 | +# Copyright (c) 2000 Peter Corke |
| 3 | +# MIT Licence, see details in top-level file: LICENCE |
| 4 | + |
| 5 | +""" |
| 6 | +Detect the interactive environment that Python is running in. |
| 7 | +
|
| 8 | +This module deliberately does not import Matplotlib, so it is cheap to import |
| 9 | +and works when Matplotlib is not installed. |
| 10 | +""" |
| 11 | + |
| 12 | +import sys |
| 13 | +from typing import Literal |
| 14 | + |
| 15 | +__all__ = ["environment", "isnotebook"] |
| 16 | + |
| 17 | +Environment = Literal["terminal", "ipython", "jupyter", "colab", "jupyterlite"] |
| 18 | + |
| 19 | + |
| 20 | +def environment() -> Environment: |
| 21 | + """ |
| 22 | + Determine the environment that Python is running in |
| 23 | +
|
| 24 | + :return: the environment |
| 25 | + :rtype: str |
| 26 | +
|
| 27 | + The result is one of: |
| 28 | +
|
| 29 | + ======================== ==================================================== |
| 30 | + ``"terminal"`` standard Python interpreter, or script |
| 31 | + ``"ipython"`` IPython running in a terminal |
| 32 | + ``"jupyter"`` Jupyter kernel, eg. JupyterLab, Notebook, VS Code or |
| 33 | + qtconsole |
| 34 | + ``"colab"`` Google Colab |
| 35 | + ``"jupyterlite"`` JupyterLite, running in the browser (Pyodide) |
| 36 | + ======================== ==================================================== |
| 37 | +
|
| 38 | + .. note:: The different Jupyter front ends (JupyterLab, classic Notebook, |
| 39 | + VS Code, Jupyter Desktop) cannot be reliably told apart from within the |
| 40 | + kernel, so all are reported as ``"jupyter"``. How figures will be |
| 41 | + rendered depends on the Matplotlib backend, not on the front end. |
| 42 | +
|
| 43 | + :seealso: :func:`isnotebook` |
| 44 | + """ |
| 45 | + # Colab and JupyterLite both also provide an IPython kernel, so test for |
| 46 | + # them first |
| 47 | + if "google.colab" in sys.modules: |
| 48 | + return "colab" |
| 49 | + if sys.platform == "emscripten": |
| 50 | + return "jupyterlite" |
| 51 | + |
| 52 | + try: |
| 53 | + shell = get_ipython().__class__.__name__ # type: ignore[name-defined] |
| 54 | + except NameError: |
| 55 | + return "terminal" # probably standard Python interpreter |
| 56 | + |
| 57 | + if shell == "ZMQInteractiveShell": |
| 58 | + return "jupyter" # Jupyter notebook or qtconsole |
| 59 | + elif shell == "TerminalInteractiveShell": |
| 60 | + return "ipython" |
| 61 | + else: |
| 62 | + return "terminal" # other type (?) |
| 63 | + |
| 64 | + |
| 65 | +def isnotebook() -> bool: |
| 66 | + """ |
| 67 | + Determine if code is being run from a Jupyter notebook |
| 68 | +
|
| 69 | + :return: True if running in a Jupyter kernel, Google Colab or JupyterLite |
| 70 | + :rtype: bool |
| 71 | +
|
| 72 | + :seealso: :func:`environment` |
| 73 | +
|
| 74 | + :references: |
| 75 | +
|
| 76 | + - https://stackoverflow.com/questions/15411967/how-can-i-check-if-code-is-executed-in-the-ipython-notebook/39662359#39662359 |
| 77 | + """ |
| 78 | + return environment() in ("jupyter", "colab", "jupyterlite") |
0 commit comments