Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Recreate old behaviour of ExecutePreprocessor(kernel_name="") #886

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions nbconvert/preprocessors/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

import warnings

from textwrap import dedent
from contextlib import contextmanager

Expand Down Expand Up @@ -161,7 +163,7 @@ class ExecutePreprocessor(Preprocessor):
@default('kernel_name')
def _kernel_name_default(self):
try:
return self.nb.metadata.get('kernelspec', {}).get('name', 'python')
return find_kernel_name(self.nb)
except AttributeError:
raise AttributeError('You did not specify a kernel_name for '
'the ExecutePreprocessor and you have not set '
Expand Down Expand Up @@ -248,7 +250,19 @@ def start_new_kernel(self, **kwargs):
kc : KernelClient
Kernel client as created by the kernel manager `km`.
"""
km = self.kernel_manager_class(kernel_name=self.kernel_name,

# Because of an old API, an empty kernel string should be interpreted as indicating
# that you want to use the notebook's metadata specified kernel.
# We use find_kernel_name to do this.
if not self.kernel_name:
kernel_name = find_kernel_name(self.nb)
warnings.warn("Setting kernel_name to '' as a way to use the kernel in a notebook metadata's is deprecated as of 5.4. \n"
"Instead, do not set kernel_name, this is now default behaviour.", DeprecationWarning, stacklevel=2)
else:
kernel_name = self.kernel_name


km = self.kernel_manager_class(kernel_name=kernel_name,
config=self.config)
km.start_kernel(extra_arguments=self.extra_arguments, **kwargs)

Expand Down Expand Up @@ -516,6 +530,20 @@ def run_cell(self, cell, cell_index=0):

return exec_reply, outs

def find_kernel_name(nb):
"""This is a utility that finds kernel names from notebook metadata.

Parameters
----------
nb : NotebookNode
The notebook that has a kernelspec defined in its metadata.

Returns
-------
kernel_name: str
The string representing the kernel name found in the metadata.
"""
return nb.metadata.get('kernelspec', {}).get('name', 'python')

def executenb(nb, cwd=None, km=None, **kwargs):
"""Execute a notebook's code, updating outputs within the notebook object.
Expand Down
32 changes: 32 additions & 0 deletions nbconvert/preprocessors/tests/files/UnicodePy3.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u2603\n"
]
}
],
"source": [
"print('\u2603')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
31 changes: 29 additions & 2 deletions nbconvert/preprocessors/tests/test_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
import pytest

from .base import PreprocessorTestsBase
from ..execute import ExecutePreprocessor, CellExecutionError, executenb
from ..execute import ExecutePreprocessor, CellExecutionError, executenb, find_kernel_name

from nbconvert.filters import strip_ansi
from testpath import modified_env
from traitlets import TraitError
from jupyter_client.kernelspec import KernelSpecManager
from ipython_genutils.py3compat import string_types

from nbconvert.filters import strip_ansi

addr_pat = re.compile(r'0x[0-9a-f]{7,9}')
ipython_input_pat = re.compile(r'<ipython-input-\d+-[0-9a-f]+>')
current_dir = os.path.dirname(__file__)
Expand Down Expand Up @@ -151,6 +154,21 @@ def test_empty_path(self):
res['metadata']['path'] = ''
input_nb, output_nb = self.run_notebook(filename, {}, res)
self.assert_notebooks_equal(input_nb, output_nb)

@pytest.mark.xfail("python3" not in KernelSpecManager().find_kernel_specs(),
reason="requires a python3 kernelspec")
def test_empty_kernel_name(self):
"""Can kernel in nb metadata be found when an empty string is passed?

Note: this pattern should be discouraged in practice.
Passing in no kernel_name to ExecutePreprocessor is recommended instead.
"""
filename = os.path.join(current_dir, 'files', 'UnicodePy3.ipynb')
res = self.build_resources()
input_nb, output_nb = self.run_notebook(filename, {"kernel_name": ""}, res)
self.assert_notebooks_equal(input_nb, output_nb)
with pytest.raises(TraitError):
input_nb, output_nb = self.run_notebook(filename, {"kernel_name": None}, res)

def test_disable_stdin(self):
"""Test disabling standard input"""
Expand Down Expand Up @@ -265,6 +283,15 @@ def test_custom_kernel_manager(self):
for method, call_count in expected:
self.assertNotEqual(call_count, 0, '{} was called'.format(method))

def test_find_kernel_name(self):
current_dir = os.path.dirname(__file__)
filename = os.path.join(current_dir, 'files', 'UnicodePy3.ipynb')

with io.open(filename) as f:
input_nb = nbformat.read(f, 4)

assert find_kernel_name(input_nb) == "python3"

def test_execute_function(self):
# Test the executenb() convenience API
current_dir = os.path.dirname(__file__)
Expand Down