##// END OF EJS Templates
Add an option to disable stdin
Jessica B. Hamrick -
Show More
@@ -1,111 +1,117 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
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
29 allow_stdin = Bool(
30 False, config=True,
31 help="Whether stdin should be enabled when executing cells"
32 )
28
33
29 extra_arguments = List(Unicode)
34 extra_arguments = List(Unicode)
30
35
31 def preprocess(self, nb, resources):
36 def preprocess(self, nb, resources):
32 from IPython.kernel import run_kernel
37 from IPython.kernel import run_kernel
33 kernel_name = nb.metadata.get('kernelspec', {}).get('name', 'python')
38 kernel_name = nb.metadata.get('kernelspec', {}).get('name', 'python')
34 self.log.info("Executing notebook with kernel: %s" % kernel_name)
39 self.log.info("Executing notebook with kernel: %s" % kernel_name)
35 with run_kernel(kernel_name=kernel_name,
40 with run_kernel(kernel_name=kernel_name,
36 extra_arguments=self.extra_arguments,
41 extra_arguments=self.extra_arguments,
37 stderr=open(os.devnull, 'w')) as kc:
42 stderr=open(os.devnull, 'w')) as kc:
38 self.kc = kc
43 self.kc = kc
44 self.kc.allow_stdin = self.allow_stdin
39 nb, resources = super(ExecutePreprocessor, self).preprocess(nb, resources)
45 nb, resources = super(ExecutePreprocessor, self).preprocess(nb, resources)
40 return nb, resources
46 return nb, resources
41
47
42 def preprocess_cell(self, cell, resources, cell_index):
48 def preprocess_cell(self, cell, resources, cell_index):
43 """
49 """
44 Apply a transformation on each code cell. See base.py for details.
50 Apply a transformation on each code cell. See base.py for details.
45 """
51 """
46 if cell.cell_type != 'code':
52 if cell.cell_type != 'code':
47 return cell, resources
53 return cell, resources
48 try:
54 try:
49 outputs = self.run_cell(cell)
55 outputs = self.run_cell(cell)
50 except Exception as e:
56 except Exception as e:
51 self.log.error("failed to run cell: " + repr(e))
57 self.log.error("failed to run cell: " + repr(e))
52 self.log.error(str(cell.source))
58 self.log.error(str(cell.source))
53 raise
59 raise
54 cell.outputs = outputs
60 cell.outputs = outputs
55 return cell, resources
61 return cell, resources
56
62
57 def run_cell(self, cell):
63 def run_cell(self, cell):
58 msg_id = self.kc.execute(cell.source)
64 msg_id = self.kc.execute(cell.source)
59 self.log.debug("Executing cell:\n%s", cell.source)
65 self.log.debug("Executing cell:\n%s", cell.source)
60 # wait for finish, with timeout
66 # wait for finish, with timeout
61 while True:
67 while True:
62 try:
68 try:
63 msg = self.kc.shell_channel.get_msg(timeout=self.timeout)
69 msg = self.kc.shell_channel.get_msg(timeout=self.timeout)
64 except Empty:
70 except Empty:
65 self.log.error("Timeout waiting for execute reply")
71 self.log.error("Timeout waiting for execute reply")
66 raise
72 raise
67 if msg['parent_header'].get('msg_id') == msg_id:
73 if msg['parent_header'].get('msg_id') == msg_id:
68 break
74 break
69 else:
75 else:
70 # not our reply
76 # not our reply
71 continue
77 continue
72
78
73 outs = []
79 outs = []
74
80
75 while True:
81 while True:
76 try:
82 try:
77 msg = self.kc.iopub_channel.get_msg(timeout=self.timeout)
83 msg = self.kc.iopub_channel.get_msg(timeout=self.timeout)
78 except Empty:
84 except Empty:
79 self.log.warn("Timeout waiting for IOPub output")
85 self.log.warn("Timeout waiting for IOPub output")
80 break
86 break
81 if msg['parent_header'].get('msg_id') != msg_id:
87 if msg['parent_header'].get('msg_id') != msg_id:
82 # not an output from our execution
88 # not an output from our execution
83 continue
89 continue
84
90
85 msg_type = msg['msg_type']
91 msg_type = msg['msg_type']
86 self.log.debug("output: %s", msg_type)
92 self.log.debug("output: %s", msg_type)
87 content = msg['content']
93 content = msg['content']
88
94
89 # set the prompt number for the input and the output
95 # set the prompt number for the input and the output
90 if 'execution_count' in content:
96 if 'execution_count' in content:
91 cell['execution_count'] = content['execution_count']
97 cell['execution_count'] = content['execution_count']
92
98
93 if msg_type == 'status':
99 if msg_type == 'status':
94 if content['execution_state'] == 'idle':
100 if content['execution_state'] == 'idle':
95 break
101 break
96 else:
102 else:
97 continue
103 continue
98 elif msg_type == 'execute_input':
104 elif msg_type == 'execute_input':
99 continue
105 continue
100 elif msg_type == 'clear_output':
106 elif msg_type == 'clear_output':
101 outs = []
107 outs = []
102 continue
108 continue
103
109
104 try:
110 try:
105 out = output_from_msg(msg)
111 out = output_from_msg(msg)
106 except ValueError:
112 except ValueError:
107 self.log.error("unhandled iopub msg: " + msg_type)
113 self.log.error("unhandled iopub msg: " + msg_type)
108 else:
114 else:
109 outs.append(out)
115 outs.append(out)
110
116
111 return outs
117 return outs
General Comments 0
You need to be logged in to leave comments. Login now