jupytext icon indicating copy to clipboard operation
jupytext copied to clipboard

Using non-Python kernels still requires Python block labels

Open nathancarter opened this issue 4 years ago • 2 comments

Let us assume we have a Jupyter installation with at least the python3 and ir kernels, such as the one available in this Docker image, and that we've done pip install jupytext there. If we create a Markdown file like so...

# Hello!

testing...

```python
1+1
```

...and we run jupytext --to ipynb --execute that-file.md, everything works as expected (code cell gets executed, outputs get created, notebook is output successfully).

But if instead we try to do the same thing for a different kernel, like this...

# Hello!

testing...

```r
1+1
```

...and we run jupytext --to ipynb --execute --set-kernel ir that-new-file.md, we find that the entire file is treated as a single Markdown cell in the resulting notebook, no code cells were created, and thus nothing was executed. The same holds if we label the cell with ir instead of r, and it's still the same if we leave off the code block label altogether. (The resulting notebook does have the correct kernel in its metadata, although it's not doing any good.)

However, if we label the cell with python (!), then the desired behavior results (!!). That is, the output notebook retains the ir kernel, creates the expected code cell, and executes the code cell (which was marked as a Python cell in the input) using the correct (R) kernel, thus generating the correct output in the generated .ipynb file.

The expected behavior would be one of these:

  • Markdown code blocks with no markings are converted to Jupyter code cells and assumed to have the language of the kernel stipulated with --set-kernel, or
  • Markdown code blocks marked with the language that matches the --set-kernel parameter are converted to Jupyter code cells.

Update: I just tried it with a code block labeled with a capital R and this had the desired behavior, so that seems to be what I "should have been doing." I would not have known to do this without checking the metadata inside the .ipynb's JSON source, and noting that the language name associated with the kernel ir is called R, making me suspect that was the name that should be used in the Markdown source. Perhaps then the question is just whether we can create warnings that direct the user better so that they don't have to experiment and/or read .ipynb source? Or a more forgiving system that will accept the kernelspec's display_name or language or name as an acceptable label for a Markdown code block?

nathancarter avatar May 11 '21 18:05 nathancarter

Hi @nathancarter , thank you for taking the time to report this! I agree that a more user-friendly language matching or at least a warning would be useful here. I'll see what I can do in the next release.

mwouts avatar May 17 '21 20:05 mwouts

Oh that is interesting... indeed we have an issue here! The function set_main_and_cell_language does not seem to work as expected, at least I don't seem to get the expected main language on the sample notebooks above... I'll think a bit more about that!

mwouts avatar May 30 '21 16:05 mwouts