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