##// END OF EJS Templates
Removed pesky decorative quotes from the OTHER test function
Removed pesky decorative quotes from the OTHER test function

File last commit:

r20747:b48f077e
r20902:57e30142
Show More
execute.py
141 lines | 4.3 KiB | text/x-python | PythonLexer
Julia Evans
Add preprocessor to execute notebooks
r17074 """Module containing a preprocessor that removes the outputs from code cells"""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import os
Jessica B. Hamrick
Add option to interrupt kernel after execution timeout
r20747 from textwrap import dedent
Julia Evans
Add preprocessor to execute notebooks
r17074
Julia Evans
Fix queue import for Python 3
r17087 try:
from queue import Empty # Py 3
except ImportError:
from Queue import Empty # Py 2
Jessica B. Hamrick
Add option to interrupt kernel after execution timeout
r20747 from IPython.utils.traitlets import List, Unicode, Bool
Julia Evans
Add preprocessor to execute notebooks
r17074
MinRK
Don't use nbformat.current in nbconvert
r18605 from IPython.nbformat.v4 import output_from_msg
Julia Evans
Add preprocessor to execute notebooks
r17074 from .base import Preprocessor
MinRK
make execute preprocessor timeout configurable
r17094 from IPython.utils.traitlets import Integer
Julia Evans
Add preprocessor to execute notebooks
r17074
MinRK
Don't use nbformat.current in nbconvert
r18605
Julia Evans
Add preprocessor to execute notebooks
r17074 class ExecutePreprocessor(Preprocessor):
"""
Executes all the cells in a notebook
"""
MinRK
update execute preprocessor for msg spec 5
r17092
MinRK
make execute preprocessor timeout configurable
r17094 timeout = Integer(30, config=True,
help="The time to wait (in seconds) for output from executions."
)
Jessica B. Hamrick
Add option to interrupt kernel after execution timeout
r20747
interrupt_on_timeout = Bool(
False, config=True,
help=dedent(
"""
If execution of a cell times out, interrupt the kernel and
continue executing other cells rather than throwing an error and
stopping.
"""
)
)
Jessica B. Hamrick
Remove unused import
r20532
MinRK
update execute preprocessor for msg spec 5
r17092 extra_arguments = List(Unicode)
Julia Evans
Move kernel starting / stopping into preprocess()
r17079
Julia Evans
Fix broken create/shutdown code
r17080 def preprocess(self, nb, resources):
Jessica B. Hamrick
Handle the case where the path is empty
r20648 path = resources.get('metadata', {}).get('path', '')
if path == '':
path = None
Jessica B. Hamrick
Run kernel in the directory of the notebook
r20646
Jessica B. Hamrick
Add option to interrupt kernel after execution timeout
r20747 from IPython.kernel.manager import start_new_kernel
Thomas Kluyver
Simplify ExecutePreprocessor using new context manager...
r17822 kernel_name = nb.metadata.get('kernelspec', {}).get('name', 'python')
MinRK
add info-level log indicating that notebook is being executed
r18458 self.log.info("Executing notebook with kernel: %s" % kernel_name)
Jessica B. Hamrick
Add option to interrupt kernel after execution timeout
r20747 self.km, self.kc = start_new_kernel(
kernel_name=kernel_name,
extra_arguments=self.extra_arguments,
stderr=open(os.devnull, 'w'),
cwd=path)
self.kc.allow_stdin = False
try:
Thomas Kluyver
Simplify ExecutePreprocessor using new context manager...
r17822 nb, resources = super(ExecutePreprocessor, self).preprocess(nb, resources)
Jessica B. Hamrick
Add option to interrupt kernel after execution timeout
r20747 finally:
self.kc.stop_channels()
self.km.shutdown_kernel(now=True)
Julia Evans
Move kernel starting / stopping into preprocess()
r17079 return nb, resources
Julia Evans
Add preprocessor to execute notebooks
r17074 def preprocess_cell(self, cell, resources, cell_index):
"""
Apply a transformation on each code cell. See base.py for details.
"""
if cell.cell_type != 'code':
return cell, resources
try:
Thomas Kluyver
Use client.execute in execute preoprocessor
r19214 outputs = self.run_cell(cell)
Julia Evans
Add preprocessor to execute notebooks
r17074 except Exception as e:
Julia Evans
Do self.log.error instead print >> sys.stderr
r17078 self.log.error("failed to run cell: " + repr(e))
MinRK
update nbconvert to nbformat 4
r18580 self.log.error(str(cell.source))
MinRK
update execute preprocessor for msg spec 5
r17092 raise
Julia Evans
Add preprocessor to execute notebooks
r17074 cell.outputs = outputs
return cell, resources
Thomas Kluyver
Use client.execute in execute preoprocessor
r19214 def run_cell(self, cell):
msg_id = self.kc.execute(cell.source)
MinRK
update nbconvert to nbformat 4
r18580 self.log.debug("Executing cell:\n%s", cell.source)
MinRK
update execute preprocessor for msg spec 5
r17092 # wait for finish, with timeout
while True:
try:
Thomas Kluyver
Use client.execute in execute preoprocessor
r19214 msg = self.kc.shell_channel.get_msg(timeout=self.timeout)
MinRK
update execute preprocessor for msg spec 5
r17092 except Empty:
self.log.error("Timeout waiting for execute reply")
Jessica B. Hamrick
Add option to interrupt kernel after execution timeout
r20747 if self.interrupt_on_timeout:
self.log.error("Interrupting kernel")
self.km.interrupt_kernel()
break
else:
raise
MinRK
update execute preprocessor for msg spec 5
r17092 if msg['parent_header'].get('msg_id') == msg_id:
break
else:
# not our reply
continue
Julia Evans
Add preprocessor to execute notebooks
r17074 outs = []
Julia Evans
Remove extraneous whitespace
r17077
Julia Evans
Add preprocessor to execute notebooks
r17074 while True:
try:
Thomas Kluyver
Use client.execute in execute preoprocessor
r19214 msg = self.kc.iopub_channel.get_msg(timeout=self.timeout)
Julia Evans
Add preprocessor to execute notebooks
r17074 except Empty:
MinRK
update execute preprocessor for msg spec 5
r17092 self.log.warn("Timeout waiting for IOPub output")
Julia Evans
Add preprocessor to execute notebooks
r17074 break
MinRK
update execute preprocessor for msg spec 5
r17092 if msg['parent_header'].get('msg_id') != msg_id:
# not an output from our execution
continue
Jessica B. Hamrick
Properly set prompt numbers
r17090
Julia Evans
Add preprocessor to execute notebooks
r17074 msg_type = msg['msg_type']
MinRK
update execute preprocessor for msg spec 5
r17092 self.log.debug("output: %s", msg_type)
Jessica B. Hamrick
Properly set prompt numbers
r17090 content = msg['content']
Jessica B. Hamrick
Make sure prompt numbers are actually set
r17824
# set the prompt number for the input and the output
if 'execution_count' in content:
MinRK
s/prompt_number/execution_count in nbformat 4
r18587 cell['execution_count'] = content['execution_count']
Jessica B. Hamrick
Make sure prompt numbers are actually set
r17824
MinRK
update execute preprocessor for msg spec 5
r17092 if msg_type == 'status':
if content['execution_state'] == 'idle':
break
else:
continue
MinRK
add nbformat.output_from_msg...
r18582 elif msg_type == 'execute_input':
MinRK
update execute preprocessor for msg spec 5
r17092 continue
elif msg_type == 'clear_output':
outs = []
continue
MinRK
update nbconvert to nbformat 4
r18580
MinRK
add nbformat.output_from_msg...
r18582 try:
out = output_from_msg(msg)
except ValueError:
Julia Evans
Do self.log.error instead print >> sys.stderr
r17078 self.log.error("unhandled iopub msg: " + msg_type)
MinRK
add nbformat.output_from_msg...
r18582 else:
outs.append(out)
Julia Evans
Remove extraneous whitespace
r17077
Julia Evans
Add preprocessor to execute notebooks
r17074 return outs