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