jupyter icon indicating copy to clipboard operation
jupyter copied to clipboard

IndentationError: unexpected indent

Open mankoff opened this issue 4 years ago • 1 comments

Given the following code:

for i in [0,1,2]:
    print(i)
    print(i*2)

While debugging it would be nice to be able to run sections of code from inside a loop. I can do one line, and it will print. But if I try to past two lines into the REPL, I get an error message: IndentationError: unexpected indent.

If I recall, when I was using pure Python with Org Babel blocks, this was not an issue. Is there some way to allow chomping all white space equal to the whitespace from the first line? If it deosn't exist, can this be tagged as a feature request?

Thank you.

mankoff avatar Apr 27 '20 21:04 mankoff

As mentioned by @matteha in the issue #280, this is due to the use of string-trim in the function jupyter-repl-execute-cell, specifically the line: (let ((code (string-trim (jupyter-repl-cell-code))))

By default, string-trim removes any leading tabs or spaces from the beginning of the cell, breaking python indentation. As a workaround, I have changed this line to: (let ((code (string-trim (jupyter-repl-cell-code) "[\n\r]+")))

In this new version, we only trim away newlines but not tabs or spaces, thereby keeping indentation consistent between the first and second lines. @nnicandro does this look like a reasonable change? Happy to make a pull request.

egr95 avatar Sep 14 '23 00:09 egr95