##// END OF EJS Templates
move GLobalConfigurable back into nbconvert2
move GLobalConfigurable back into nbconvert2

File last commit:

r10834:350169ee
r10858:cfe2c8b1
Show More
base.py
101 lines | 3.5 KiB | text/x-python | PythonLexer
Matthias BUSSONNIER
move transformer in separate file
r9302 """
Jonathan Frederic
Cleanup and refactor, transformers
r10674 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
Matthias BUSSONNIER
fix some relative path issues
r9819 from __future__ import print_function, absolute_import
Matthias BUSSONNIER
move html header out
r9490
Matthias BUSSONNIER
create configurable preprocessors
r9307 from IPython.config.configurable import Configurable
Jonathan Frederic
Cleanup and refactor, transformers
r10674 #-----------------------------------------------------------------------------
# Classes and Functions
#-----------------------------------------------------------------------------
Jonathan Frederic
Fixed all broken references, refactored some stuff here and there,...
r10624 class ConfigurableTransformer(Configurable):
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
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.
"""
Jonathan Frederic
Generator used to create figure indicies
r10772
Matthias BUSSONNIER
create configurable preprocessors
r9307 def __init__(self, config=None, **kw):
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """
Public constructor
Parameters
----------
config : Config
Configuration file structure
**kw : misc
Additional arguments
"""
Jonathan Frederic
Fixed all broken references, refactored some stuff here and there,...
r10624 super(ConfigurableTransformer, self).__init__(config=config, **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):
Matthias BUSSONNIER
do not ask to inherit __call__ but call
r10834 return self.call(nb,resources)
def call(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
overwrite cell_transform method instead.
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 """
Matthias BUSSONNIER
create configurable preprocessors
r9307 try :
for worksheet in nb.worksheets :
for index, cell in enumerate(worksheet.cells):
Jonathan Frederic
Generator used to create figure indicies
r10772 worksheet.cells[index], resources = self.cell_transform(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
Cleanup and refactor, transformers
r10674 def cell_transform(self, cell, resources, index):
"""
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