-
Notifications
You must be signed in to change notification settings - Fork 394
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New argument
--set-kernel
in Jupytext command line (#230)
- Loading branch information
Showing
6 changed files
with
130 additions
and
24 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"""Find kernel specifications for a given language""" | ||
|
||
import sys | ||
from jupyter_client.kernelspec import find_kernel_specs, get_kernel_spec | ||
|
||
|
||
def set_kernelspec_from_language(notebook): | ||
"""Set the kernel specification based on the 'main_language' metadata""" | ||
language = notebook.metadata.get('jupytext', {}).get('main_language') | ||
if 'kernelspec' not in notebook.metadata and language: | ||
kernelspec = kernelspec_from_language(language) | ||
if kernelspec: | ||
notebook.metadata['kernelspec'] = kernelspec | ||
notebook.metadata.get('jupytext', {}).pop('main_language') | ||
|
||
|
||
def kernelspec_from_language(language): | ||
"""Return the python kernel that matches the current env, or the first kernel that matches the given language""" | ||
try: | ||
if language == 'python': | ||
# Return the kernel that matches the current Python executable | ||
for name in find_kernel_specs(): | ||
kernel_specs = get_kernel_spec(name) | ||
if kernel_specs.argv[0] == sys.executable: | ||
return {'name': name, 'language': language, 'display_name': kernel_specs.display_name} | ||
|
||
for name in find_kernel_specs(): | ||
kernel_specs = get_kernel_spec(name) | ||
if kernel_specs.language == language or (language == 'c++' and kernel_specs.language.startswith('C++')): | ||
return {'name': name, 'language': language, 'display_name': kernel_specs.display_name} | ||
except (KeyError, ValueError): | ||
pass | ||
return None |
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