base.py
111 lines
| 3.7 KiB
| text/x-python
|
PythonLexer
Matthias BUSSONNIER
|
r9302 | """ | ||
Paul Ivanov
|
r12219 | Module that re-groups preprocessor that would be applied to ipynb files | ||
Matthias BUSSONNIER
|
r9528 | before going through the templating machinery. | ||
Matthias BUSSONNIER
|
r9302 | |||
Jonathan Frederic
|
r10674 | It exposes a convenient class to inherit from to access configurability. | ||
Matthias BUSSONNIER
|
r9302 | """ | ||
Jonathan Frederic
|
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
|
r9302 | |||
Jonathan Frederic
|
r11420 | from ..utils.base import NbConvertBase | ||
Jonathan Frederic
|
r11388 | from IPython.utils.traitlets import Bool | ||
Matthias BUSSONNIER
|
r9307 | |||
Jonathan Frederic
|
r10674 | #----------------------------------------------------------------------------- | ||
# Classes and Functions | ||||
#----------------------------------------------------------------------------- | ||||
Paul Ivanov
|
r12219 | class Preprocessor(NbConvertBase): | ||
""" A configurable preprocessor | ||||
Matthias BUSSONNIER
|
r9528 | |||
Inherit from this class if you wish to have configurability for your | ||||
Paul Ivanov
|
r12219 | preprocessor. | ||
Matthias BUSSONNIER
|
r9528 | |||
Paul Ivanov
|
r12220 | Any configurable traitlets this class exposed will be configurable in | ||
profiles using c.SubClassName.atribute=value | ||||
Matthias BUSSONNIER
|
r9528 | |||
Paul Ivanov
|
r12220 | you can overwrite :meth:`preprocess_cell` to apply a transformation | ||
independently on each cell or :meth:`preprocess` if you prefer your own | ||||
logic. See corresponding docstring for informations. | ||||
Jonathan Frederic
|
r11388 | |||
Disabled by default and can be enabled via the config by | ||||
Paul Ivanov
|
r12219 | 'c.YourPreprocessorName.enabled = True' | ||
Matthias BUSSONNIER
|
r9528 | """ | ||
Jonathan Frederic
|
r10772 | |||
Jonathan Frederic
|
r11382 | enabled = Bool(False, config=True) | ||
def __init__(self, **kw): | ||||
Jonathan Frederic
|
r10674 | """ | ||
Public constructor | ||||
Parameters | ||||
---------- | ||||
config : Config | ||||
Configuration file structure | ||||
**kw : misc | ||||
Additional arguments | ||||
""" | ||||
Paul Ivanov
|
r12219 | super(Preprocessor, self).__init__(**kw) | ||
Matthias BUSSONNIER
|
r9307 | |||
Jonathan Frederic
|
r10772 | |||
Jonathan Frederic
|
r10674 | def __call__(self, nb, resources): | ||
Jonathan Frederic
|
r11388 | if self.enabled: | ||
Paul Ivanov
|
r12219 | return self.preprocess(nb,resources) | ||
Jonathan Frederic
|
r11388 | else: | ||
return nb, resources | ||||
Matthias BUSSONNIER
|
r10834 | |||
Paul Ivanov
|
r12219 | def preprocess(self, nb, resources): | ||
Jonathan Frederic
|
r10674 | """ | ||
Paul Ivanov
|
r12220 | Preprocessing to apply on each notebook. | ||
Jonathan Frederic
|
r10674 | |||
You should return modified nb, resources. | ||||
Paul Ivanov
|
r12220 | If you wish to apply your preprocessing to each cell, you might want | ||
to overwrite preprocess_cell method instead. | ||||
Jonathan Frederic
|
r10674 | |||
Parameters | ||||
---------- | ||||
nb : NotebookNode | ||||
Notebook being converted | ||||
resources : dictionary | ||||
Additional resources used in the conversion process. Allows | ||||
Paul Ivanov
|
r12219 | preprocessors to pass variables into the Jinja engine. | ||
Matthias BUSSONNIER
|
r9528 | """ | ||
Paul Ivanov
|
r12219 | self.log.debug("Applying preprocess: %s", self.__class__.__name__) | ||
Matthias BUSSONNIER
|
r9307 | try : | ||
MinRK
|
r11842 | for worksheet in nb.worksheets: | ||
Matthias BUSSONNIER
|
r9307 | for index, cell in enumerate(worksheet.cells): | ||
Paul Ivanov
|
r12219 | worksheet.cells[index], resources = self.preprocess_cell(cell, resources, index) | ||
Jonathan Frederic
|
r10674 | return nb, resources | ||
Matthias BUSSONNIER
|
r9491 | except NotImplementedError: | ||
Matthias BUSSONNIER
|
r9307 | raise NotImplementedError('should be implemented by subclass') | ||
Matthias BUSSONNIER
|
r9528 | |||
Paul Ivanov
|
r12219 | def preprocess_cell(self, cell, resources, index): | ||
Jonathan Frederic
|
r10674 | """ | ||
Paul Ivanov
|
r12220 | Overwrite if you want to apply some preprocessing to each cell. You | ||
Jonathan Frederic
|
r10674 | 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 | ||||
Paul Ivanov
|
r12219 | preprocessors to pass variables into the Jinja engine. | ||
Jonathan Frederic
|
r10674 | index : int | ||
Index of the cell being processed | ||||
Matthias BUSSONNIER
|
r9307 | """ | ||
Jonathan Frederic
|
r10485 | |||
Matthias BUSSONNIER
|
r9307 | raise NotImplementedError('should be implemented by subclass') | ||
Jonathan Frederic
|
r10674 | return cell, resources | ||
Matthias BUSSONNIER
|
r10834 | |||