##// END OF EJS Templates
move transformer in separate file
Matthias BUSSONNIER -
Show More
@@ -0,0 +1,50 b''
1 """
2
3 """
4
5 def cell_preprocessor(function):
6 """ wrap a function to be executed on all cells of a notebook
7
8 wrapped function parameters :
9 cell : the cell
10 other : external resources
11 index : index of the cell
12 """
13 def wrappedfunc(nb,other):
14 for worksheet in nb.worksheets :
15 for index, cell in enumerate(worksheet.cells):
16 worksheet.cells[index],other= function(cell,other,index)
17 return nb,other
18 return wrappedfunc
19
20
21 @cell_preprocessor
22 def haspyout_transformer(cell, other, count):
23 """
24 Add a haspyout flag to cell that have it
25
26 Easier for templating, where you can't know in advance
27 wether to write the out prompt
28
29 """
30 cell.type = cell.cell_type
31 cell.haspyout = False
32 for out in cell.get('outputs', []):
33 if out.output_type == 'pyout':
34 cell.haspyout = True
35 break
36 return cell,other
37
38
39 @cell_preprocessor
40 def extract_figure_transformer(cell,other,count):
41 for i,out in enumerate(cell.get('outputs', [])):
42 for type in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg']:
43 if out.hasattr(type):
44 figname,data = _new_figure(out[type], type, count)
45 cell.outputs[i][type] = figname
46 out['key_'+type] = figname
47 other[figname] = data
48 count = count+1
49 return cell,other
50
@@ -16,6 +16,9 b' a conversion of IPython notebooks to some other format should inherit.'
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 from __future__ import print_function, absolute_import
18 from __future__ import print_function, absolute_import
19 from .transformers import extract_figure_transformer
20 import converters.transformers as trans
21
19
22
20 # Stdlib imports
23 # Stdlib imports
21 import io
24 import io
@@ -158,36 +161,6 b' def cell_preprocessor(function):'
158
161
159
162
160
163
161 @cell_preprocessor
162 def haspyout_transformer(cell, other, count):
163 """
164 Add a haspyout flag to cell that have it
165
166 Easier for templating, where you can't know in advance
167 wether to write the out prompt
168
169 """
170 cell.type = cell.cell_type
171 cell.haspyout = False
172 for out in cell.get('outputs', []):
173 if out.output_type == 'pyout':
174 cell.haspyout = True
175 break
176 return cell,other
177
178
179 @cell_preprocessor
180 def extract_figure_transformer(cell,other,count):
181 for i,out in enumerate(cell.get('outputs', [])):
182 for type in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg']:
183 if out.hasattr(type):
184 figname,data = _new_figure(out[type], type,count)
185 cell.outputs[i][type] = figname
186 out['key_'+type] = figname
187 other[figname] = data
188 count = count+1
189 return cell,other
190
191
164
192 class ConverterTemplate(Configurable):
165 class ConverterTemplate(Configurable):
193 """ A Jinja2 base converter templates"""
166 """ A Jinja2 base converter templates"""
@@ -200,7 +173,7 b' class ConverterTemplate(Configurable):'
200 """
173 """
201 )
174 )
202
175
203 pre_transformer_order = List([],
176 pre_transformer_order = List(['haspyout_transformer'],
204 config=True,
177 config=True,
205 help= """ An ordered list of pretransformer to apply to the ipynb file befor running through templates
178 help= """ An ordered list of pretransformer to apply to the ipynb file befor running through templates
206 """
179 """
@@ -250,7 +223,8 b' class ConverterTemplate(Configurable):'
250 self.ext = '.tplx' if self.tex_environement else '.tpl'
223 self.ext = '.tplx' if self.tex_environement else '.tpl'
251 self.nb = None
224 self.nb = None
252 self.preprocessors = preprocessors
225 self.preprocessors = preprocessors
253 self.preprocessors.append(haspyout_transformer)
226 for name in self.pre_transformer_order:
227 self.preprocessors.append(getattr(trans,'haspyout_transformer'))
254 if self.extract_figures:
228 if self.extract_figures:
255 self.preprocessors.append(extract_figure_transformer)
229 self.preprocessors.append(extract_figure_transformer)
256
230
@@ -267,7 +241,6 b' class ConverterTemplate(Configurable):'
267 self.template = self.env.get_template(self.template_file+self.ext)
241 self.template = self.env.get_template(self.template_file+self.ext)
268
242
269
243
270
271 def process(self):
244 def process(self):
272 """
245 """
273 preprocess the notebook json for easier use with the templates.
246 preprocess the notebook json for easier use with the templates.
General Comments 0
You need to be logged in to leave comments. Login now