astoff / code-cells.el

Emacs utilities for code split into cells, including Jupyter notebooks
GNU General Public License v3.0
180 stars 11 forks source link

evaluating cells with cell-level magics with emacs-jupyter raises an UsageError #18

Closed martibosch closed 9 months ago

martibosch commented 1 year ago

Consider the following notebook with an associated emacs-jupyter REPL:

# %%
%%timeit

N = 10000
list(range(N)) * 2

# %%
%time list(range(10000)) * 2

running code-cells-eval in the first cell raises a Jupyter error "UsageError: Line magic function %%timeit not found.". Instead, manually selecting the four lines of the first cell and running jupyter-eval-region runs correctly, giving the execution time in a buffer named *jupyter-output*.

The second cell runs properly (createing two buffers, one named *jupyter-result* with the result and another named *jupyter-output* with the measured execution time).

How can this be fixed? I suppose that it is an issue of how code-cells sends the cell code to emacs-jupyter given that jupyter-eval-region works properly.

Thank you in advance. Best, Martí

astoff commented 1 year ago

Puzzling. code-cells-eval sends the whole cell, including the comment header # %%, to the REPL. Could that be the cause of the trouble? In this case one would have to argue that this is not a bug in Jupyter...

martibosch commented 1 year ago

I suppose that including the comment header is indeed the cause of the trouble, because when I select the whole cell including the commend header and run it using jupyter-eval-region, I get the same error.

I therefore do not think that it is a bug in Jupyter as cell magic is supposed to be at the first line of the cell (regardless of comments). In any case, it is not a big deal and for other users encountering this issue, using the jupyter-eval-region approach as workaround is quite straightforward.

astoff commented 1 year ago

I guess I could add an option to modify this behavior, or even hard-code it. But I need to think of possible side-effects.