##// 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
161
162 def cell_preprocessor(function):
163 """ wrap a function to be executed on all cells of a notebook
163
164
164 def haspyout_transformer(nb,_):
165 wrapped function parameters :
166 cell : the cell
167 other : external resources
168 index : index of the cell
169 """
170 def wrappedfunc(nb,other):
165 for worksheet in nb.worksheets:
171 for worksheet in nb.worksheets :
166 for cell in worksheet.cells:
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
186
187 """
167 cell.type = cell.cell_type
188 cell.type = cell.cell_type
168 cell.haspyout = False
189 cell.haspyout = False
169 for out in cell.get('outputs', []):
190 for out in cell.get('outputs', []):
170 if out.output_type == 'pyout':
191 if out.output_type == 'pyout':
171 cell.haspyout = True
192 cell.haspyout = True
172 break
193 break
173 return nb,_
194 return cell,other
174
195
175
196
176 def outline_figure_transformer(nb,other):
197 @cell_preprocessor
177 count=0
198 def outline_figure_transformer(cell,other,count):
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', [])):
199 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']:
200 for type in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg']:
184 if out.hasattr(type):
201 if out.hasattr(type):
185 figname,data = _new_figure(out[type], type,count)
202 figname,data = _new_figure(out[type], type,count)
186 cell.outputs[i][type] = figname
203 cell.outputs[i][type] = figname
187 out[type] = figname
204 out['key_'+type] = figname
188 print('set',type, 'to' ,figname)
189 other[figname] = data
205 other[figname] = data
190 count = count+1
206 count = count+1
191 return nb,other
207 return nb,other
192
208
193
209
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)
201 return nb,other
202
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:
@@ -242,7 +255,6 b' class ConverterTemplate(Configurable):'
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)
@@ -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