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