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