Notebook: On a SyntaxError or traceback, jump the cursor to the error.

Created on 26 May 2017  路  8Comments  路  Source: jupyter/notebook

If the user has a large notebook, and they've made some coding error that causes either a SyntaxError or a runtime error and traceback, it can be very hard to tell what's happened — whether an error has occurred, and if so where.

I currently work around this by searching for strings like SyntaxError and most recent call in the page. But it seems that it would be much saner to jump the user to the place where the error happened, and highlight things in red.

(There are probably fancy things the notebook could do, like highlighting the exact line that errored, and displaying the traceback in the huge amount of blank margin space that's typical on 16:9 monitors... but it would be a great start to put the cursor in the right place).

Most helpful comment

It's 2019 and would love to see if technology has matured for this feature for Jupyter notebook or lab?

All 8 comments

This is definitely a good idea, but it's not trivial to get it right.

It doesn't seem that hard to get the basic case right! This snippet seems to correctly print the location of the error on the Python side:

import re
import sys
try:
    import nosuchthing
except:
    exc_type, value, tb = sys.exc_info()
    line = tb.tb_lineno
    context = tb.tb_frame and tb.tb_frame.f_code and tb.tb_frame.f_code.co_filename
    if context:
        match = re.match(r"<ipython-input-([0-9]+)-", context)
        input_id = match and match.group(0)

    print "Input:", input_id, "line", line
    raise

tb-demo

The CSS selector path for the line in question would be something like

  • Find the right div.input_prompt (ideally that div should get the number added to it as an id, currently I see <div class="prompt input_prompt">In&nbsp;[1182]:</div>, so you'd need to walk them to find the right one at present)
  • within that DOM subtree, cssselect on div.CodeMirror-code pre and you have a reference to the line for highlighting / jumping.

@takluyver am I missing anything important there? If not, I might be tempted to make a quick PR for this, though I'd appreciate working with someone who's familiar with Jupyter Notebook's JS front end to do this faster/better.

I think the tricky bits are:

  • Working out how the line info gets to the frontend in a language agnostic way (the frontend shouldn't be regexing for IPython tracebacks)
  • Codemirror has something called 'overlays', IIRC, for this kind of information, but I don't know how to use them.

    • When and how do we clear the highlighting? If the line is modified? If any part of the cell is modified?

It's all doable if you're keen to work on it, but it's a bit more than a 'quick PR' - it's going to require changes in at least a couple of repositories (this one and ipykernel).

  • I wasn't proposing regexing for Python tracebacks in the frontend; my example would be backend code that is added to whatever piece of python executes the cell. The regex is just there to recognise the Notebook cells from within the Python interpreter. An initial PR would probably just work with Python, but it would include an interface for signalling to the front end, so that support for other languages could be added later. Having said that, I don't know how the Jupyter Notebook front and back ends talk to each other, but I presume a new "hey there's a traceback on line X of cell Y" call/event could be added?
  • I think that skips needing to do anything with Codemirror Overlays?
  • I think "cell modified or any cell executed" would be a good time to clear the highlight.

It's 2019 and would love to see if technology has matured for this feature for Jupyter notebook or lab?

Was this page helpful?
0 / 5 - 0 ratings