Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Record cell number when using a jupyter notebook #42

Open
valentinsulzer opened this issue Sep 2, 2020 · 0 comments
Open

Record cell number when using a jupyter notebook #42

valentinsulzer opened this issue Sep 2, 2020 · 0 comments

Comments

@valentinsulzer
Copy link
Collaborator

valentinsulzer commented Sep 2, 2020

When using a notebook, it makes more sense to record the cell number than the name of the file. It might be possible to:

  1. automatically detect if using a notebook environment
def is_notebook():
    try:
        shell = get_ipython().__class__.__name__
        if shell == "ZMQInteractiveShell":  # pragma: no cover
            # Jupyter notebook or qtconsole
            cfg = get_ipython().config
            nb = len(cfg["InteractiveShell"].keys()) == 0
            return nb
        elif shell == "TerminalInteractiveShell":  # pragma: no cover
            return False  # Terminal running IPython
        elif shell == "Shell":  # pragma: no cover
            return True  # Google Colab notebook
        else:
            return False  # Other type (?)
    except NameError:
        return False  # Probably standard Python interpreter
  1. return cell number instead of file name if so (using get_ipython().execution_count)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant