mwouts / jupytext

Jupyter Notebooks as Markdown Documents, Julia, Python or R scripts
https://jupytext.readthedocs.io
MIT License
6.6k stars 386 forks source link

Losing code cell language from MyST markdown inputs with `--pipe black` #1267

Open davidorme opened 1 month ago

davidorme commented 1 month ago

We're using jupytext --pipe black to automatically format Python code in MyST markdown notebooks (as part of pre-commit, but I don't think that's relevant here). The problem we're having is that (IIUC) the round trip through the percent format to pass it to black strips out the language specification on the code-cell directives. So given:

---
jupytext:
  formats: md:myst
  text_representation:
    extension: .md
    format_name: myst
    format_version: 0.13
kernelspec:
  display_name: Python 3 (ipykernel)
  language: python
  name: python3
---

# Quantum yield efficiency of photosynthesis

```{code-cell} python
# I'm some code
x = 1

Running that through `jupytext --pipe black` results in:

````md
---
jupytext:
  formats: md:myst
  text_representation:
    extension: .md
    format_name: myst
    format_version: 0.13
kernelspec:
  display_name: Python 3 (ipykernel)
  language: python
  name: python3
---

# Quantum yield efficiency of photosynthesis

```{code-cell}
# I'm some code
x = 1


That does affect other tools that rely on the language specification for syntax highlighting of code cells - we're using VSCode. I  wondered if this might be tackled by setting `cell_metadata_filter = "all"` but I think that language specification is not part of the cell metadata? I don't think that any of the other settings in [config.py](https://github.com/mwouts/jupytext/blob/main/src/jupytext/config.py) tackle this?
mwouts commented 1 month ago

Hi @davidorme , thank you for reporting this! We would need to make sure that this language information is preserved when the notebook is converted to a Jupyter notebook (the py:percent format will then, in turn, preserve the cell metadata).

Let me check with @chrisjsewell who knows that part better than I do, what happens to that language specification when the conversion occurs.

chrisjsewell commented 1 month ago

Will put it on the todo list to have a look 😅 but feel free to ping me again if I don't reply