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