Skip to content

Commit

Permalink
Allow the execute preprocessor to make use of an existing kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
SylvainCorlay committed Aug 7, 2018
1 parent d8bccf4 commit 5de5123
Showing 1 changed file with 25 additions and 20 deletions.
45 changes: 25 additions & 20 deletions nbconvert/preprocessors/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class ExecutePreprocessor(Preprocessor):
"""
)
).tag(config=True)

@default('kernel_name')
def _kernel_name_default(self):
try:
Expand All @@ -167,7 +167,6 @@ def _kernel_name_default(self):
'the ExecutePreprocessor and you have not set '
'self.nb to be able to use that to infer the '
'kernel_name.')


raise_on_iopub_timeout = Bool(False,
help=dedent(
Expand Down Expand Up @@ -252,7 +251,7 @@ def start_new_kernel(self, **kwargs):
km = self.kernel_manager_class(kernel_name=self.kernel_name,
config=self.config)
km.start_kernel(extra_arguments=self.extra_arguments, **kwargs)

kc = km.client()
kc.start_channels()
try:
Expand All @@ -263,18 +262,18 @@ def start_new_kernel(self, **kwargs):
raise
kc.allow_stdin = False
return km, kc

@contextmanager
def setup_preprocessor(self, nb, resources):
def setup_preprocessor(self, nb, resources, kernel=None):
"""
Context manager for setting up the class to execute a notebook.
The assigns `nb` to `self.nb` where it will be modified in-place. It also creates
The assigns `nb` to `self.nb` where it will be modified in-place. It also creates
and assigns the Kernel Manager (`self.km`) and Kernel Client(`self.kc`).
It is intended to yield to a block that will execute codeself.
When control returns from the yield it stops the client's zmq channels, shuts
When control returns from the yield it stops the client's zmq channels, shuts
down the kernel, and removes the now unused attributes.
Parameters
Expand All @@ -285,6 +284,8 @@ def setup_preprocessor(self, nb, resources):
Additional resources used in the conversion process. For example,
passing ``{'metadata': {'path': run_path}}`` sets the
execution path to ``run_path``.
kernel : Optional pair for (kernel manager, kernel client)
If such a pair is passed, it will be used for code execution.
Returns
-------
Expand All @@ -298,17 +299,21 @@ def setup_preprocessor(self, nb, resources):
# clear display_id map
self._display_id_map = {}

self.km, self.kc = self.start_new_kernel(cwd=path)
try:
# Yielding unbound args for more easier understanding and downstream consumption
yield nb, self.km, self.kc
finally:
self.kc.stop_channels()
self.km.shutdown_kernel(now=self.shutdown_kernel == 'immediate')

for attr in ['nb', 'km', 'kc']:
delattr(self, attr)

if kernel is None:
self.km, self.kc = self.start_new_kernel(cwd=path)
try:
# Yielding unbound args for more easier understanding and downstream consumption
yield nb, self.km, self.kc
finally:
self.kc.stop_channels()
self.km.shutdown_kernel(now=self.shutdown_kernel == 'immediate')

for attr in ['nb', 'km', 'kc']:
delattr(self, attr)
else:
self.km, self.kc = kernel
return nb, self.kc

def preprocess(self, nb, resources):
"""
Preprocess notebook executing each code cell.
Expand Down

0 comments on commit 5de5123

Please sign in to comment.