ImperialCollegeLondon / R2T2

Research References Tracking Tool
MIT License
14 stars 155 forks source link

Record cell number when using a jupyter notebook #42

Open valentinsulzer opened 4 years ago

valentinsulzer commented 4 years ago

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
  2. return cell number instead of file name if so (using get_ipython().execution_count)