##// END OF EJS Templates
Compile less files
Compile less files

File last commit:

r17094:85f1285b
r17293:7d8d6a5c
Show More
execute.py
165 lines | 5.4 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
import sys
Julia Evans
Fix queue import for Python 3
r17087 try:
from queue import Empty # Py 3
except ImportError:
from Queue import Empty # Py 2
MinRK
update execute preprocessor for msg spec 5
r17092 from IPython.utils.traitlets import List, Unicode
Julia Evans
Add preprocessor to execute notebooks
r17074
MinRK
update execute preprocessor for msg spec 5
r17092 from IPython.nbformat.current import reads, NotebookNode, writes
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
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."
)
MinRK
update execute preprocessor for msg spec 5
r17092 # FIXME: to be removed with nbformat v4
# map msg_type to v3 output_type
msg_type_map = {
"error" : "pyerr",
"execute_result" : "pyout",
}
# FIXME: to be removed with nbformat v4
# map mime-type to v3 mime-type keys
mime_map = {
"text/plain" : "text",
"text/html" : "html",
"image/svg+xml" : "svg",
"image/png" : "png",
"image/jpeg" : "jpeg",
"text/latex" : "latex",
"application/json" : "json",
"application/javascript" : "javascript",
}
extra_arguments = List(Unicode)
Julia Evans
Fix broken create/shutdown code
r17080 def _create_client(self):
MinRK
update execute preprocessor for msg spec 5
r17092 from IPython.kernel import KernelManager
Julia Evans
Add preprocessor to execute notebooks
r17074 self.km = KernelManager()
MinRK
make execute preprocessor timeout configurable
r17094 self.km.write_connection_file()
Julia Evans
Add preprocessor to execute notebooks
r17074 self.kc = self.km.client()
self.kc.start_channels()
MinRK
make execute preprocessor timeout configurable
r17094 self.km.start_kernel(extra_arguments=self.extra_arguments, stderr=open(os.devnull, 'w'))
Julia Evans
Add preprocessor to execute notebooks
r17074 self.iopub = self.kc.iopub_channel
self.shell = self.kc.shell_channel
Julia Evans
Replace execute(pass) with kernel_info()
r17081 self.shell.kernel_info()
MinRK
update execute preprocessor for msg spec 5
r17092 try:
MinRK
make execute preprocessor timeout configurable
r17094 self.shell.get_msg(timeout=self.timeout)
MinRK
update execute preprocessor for msg spec 5
r17092 except Empty:
self.log.error("Timeout waiting for kernel_info reply")
raise
MinRK
make execute preprocessor timeout configurable
r17094 # flush IOPub
while True:
try:
self.iopub.get_msg(block=True, timeout=0.25)
except Empty:
break
Julia Evans
Remove extraneous whitespace
r17077
Julia Evans
Fix broken create/shutdown code
r17080 def _shutdown_client(self):
self.kc.stop_channels()
self.km.shutdown_kernel()
del self.km
Julia Evans
Move kernel starting / stopping into preprocess()
r17079
Julia Evans
Fix broken create/shutdown code
r17080 def preprocess(self, nb, resources):
self._create_client()
Julia Evans
Move kernel starting / stopping into preprocess()
r17079 nb, resources = super(ExecutePreprocessor, self).preprocess(nb, resources)
Julia Evans
Fix broken create/shutdown code
r17080 self._shutdown_client()
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:
outputs = self.run_cell(self.shell, self.iopub, cell)
except Exception as e:
Julia Evans
Do self.log.error instead print >> sys.stderr
r17078 self.log.error("failed to run cell: " + repr(e))
self.log.error(str(cell.input))
MinRK
update execute preprocessor for msg spec 5
r17092 raise
Julia Evans
Add preprocessor to execute notebooks
r17074 cell.outputs = outputs
return cell, resources
MinRK
update execute preprocessor for msg spec 5
r17092 def run_cell(self, shell, iopub, cell):
msg_id = shell.execute(cell.input)
self.log.debug("Executing cell:\n%s", cell.input)
# wait for finish, with timeout
while True:
try:
MinRK
make execute preprocessor timeout configurable
r17094 msg = shell.get_msg(timeout=self.timeout)
MinRK
update execute preprocessor for msg spec 5
r17092 except Empty:
self.log.error("Timeout waiting for execute reply")
raise
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:
MinRK
make execute preprocessor timeout configurable
r17094 msg = iopub.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']
MinRK
update execute preprocessor for msg spec 5
r17092 if msg_type == 'status':
if content['execution_state'] == 'idle':
break
else:
continue
elif msg_type in {'execute_input', 'pyin'}:
continue
elif msg_type == 'clear_output':
outs = []
continue
out = NotebookNode(output_type=self.msg_type_map.get(msg_type, msg_type))
Jessica B. Hamrick
Properly set prompt numbers
r17090
# set the prompt number for the input and the output
if 'execution_count' in content:
cell['prompt_number'] = content['execution_count']
out.prompt_number = content['execution_count']
Julia Evans
Add preprocessor to execute notebooks
r17074 if msg_type == 'stream':
out.stream = content['name']
out.text = content['data']
MinRK
update execute preprocessor for msg spec 5
r17092 elif msg_type in ('display_data', 'execute_result'):
Julia Evans
Add preprocessor to execute notebooks
r17074 out['metadata'] = content['metadata']
MinRK
update execute preprocessor for msg spec 5
r17092 for mime, data in content['data'].items():
# map mime-type keys to nbformat v3 keys
# this will be unnecessary in nbformat v4
key = self.mime_map.get(mime, mime)
out[key] = data
elif msg_type == 'error':
Julia Evans
Add preprocessor to execute notebooks
r17074 out.ename = content['ename']
out.evalue = content['evalue']
out.traceback = content['traceback']
else:
Julia Evans
Do self.log.error instead print >> sys.stderr
r17078 self.log.error("unhandled iopub msg: " + msg_type)
Julia Evans
Remove extraneous whitespace
r17077
Julia Evans
Add preprocessor to execute notebooks
r17074 outs.append(out)
return outs