-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fallback to kernelspec to check if it's a Python notebook (#12875)
## Summary This PR adds a fallback logic for `is_python_notebook` to check the `kernelspec.language` field. Reference implementation in VS Code: https://github.com/microsoft/vscode/blob/1c31e758985efe11bc0453a45ea0bb6887e670a4/extensions/ipynb/src/deserializers.ts#L20-L22 It's also required for the kernel to provide the `language` they're implementing based on https://jupyter-client.readthedocs.io/en/stable/kernels.html#kernel-specs reference although that's for the `kernel.json` file but is also included in the notebook metadata. Closes: #12281 ## Test Plan Add a test case for `is_python_notebook` and include the test notebook for round trip validation. The test notebook contains two cells, one is JavaScript (denoted via the `vscode.languageId` metadata) and the other is Python (no metadata). The notebook metadata only contains `kernelspec` and the `language_info` is absent. I also verified that this is a valid notebook by opening it in Jupyter Lab, VS Code and using `nbformat` validator.
- Loading branch information
1 parent
89c8b49
commit 2520ebb
Showing
3 changed files
with
89 additions
and
24 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
crates/ruff_notebook/resources/test/fixtures/jupyter/kernelspec_language.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Kernel spec language\n", | ||
"\n", | ||
"This is a test notebook for validating the fallback logic of `is_python_notebook` to check `kernelspec.language` if `language_info` is absent.\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"vscode": { | ||
"languageId": "javascript" | ||
} | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"function add(x, y) {\n", | ||
" return x + y;\n", | ||
"}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n", | ||
"\n", | ||
"print(\"hello world\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters