##// END OF EJS Templates
Replace execute(pass) with kernel_info()
Julia Evans -
Show More
@@ -1,115 +1,114
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 #-----------------------------------------------------------------------------
7 7 # Imports
8 8 #-----------------------------------------------------------------------------
9 9
10 10 import os
11 11 import sys
12 12
13 13 from Queue import Empty
14 14 from IPython.kernel import KernelManager
15 15 from IPython.nbformat.current import reads, NotebookNode, writes
16 16
17 17 from .base import Preprocessor
18 18
19 19
20 20 #-----------------------------------------------------------------------------
21 21 # Classes
22 22 #-----------------------------------------------------------------------------
23 23 class ExecutePreprocessor(Preprocessor):
24 24 """
25 25 Executes all the cells in a notebook
26 26 """
27 27 def __init__(self, extra_arguments=[], **kwargs):
28 28 """
29 29 Start an kernel to run the Python code
30 30 """
31 31 super(ExecutePreprocessor, self).__init__(**kwargs)
32 32 self.extra_arguments = []
33 33
34 34 def _create_client(self):
35 35 self.km = KernelManager()
36 36 self.km.start_kernel(extra_arguments=self.extra_arguments, stderr=open(os.devnull, 'w'))
37 37 self.kc = self.km.client()
38 38 self.kc.start_channels()
39 39 self.iopub = self.kc.iopub_channel
40 40 self.shell = self.kc.shell_channel
41 self.shell.execute("pass")
42 self.shell.get_msg()
41 self.shell.kernel_info()
43 42
44 43 def _shutdown_client(self):
45 44 self.kc.stop_channels()
46 45 self.km.shutdown_kernel()
47 46 del self.km
48 47
49 48 def preprocess(self, nb, resources):
50 49 self._create_client()
51 50 nb, resources = super(ExecutePreprocessor, self).preprocess(nb, resources)
52 51 self._shutdown_client()
53 52 return nb, resources
54 53
55 54 def preprocess_cell(self, cell, resources, cell_index):
56 55 """
57 56 Apply a transformation on each code cell. See base.py for details.
58 57 """
59 58 if cell.cell_type != 'code':
60 59 return cell, resources
61 60 try:
62 61 outputs = self.run_cell(self.shell, self.iopub, cell)
63 62 except Exception as e:
64 63 self.log.error("failed to run cell: " + repr(e))
65 64 self.log.error(str(cell.input))
66 65 sys.exit(1)
67 66 cell.outputs = outputs
68 67 return cell, resources
69 68
70 69 @staticmethod
71 70 def run_cell(shell, iopub, cell):
72 71 # print cell.input
73 72 shell.execute(cell.input)
74 73 # wait for finish, maximum 20s
75 74 shell.get_msg(timeout=20)
76 75 outs = []
77 76
78 77 while True:
79 78 try:
80 79 msg = iopub.get_msg(timeout=0.2)
81 80 except Empty:
82 81 break
83 82 msg_type = msg['msg_type']
84 83 if msg_type in ('status', 'pyin'):
85 84 continue
86 85 elif msg_type == 'clear_output':
87 86 outs = []
88 87 continue
89 88
90 89 content = msg['content']
91 90 # print msg_type, content
92 91 out = NotebookNode(output_type=msg_type)
93 92
94 93 if msg_type == 'stream':
95 94 out.stream = content['name']
96 95 out.text = content['data']
97 96 elif msg_type in ('display_data', 'pyout'):
98 97 out['metadata'] = content['metadata']
99 98 for mime, data in content['data'].iteritems():
100 99 attr = mime.split('/')[-1].lower()
101 100 # this gets most right, but fix svg+html, plain
102 101 attr = attr.replace('+xml', '').replace('plain', 'text')
103 102 setattr(out, attr, data)
104 103 if msg_type == 'pyout':
105 104 out.prompt_number = content['execution_count']
106 105 elif msg_type == 'pyerr':
107 106 out.ename = content['ename']
108 107 out.evalue = content['evalue']
109 108 out.traceback = content['traceback']
110 109 else:
111 110 self.log.error("unhandled iopub msg: " + msg_type)
112 111
113 112 outs.append(out)
114 113 return outs
115 114
General Comments 0
You need to be logged in to leave comments. Login now