activatable.py
53 lines
| 1.9 KiB
| text/x-python
|
PythonLexer
Jonathan Frederic
|
r10674 | """ | ||
Contains base transformer with an enable/disable flag. | ||||
""" | ||||
#----------------------------------------------------------------------------- | ||||
# 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 | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r10624 | from .base import ConfigurableTransformer | ||
from IPython.utils.traitlets import (Bool) | ||||
Jonathan Frederic
|
r10485 | |||
Jonathan Frederic
|
r10674 | #----------------------------------------------------------------------------- | ||
# Classes and Functions | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r10437 | |||
Jonathan Frederic
|
r10674 | class ActivatableTransformer(ConfigurableTransformer): | ||
"""ConfigurableTransformer that has an enabled flag | ||||
Jonathan Frederic
|
r10437 | |||
Jonathan Frederic
|
r10674 | Inherit from this if you just want to have a transformer which is | ||
enabled by default and can be disabled via the config by | ||||
'c.YourTransformerName.enabled = True' | ||||
Jonathan Frederic
|
r10437 | """ | ||
Jonathan Frederic
|
r10630 | enabled = Bool(True, config=True) | ||
Jonathan Frederic
|
r10437 | |||
Jonathan Frederic
|
r10674 | def __call__(self, nb, resources): | ||
""" | ||||
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. | ||||
""" | ||||
Jonathan Frederic
|
r10437 | if not self.enabled : | ||
Jonathan Frederic
|
r10674 | return nb, resources | ||
Jonathan Frederic
|
r10437 | else : | ||
Jonathan Frederic
|
r10674 | return super(ActivatableTransformer, self).__call__(nb, resources) | ||