base.py
99 lines
| 3.4 KiB
| text/x-python
|
PythonLexer
|
r9302 | """ | ||
|
r11046 | Module that re-groups transformer that would be applied to ipynb files | ||
|
r9528 | before going through the templating machinery. | ||
|
r9302 | |||
|
r10674 | It exposes a convenient class to inherit from to access configurability. | ||
|
r9302 | """ | ||
|
r10674 | #----------------------------------------------------------------------------- | ||
# Copyright (c) 2013, the IPython Development Team. | ||||
# | ||||
# Distributed under the terms of the Modified BSD License. | ||||
# | ||||
# The full license is in the file COPYING.txt, distributed with this software. | ||||
#----------------------------------------------------------------------------- | ||||
#----------------------------------------------------------------------------- | ||||
# Imports | ||||
#----------------------------------------------------------------------------- | ||||
|
r9302 | |||
|
r10859 | from ..utils.config import GlobalConfigurable | ||
|
r9307 | |||
|
r10674 | #----------------------------------------------------------------------------- | ||
# Classes and Functions | ||||
#----------------------------------------------------------------------------- | ||||
|
r10859 | class ConfigurableTransformer(GlobalConfigurable): | ||
|
r9528 | """ 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 | ||||
|
r11367 | you can overwrite transform_cell to apply a transformation independently on each cell | ||
|
r11046 | or __call__ if you prefer your own logic. See corresponding docstring for informations. | ||
|
r9528 | """ | ||
|
r10772 | |||
|
r9307 | def __init__(self, config=None, **kw): | ||
|
r10674 | """ | ||
Public constructor | ||||
Parameters | ||||
---------- | ||||
config : Config | ||||
Configuration file structure | ||||
**kw : misc | ||||
Additional arguments | ||||
""" | ||||
|
r10624 | super(ConfigurableTransformer, self).__init__(config=config, **kw) | ||
|
r9307 | |||
|
r10772 | |||
|
r10674 | def __call__(self, nb, resources): | ||
|
r10834 | return self.call(nb,resources) | ||
def call(self, nb, resources): | ||||
|
r10674 | """ | ||
Transformation to apply on each notebook. | ||||
You should return modified nb, resources. | ||||
If you wish to apply your transform on each cell, you might want to | ||||
|
r11367 | overwrite transform_cell method instead. | ||
|
r10674 | |||
Parameters | ||||
---------- | ||||
nb : NotebookNode | ||||
Notebook being converted | ||||
resources : dictionary | ||||
Additional resources used in the conversion process. Allows | ||||
transformers to pass variables into the Jinja engine. | ||||
|
r9528 | """ | ||
|
r9307 | try : | ||
for worksheet in nb.worksheets : | ||||
for index, cell in enumerate(worksheet.cells): | ||||
|
r11367 | worksheet.cells[index], resources = self.transform_cell(cell, resources, index) | ||
|
r10674 | return nb, resources | ||
|
r9491 | except NotImplementedError: | ||
|
r9307 | raise NotImplementedError('should be implemented by subclass') | ||
|
r9528 | |||
|
r11367 | def transform_cell(self, cell, resources, index): | ||
|
r10674 | """ | ||
Overwrite if you want to apply a transformation on each cell. You | ||||
should return modified cell and resource dictionary. | ||||
Parameters | ||||
---------- | ||||
cell : NotebookNode cell | ||||
Notebook cell being processed | ||||
resources : dictionary | ||||
Additional resources used in the conversion process. Allows | ||||
transformers to pass variables into the Jinja engine. | ||||
index : int | ||||
Index of the cell being processed | ||||
|
r9307 | """ | ||
|
r10485 | |||
|
r9307 | raise NotImplementedError('should be implemented by subclass') | ||
|
r10674 | return cell, resources | ||
|
r10834 | |||