##// END OF EJS Templates
Moved all converter code into nbconvert subdirectory
Moved all converter code into nbconvert subdirectory

File last commit:

r10482:87812de9
r10482:87812de9
Show More
base.py
120 lines | 3.7 KiB | text/x-python | PythonLexer
Matthias BUSSONNIER
move transformer in separate file
r9302 """
Matthias BUSSONNIER
docs
r9528 Module that regroups transformer that woudl be applied to ipynb files
before going through the templating machinery.
Matthias BUSSONNIER
move transformer in separate file
r9302
Matthias BUSSONNIER
docs
r9528 It exposes convenient classes to inherit from to access configurability
as well as decorator to simplify tasks.
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
Matthias BUSSONNIER
fix soem figure extraction logic
r9312 from IPython.utils.traitlets import Unicode, Bool, Dict, List
Matthias BUSSONNIER
create configurable preprocessors
r9307
Matthias BUSSONNIER
fix some relative path issues
r9819 from .config import GlobalConfigurable
Matthias BUSSONNIER
share some global config state
r9529
class ConfigurableTransformers(GlobalConfigurable):
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.
"""
Matthias BUSSONNIER
create configurable preprocessors
r9307
def __init__(self, config=None, **kw):
super(ConfigurableTransformers, self).__init__(config=config, **kw)
def __call__(self, nb, other):
Matthias BUSSONNIER
docs
r9528 """transformation to apply on each notebook.
received a handle to the current notebook as well as a dict of resources
which structure depends on the transformer.
You should return modified nb, other.
If you wish to apply on each cell, you might want to overwrite cell_transform method.
"""
Matthias BUSSONNIER
create configurable preprocessors
r9307 try :
for worksheet in nb.worksheets :
for index, cell in enumerate(worksheet.cells):
MinRK
band-aid to avoid figure collision...
r10132 worksheet.cells[index], other = self.cell_transform(cell, other, 100*index)
Matthias BUSSONNIER
create configurable preprocessors
r9307 return nb, other
Matthias BUSSONNIER
pylint
r9491 except NotImplementedError:
Matthias BUSSONNIER
create configurable preprocessors
r9307 raise NotImplementedError('should be implemented by subclass')
def cell_transform(self, cell, other, index):
"""
Matthias BUSSONNIER
docs
r9528 Overwrite if you want to apply a transformation on each cell,
receive the current cell, the resource dict and the index of current cell as parameter.
You should return modified cell and resource dict.
Matthias BUSSONNIER
create configurable preprocessors
r9307 """
raise NotImplementedError('should be implemented by subclass')
Matthias BUSSONNIER
docs
r9528 return cell, other
Matthias BUSSONNIER
create configurable preprocessors
r9307
Matthias BUSSONNIER
move transformer in separate file
r9302 def cell_preprocessor(function):
""" wrap a function to be executed on all cells of a notebook
wrapped function parameters :
Matthias BUSSONNIER
docs
r9528 cell : the cell
other : external resources
index : index of the cell
Matthias BUSSONNIER
move transformer in separate file
r9302 """
Matthias BUSSONNIER
mofe filter in separate file
r9303 def wrappedfunc(nb, other):
Matthias BUSSONNIER
move transformer in separate file
r9302 for worksheet in nb.worksheets :
for index, cell in enumerate(worksheet.cells):
Matthias BUSSONNIER
mofe filter in separate file
r9303 worksheet.cells[index], other = function(cell, other, index)
return nb, other
Matthias BUSSONNIER
move transformer in separate file
r9302 return wrappedfunc
@cell_preprocessor
def haspyout_transformer(cell, other, count):
"""
Add a haspyout flag to cell that have it
Matthias BUSSONNIER
create configurable preprocessors
r9307
Matthias BUSSONNIER
move transformer in separate file
r9302 Easier for templating, where you can't know in advance
wether to write the out prompt
"""
cell.type = cell.cell_type
cell.haspyout = False
for out in cell.get('outputs', []):
if out.output_type == 'pyout':
cell.haspyout = True
break
Matthias BUSSONNIER
mofe filter in separate file
r9303 return cell, other
Matthias BUSSONNIER
move transformer in separate file
r9302
Matthias BUSSONNIER
add a coalesce stream transformer
r9492 @cell_preprocessor
def coalesce_streams(cell, other, count):
"""merge consecutive sequences of stream output into single stream
to prevent extra newlines inserted at flush calls
TODO: handle \r deletion
"""
Matthias BUSSONNIER
fix test for coalesce stream, and fig key config
r9494 outputs = cell.get('outputs', [])
Matthias BUSSONNIER
add a coalesce stream transformer
r9492 if not outputs:
return cell, other
new_outputs = []
last = outputs[0]
new_outputs = [last]
for output in outputs[1:]:
if (output.output_type == 'stream' and
last.output_type == 'stream' and
last.stream == output.stream
):
last.text += output.text
else:
new_outputs.append(output)
cell.outputs = new_outputs
return cell, other