Show More
@@ -92,7 +92,7 b' def header_body():' | |||
|
92 | 92 | header.append(pygments_css) |
|
93 | 93 | return header |
|
94 | 94 | |
|
95 | ||
|
95 | # todo, make the key part configurable. | |
|
96 | 96 | def _new_figure(data, fmt, count): |
|
97 | 97 | """Create a new figure file in the given format. |
|
98 | 98 | |
@@ -158,48 +158,55 b" texenv.filters['markdown'] = markdown" | |||
|
158 | 158 | texenv.filters['highlight'] = highlight |
|
159 | 159 | texenv.filters['ansi2html'] = ansi2html |
|
160 | 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): | |
|
195 | count=0 | |
|
196 | for worksheet in nb.worksheets: | |
|
197 | for cell in worksheet.cells: | |
|
198 | cell.type = cell.cell_type | |
|
199 | for i,out in enumerate(cell.get('outputs', [])): | |
|
200 | print(cell.outputs) | |
|
187 | """ | |
|
188 | cell.type = cell.cell_type | |
|
189 | cell.haspyout = False | |
|
190 | for out in cell.get('outputs', []): | |
|
191 | if out.output_type == 'pyout': | |
|
192 | cell.haspyout = True | |
|
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 | 207 | return nb,other |
|
202 | 208 | |
|
209 | ||
|
203 | 210 | class ConverterTemplate(Configurable): |
|
204 | 211 | """ A Jinja2 base converter templates""" |
|
205 | 212 | |
@@ -210,6 +217,14 b' class ConverterTemplate(Configurable):' | |||
|
210 | 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 | 229 | # Instance-level attributes that are set in the constructor for this |
|
215 | 230 | # class. |
@@ -218,8 +233,6 b' class ConverterTemplate(Configurable):' | |||
|
218 | 233 | |
|
219 | 234 | |
|
220 | 235 | infile_dir = Unicode() |
|
221 | def display_data_priority_changed(self, name, old, new): | |
|
222 | print('== changed', name,old,new) | |
|
223 | 236 | |
|
224 | 237 | def filter_data_type(self,output): |
|
225 | 238 | for fmt in self.display_data_priority: |
@@ -237,12 +250,11 b' class ConverterTemplate(Configurable):' | |||
|
237 | 250 | |
|
238 | 251 | """ |
|
239 | 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 | 254 | self.nb = None |
|
242 | 255 | self.preprocessors = preprocessors |
|
243 | 256 | self.preprocessors.append(haspyout_transformer) |
|
244 | 257 | self.preprocessors.append(outline_figure_transformer) |
|
245 | self.preprocessors.append(print_transformer) | |
|
246 | 258 | super(ConverterTemplate, self).__init__(config=config, **kw) |
|
247 | 259 | self.env.filters['filter_data_type'] = self.filter_data_type |
|
248 | 260 | self.template = self.env.get_template(tplfile+self.ext) |
@@ -251,8 +263,8 b' class ConverterTemplate(Configurable):' | |||
|
251 | 263 | |
|
252 | 264 | def process(self): |
|
253 | 265 | """ |
|
254 |
preprocess the notebook json for easier use with the templates. |
|
|
255 |
will call all the `preprocessor`s in order before returning it. |
|
|
266 | preprocess the notebook json for easier use with the templates. | |
|
267 | will call all the `preprocessor`s in order before returning it. | |
|
256 | 268 | """ |
|
257 | 269 | nb = self.nb |
|
258 | 270 |
General Comments 0
You need to be logged in to leave comments.
Login now