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