Skip to content

Commit

Permalink
Default to first kernel that matches the language #120
Browse files Browse the repository at this point in the history
  • Loading branch information
mwouts committed Nov 12, 2018
1 parent 277edc2 commit 8216a2d
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions jupytext/contentsmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
pass

from notebook.services.contents.filemanager import FileContentsManager
from jupyter_client.kernelspec import find_kernel_specs, get_kernel_spec

import jupytext
from .combine import combine_inputs_with_outputs
Expand All @@ -23,6 +24,15 @@
from .metadata_filter import metadata_filter_as_dict


def kernelspec_from_language(language):
"""Return the kernel specification for the first kernel with a matching language"""
for name in find_kernel_specs():
ks = get_kernel_spec(name)
if ks.language == language:
return {'name': name, 'language': language, 'display_name': ks.display_name}
return None


def _jupytext_writes(ext, format_name):
def _writes(nbk, version=nbformat.NO_CONVERT, **kwargs):
return jupytext.writes(nbk, version=version, ext=ext, format_name=format_name, **kwargs)
Expand Down Expand Up @@ -360,8 +370,15 @@ def get(self, path, content=True, type=None, format=None,
if model_outputs:
combine_inputs_with_outputs(model['content'], model_outputs['content'])
elif not fmt.endswith('.ipynb'):
self.notary.sign(model['content'])
self.mark_trusted_cells(model['content'], path)
nb = model['content']
language = nb.metadata.get('jupytext', {}).get('main_language', 'python')
if 'kernelspec' not in nb.metadata and language != 'python':
kernelspec = kernelspec_from_language(language)
if kernelspec:
nb.metadata['kernelspec'] = kernelspec

self.notary.sign(nb)
self.mark_trusted_cells(nb, path)

return model

Expand Down

0 comments on commit 8216a2d

Please sign in to comment.