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