##// END OF EJS Templates
Don't make it configurable
Jessica B. Hamrick -
Show More
@@ -1,117 +1,112 b''
1 1 """Module containing a preprocessor that removes the outputs from code cells"""
2 2
3 3 # Copyright (c) IPython Development Team.
4 4 # Distributed under the terms of the Modified BSD License.
5 5
6 6 import os
7 7
8 8 try:
9 9 from queue import Empty # Py 3
10 10 except ImportError:
11 11 from Queue import Empty # Py 2
12 12
13 13 from IPython.utils.traitlets import List, Unicode, Bool
14 14
15 15 from IPython.nbformat.v4 import output_from_msg
16 16 from .base import Preprocessor
17 17 from IPython.utils.traitlets import Integer
18 18
19 19
20 20 class ExecutePreprocessor(Preprocessor):
21 21 """
22 22 Executes all the cells in a notebook
23 23 """
24 24
25 25 timeout = Integer(30, config=True,
26 26 help="The time to wait (in seconds) for output from executions."
27 27 )
28 28
29 allow_stdin = Bool(
30 False, config=True,
31 help="Whether stdin should be enabled when executing cells"
32 )
33
34 29 extra_arguments = List(Unicode)
35 30
36 31 def preprocess(self, nb, resources):
37 32 from IPython.kernel import run_kernel
38 33 kernel_name = nb.metadata.get('kernelspec', {}).get('name', 'python')
39 34 self.log.info("Executing notebook with kernel: %s" % kernel_name)
40 35 with run_kernel(kernel_name=kernel_name,
41 36 extra_arguments=self.extra_arguments,
42 37 stderr=open(os.devnull, 'w')) as kc:
43 38 self.kc = kc
44 self.kc.allow_stdin = self.allow_stdin
39 self.kc.allow_stdin = False
45 40 nb, resources = super(ExecutePreprocessor, self).preprocess(nb, resources)
46 41 return nb, resources
47 42
48 43 def preprocess_cell(self, cell, resources, cell_index):
49 44 """
50 45 Apply a transformation on each code cell. See base.py for details.
51 46 """
52 47 if cell.cell_type != 'code':
53 48 return cell, resources
54 49 try:
55 50 outputs = self.run_cell(cell)
56 51 except Exception as e:
57 52 self.log.error("failed to run cell: " + repr(e))
58 53 self.log.error(str(cell.source))
59 54 raise
60 55 cell.outputs = outputs
61 56 return cell, resources
62 57
63 58 def run_cell(self, cell):
64 59 msg_id = self.kc.execute(cell.source)
65 60 self.log.debug("Executing cell:\n%s", cell.source)
66 61 # wait for finish, with timeout
67 62 while True:
68 63 try:
69 64 msg = self.kc.shell_channel.get_msg(timeout=self.timeout)
70 65 except Empty:
71 66 self.log.error("Timeout waiting for execute reply")
72 67 raise
73 68 if msg['parent_header'].get('msg_id') == msg_id:
74 69 break
75 70 else:
76 71 # not our reply
77 72 continue
78 73
79 74 outs = []
80 75
81 76 while True:
82 77 try:
83 78 msg = self.kc.iopub_channel.get_msg(timeout=self.timeout)
84 79 except Empty:
85 80 self.log.warn("Timeout waiting for IOPub output")
86 81 break
87 82 if msg['parent_header'].get('msg_id') != msg_id:
88 83 # not an output from our execution
89 84 continue
90 85
91 86 msg_type = msg['msg_type']
92 87 self.log.debug("output: %s", msg_type)
93 88 content = msg['content']
94 89
95 90 # set the prompt number for the input and the output
96 91 if 'execution_count' in content:
97 92 cell['execution_count'] = content['execution_count']
98 93
99 94 if msg_type == 'status':
100 95 if content['execution_state'] == 'idle':
101 96 break
102 97 else:
103 98 continue
104 99 elif msg_type == 'execute_input':
105 100 continue
106 101 elif msg_type == 'clear_output':
107 102 outs = []
108 103 continue
109 104
110 105 try:
111 106 out = output_from_msg(msg)
112 107 except ValueError:
113 108 self.log.error("unhandled iopub msg: " + msg_type)
114 109 else:
115 110 outs.append(out)
116 111
117 112 return outs
General Comments 0
You need to be logged in to leave comments. Login now