##// END OF EJS Templates
some improvement
Matthias BUSSONNIER -
Show More
@@ -92,7 +92,7 b' def header_body():'
92 header.append(pygments_css)
92 header.append(pygments_css)
93 return header
93 return header
94
94
95
95 # todo, make the key part configurable.
96 def _new_figure(data, fmt, count):
96 def _new_figure(data, fmt, count):
97 """Create a new figure file in the given format.
97 """Create a new figure file in the given format.
98
98
@@ -158,48 +158,55 b" texenv.filters['markdown'] = markdown"
158 texenv.filters['highlight'] = highlight
158 texenv.filters['highlight'] = highlight
159 texenv.filters['ansi2html'] = ansi2html
159 texenv.filters['ansi2html'] = ansi2html
160 texenv.filters['markdown2latex'] = markdown2latex
160 texenv.filters['markdown2latex'] = markdown2latex
161 markdown2latex
162
163
164 def haspyout_transformer(nb,_):
165 for worksheet in nb.worksheets:
166 for cell in worksheet.cells:
167 cell.type = cell.cell_type
168 cell.haspyout = False
169 for out in cell.get('outputs', []):
170 if out.output_type == 'pyout':
171 cell.haspyout = True
172 break
173 return nb,_
174
175
176 def outline_figure_transformer(nb,other):
177 count=0
178 for worksheet in nb.worksheets:
179 for cell in worksheet.cells:
180 cell.type = cell.cell_type
181 for i,out in enumerate(cell.get('outputs', [])):
182 print('loop outputs',out.output_type)
183 for type in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg']:
184 if out.hasattr(type):
185 figname,data = _new_figure(out[type], type,count)
186 cell.outputs[i][type] = figname
187 out[type] = figname
188 print('set',type, 'to' ,figname)
189 other[figname] = data
190 count = count+1
191 return nb,other
192
161
162 def cell_preprocessor(function):
163 """ wrap a function to be executed on all cells of a notebook
164
165 wrapped function parameters :
166 cell : the cell
167 other : external resources
168 index : index of the cell
169 """
170 def wrappedfunc(nb,other):
171 for worksheet in nb.worksheets :
172 for index, cell in enumerate(worksheet.cells):
173 worksheet.cells[index],other= function(cell,other,index)
174 return nb,other
175 return wrappedfunc
176
177
178
179 @cell_preprocessor
180 def haspyout_transformer(cell, other, count):
181 """
182 Add a haspyout flag to cell that have it
183
184 Easier for templating, where you can't know in advance
185 wether to write the out prompt
193
186
194 def print_transformer(nb,other):
187 """
195 count=0
188 cell.type = cell.cell_type
196 for worksheet in nb.worksheets:
189 cell.haspyout = False
197 for cell in worksheet.cells:
190 for out in cell.get('outputs', []):
198 cell.type = cell.cell_type
191 if out.output_type == 'pyout':
199 for i,out in enumerate(cell.get('outputs', [])):
192 cell.haspyout = True
200 print(cell.outputs)
193 break
194 return cell,other
195
196
197 @cell_preprocessor
198 def outline_figure_transformer(cell,other,count):
199 for i,out in enumerate(cell.get('outputs', [])):
200 for type in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg']:
201 if out.hasattr(type):
202 figname,data = _new_figure(out[type], type,count)
203 cell.outputs[i][type] = figname
204 out['key_'+type] = figname
205 other[figname] = data
206 count = count+1
201 return nb,other
207 return nb,other
202
208
209
203 class ConverterTemplate(Configurable):
210 class ConverterTemplate(Configurable):
204 """ A Jinja2 base converter templates"""
211 """ A Jinja2 base converter templates"""
205
212
@@ -210,6 +217,14 b' class ConverterTemplate(Configurable):'
210 to user input before code is run.
217 to user input before code is run.
211 """
218 """
212 )
219 )
220
221 extract_figures = Bool(False,
222 config=True,
223 help= """
224 wether to remove figure data from ipynb and store them in auxiliary
225 dictionnary
226 """
227 )
213 #-------------------------------------------------------------------------
228 #-------------------------------------------------------------------------
214 # Instance-level attributes that are set in the constructor for this
229 # Instance-level attributes that are set in the constructor for this
215 # class.
230 # class.
@@ -218,8 +233,6 b' class ConverterTemplate(Configurable):'
218
233
219
234
220 infile_dir = Unicode()
235 infile_dir = Unicode()
221 def display_data_priority_changed(self, name, old, new):
222 print('== changed', name,old,new)
223
236
224 def filter_data_type(self,output):
237 def filter_data_type(self,output):
225 for fmt in self.display_data_priority:
238 for fmt in self.display_data_priority:
@@ -237,12 +250,11 b' class ConverterTemplate(Configurable):'
237
250
238 """
251 """
239 self.env = texenv if tex_environement else env
252 self.env = texenv if tex_environement else env
240 self.ext = '.tplx' if tex_environement else '.tpl'
253 self.ext = '.tplx' if tex_environement else '.tpl'
241 self.nb = None
254 self.nb = None
242 self.preprocessors = preprocessors
255 self.preprocessors = preprocessors
243 self.preprocessors.append(haspyout_transformer)
256 self.preprocessors.append(haspyout_transformer)
244 self.preprocessors.append(outline_figure_transformer)
257 self.preprocessors.append(outline_figure_transformer)
245 self.preprocessors.append(print_transformer)
246 super(ConverterTemplate, self).__init__(config=config, **kw)
258 super(ConverterTemplate, self).__init__(config=config, **kw)
247 self.env.filters['filter_data_type'] = self.filter_data_type
259 self.env.filters['filter_data_type'] = self.filter_data_type
248 self.template = self.env.get_template(tplfile+self.ext)
260 self.template = self.env.get_template(tplfile+self.ext)
@@ -251,8 +263,8 b' class ConverterTemplate(Configurable):'
251
263
252 def process(self):
264 def process(self):
253 """
265 """
254 preprocess the notebook json for easier use with the templates.
266 preprocess the notebook json for easier use with the templates.
255 will call all the `preprocessor`s in order before returning it.
267 will call all the `preprocessor`s in order before returning it.
256 """
268 """
257 nb = self.nb
269 nb = self.nb
258
270
@@ -36,7 +36,6 b' it introduces a new line'
36 \end{codeoutput}((* endblock *))
36 \end{codeoutput}((* endblock *))
37
37
38 ((*- block data_png -*))
38 ((*- block data_png -*))
39 ++(((output.png)))++
40 \begin{center}
39 \begin{center}
41 \includegraphics[width=0.7\textwidth]{(((output.png)))}
40 \includegraphics[width=0.7\textwidth]{(((output.png)))}
42 \par
41 \par
General Comments 0
You need to be logged in to leave comments. Login now