##// END OF EJS Templates
Comment & Refactor, utils and nbconvert main.
Comment & Refactor, utils and nbconvert main.

File last commit:

r10624:bfa8f97c
r10673:70e4dc3c
Show More
base.py
59 lines | 2.0 KiB | text/x-python | PythonLexer
"""
Module that regroups transformer that woudl be applied to ipynb files
before going through the templating machinery.
It exposes convenient classes to inherit from to access configurability
as well as decorator to simplify tasks.
"""
from __future__ import print_function, absolute_import
from IPython.config.configurable import Configurable
class ConfigurableTransformer(Configurable):
""" A configurable transformer
Inherit from this class if you wish to have configurability for your
transformer.
Any configurable traitlets this class exposed will be configurable in profiles
using c.SubClassName.atribute=value
you can overwrite cell_transform to apply a transformation independently on each cell
or __call__ if you prefer your own logic. See orresponding docstring for informations.
"""
def __init__(self, config=None, **kw):
super(ConfigurableTransformer, self).__init__(config=config, **kw)
def __call__(self, nb, other):
"""transformation to apply on each notebook.
received a handle to the current notebook as well as a dict of resources
which structure depends on the transformer.
You should return modified nb, other.
If you wish to apply on each cell, you might want to overwrite cell_transform method.
"""
try :
for worksheet in nb.worksheets :
for index, cell in enumerate(worksheet.cells):
worksheet.cells[index], other = self.cell_transform(cell, other, 100*index)
return nb, other
except NotImplementedError:
raise NotImplementedError('should be implemented by subclass')
def cell_transform(self, cell, other, index):
"""
Overwrite if you want to apply a transformation on each cell,
receive the current cell, the resource dict and the index of current cell as parameter.
You should return modified cell and resource dict.
"""
raise NotImplementedError('should be implemented by subclass')
return cell, other