visual studio code - How to enable YAML syntax highlighting in notebook cells for a custom magic? - Stack Overflow

admin2025-04-17  3

I have a Jupyter notebook in Visual Studio Code where I use custom magics to run cells in different languages through the Python interpreter. For SQL with DuckDB, I start the cell with %%duck_magic and select "SQL" in the cell language dropdown in the bottom right. However, for my YAML magic (%%yaml_magic), "YAML" doesn’t appear as an option in the dropdown.

Environment Details:

  • VS Code version: 1.92.0
  • Jupyter extension (Microsoft) version: v2023.11.1100101639
  • YAML extension (Red Hat) version: v1.15.0
  • Python version: 3.10.11

Steps to Reproduce:

  1. Create a new Jupyter notebook in VS Code.
  2. Add a cell with %%yaml_magic (or just an empty cell).
  3. Click the select cell language mode dropdown in the bottom right.
  4. Observe that "YAML" is missing from the list, while "SQL" and several other common languages other than python are available.

What I’ve Already Tried:

  • Hacking a workaround:

    • I copied the YAML syntax highlighting rules and created an override "Ruby" language extension (by modifying the tmLanguage.json file). This allows me to select "Ruby" to get YAML-like syntax, but it fails with indentation issues and is generally a hacky solution.
  • Testing behavior during reload:

    • If I run Developer: Reload Window, the dropdown briefly shows "YAML" and many other language options mid-reload, but once the reload completes, they disappear again.

Question:

Is there a way to enable YAML as a selectable language in the Jupyter notebook cell language dropdown in VS Code? Ideally, I'd like to associate YAML syntax highlighting with my custom %yaml_magic cells without workarounds like repurposing other languages.

转载请注明原文地址:http://anycun.com/QandA/1744834908a88276.html