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