##// END OF EJS Templates
rename call methods to transform and postprocess...
rename call methods to transform and postprocess - The `call` methods for nbconvert transformers has been renamed to `transform`. - The `call` methods of nbconvert post-processsors have been renamed to `postprocess`. pinging @Carreau and @jdrefer, who probably knows who'd be affected by this change... maybe @damianavila, @jakevdp

File last commit:

r12218:19e43d9d
r12218:19e43d9d
Show More
base.py
110 lines | 3.7 KiB | text/x-python | PythonLexer
Matthias BUSSONNIER
move transformer in separate file
r9302 """
MinRK
various spelling and capitalization fixes
r11046 Module that re-groups transformer that would be applied to ipynb files
Matthias BUSSONNIER
docs
r9528 before going through the templating machinery.
Matthias BUSSONNIER
move transformer in separate file
r9302
Jonathan Frederic
Cleanup and refactor, transformers
r10674 It exposes a convenient class to inherit from to access configurability.
Matthias BUSSONNIER
move transformer in separate file
r9302 """
Jonathan Frederic
Cleanup and refactor, transformers
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
#-----------------------------------------------------------------------------
Matthias BUSSONNIER
move transformer in separate file
r9302
Jonathan Frederic
Rename utils.config to utils.base
r11420 from ..utils.base import NbConvertBase
Jonathan Frederic
Removed ActivatableTransformer and moved enabled key into base.
r11388 from IPython.utils.traitlets import Bool
Matthias BUSSONNIER
create configurable preprocessors
r9307
Jonathan Frederic
Cleanup and refactor, transformers
r10674 #-----------------------------------------------------------------------------
# Classes and Functions
#-----------------------------------------------------------------------------
MinRK
s/ConfigurableTransformer/Transformer/
r11452 class Transformer(NbConvertBase):
Matthias BUSSONNIER
docs
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
Matthias Bussonnier
prepend :meth: Docstrings are not markdown.
r12190 you can overwrite :meth:`transform_cell` to apply a transformation independently on each cell
Paul Ivanov
rename call methods to transform and postprocess...
r12218 or :meth:`transform` if you prefer your own logic. See corresponding docstring for informations.
Jonathan Frederic
Removed ActivatableTransformer and moved enabled key into base.
r11388
Disabled by default and can be enabled via the config by
'c.YourTransformerName.enabled = True'
Matthias BUSSONNIER
docs
r9528 """
Jonathan Frederic
Generator used to create figure indicies
r10772
Jonathan Frederic
Prefer new style of config passing, parent=self (replaces config=self.config)
r11382 enabled = Bool(False, config=True)
def __init__(self, **kw):
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """
Public constructor
Parameters
----------
config : Config
Configuration file structure
**kw : misc
Additional arguments
"""
MinRK
s/ConfigurableTransformer/Transformer/
r11452 super(Transformer, self).__init__(**kw)
Matthias BUSSONNIER
create configurable preprocessors
r9307
Jonathan Frederic
Generator used to create figure indicies
r10772
Jonathan Frederic
Cleanup and refactor, transformers
r10674 def __call__(self, nb, resources):
Jonathan Frederic
Removed ActivatableTransformer and moved enabled key into base.
r11388 if self.enabled:
Paul Ivanov
rename call methods to transform and postprocess...
r12218 return self.transform(nb,resources)
Jonathan Frederic
Removed ActivatableTransformer and moved enabled key into base.
r11388 else:
return nb, resources
Matthias BUSSONNIER
do not ask to inherit __call__ but call
r10834
Paul Ivanov
rename call methods to transform and postprocess...
r12218 def transform(self, nb, resources):
Jonathan Frederic
Cleanup and refactor, transformers
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
Jonathan Frederic
Added writers and supporting code.
r11367 overwrite transform_cell method instead.
Jonathan Frederic
Cleanup and refactor, transformers
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.
Matthias BUSSONNIER
docs
r9528 """
MinRK
add basic logging to NbConvert stages
r11842 self.log.debug("Applying transform: %s", self.__class__.__name__)
Matthias BUSSONNIER
create configurable preprocessors
r9307 try :
MinRK
add basic logging to NbConvert stages
r11842 for worksheet in nb.worksheets:
Matthias BUSSONNIER
create configurable preprocessors
r9307 for index, cell in enumerate(worksheet.cells):
Jonathan Frederic
Added writers and supporting code.
r11367 worksheet.cells[index], resources = self.transform_cell(cell, resources, index)
Jonathan Frederic
Cleanup and refactor, transformers
r10674 return nb, resources
Matthias BUSSONNIER
pylint
r9491 except NotImplementedError:
Matthias BUSSONNIER
create configurable preprocessors
r9307 raise NotImplementedError('should be implemented by subclass')
Matthias BUSSONNIER
docs
r9528
Jonathan Frederic
Added writers and supporting code.
r11367 def transform_cell(self, cell, resources, index):
Jonathan Frederic
Cleanup and refactor, transformers
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
Matthias BUSSONNIER
create configurable preprocessors
r9307 """
Jonathan Frederic
Post code-review, extended refactor.
r10485
Matthias BUSSONNIER
create configurable preprocessors
r9307 raise NotImplementedError('should be implemented by subclass')
Jonathan Frederic
Cleanup and refactor, transformers
r10674 return cell, resources
Matthias BUSSONNIER
do not ask to inherit __call__ but call
r10834