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