##// END OF EJS Templates
move transformer in separate file
move transformer in separate file

File last commit:

r9621:5c9515f4
r9621:5c9515f4
Show More
transformers.py
50 lines | 1.4 KiB | text/x-python | PythonLexer
"""
"""
def cell_preprocessor(function):
""" wrap a function to be executed on all cells of a notebook
wrapped function parameters :
cell : the cell
other : external resources
index : index of the cell
"""
def wrappedfunc(nb,other):
for worksheet in nb.worksheets :
for index, cell in enumerate(worksheet.cells):
worksheet.cells[index],other= function(cell,other,index)
return nb,other
return wrappedfunc
@cell_preprocessor
def haspyout_transformer(cell, other, count):
"""
Add a haspyout flag to cell that have it
Easier for templating, where you can't know in advance
wether to write the out prompt
"""
cell.type = cell.cell_type
cell.haspyout = False
for out in cell.get('outputs', []):
if out.output_type == 'pyout':
cell.haspyout = True
break
return cell,other
@cell_preprocessor
def extract_figure_transformer(cell,other,count):
for i,out in enumerate(cell.get('outputs', [])):
for type in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg']:
if out.hasattr(type):
figname,data = _new_figure(out[type], type, count)
cell.outputs[i][type] = figname
out['key_'+type] = figname
other[figname] = data
count = count+1
return cell,other