Show More
@@ -1,20 +1,44 b'' | |||
|
1 | 1 | """ |
|
2 | Module that regroups transformer that woudl be applied to ipynb files | |
|
3 | before going through the templating machinery. | |
|
2 | 4 | |
|
5 | It exposes convenient classes to inherit from to access configurability | |
|
6 | as well as decorator to simplify tasks. | |
|
3 | 7 | """ |
|
4 | 8 | |
|
5 | 9 | from __future__ import print_function |
|
6 | 10 | |
|
7 | ||
|
8 | 11 | from IPython.config.configurable import Configurable |
|
9 | 12 | from IPython.utils.traitlets import Unicode, Bool, Dict, List |
|
10 | 13 | |
|
11 | 14 | class ConfigurableTransformers(Configurable): |
|
12 |
""" A configurable transformer |
|
|
15 | """ A configurable transformer | |
|
16 | ||
|
17 | Inherit from this class if you wish to have configurability for your | |
|
18 | transformer. | |
|
19 | ||
|
20 | Any configurable traitlets this class exposed will be configurable in profiles | |
|
21 | using c.SubClassName.atribute=value | |
|
22 | ||
|
23 | you can overwrite cell_transform to apply a transformation independently on each cell | |
|
24 | or __call__ if you prefer your own logic. See orresponding docstring for informations. | |
|
25 | ||
|
26 | ||
|
27 | """ | |
|
13 | 28 | |
|
14 | 29 | def __init__(self, config=None, **kw): |
|
15 | 30 | super(ConfigurableTransformers, self).__init__(config=config, **kw) |
|
16 | 31 | |
|
17 | 32 | def __call__(self, nb, other): |
|
33 | """transformation to apply on each notebook. | |
|
34 | ||
|
35 | received a handle to the current notebook as well as a dict of resources | |
|
36 | which structure depends on the transformer. | |
|
37 | ||
|
38 | You should return modified nb, other. | |
|
39 | ||
|
40 | If you wish to apply on each cell, you might want to overwrite cell_transform method. | |
|
41 | """ | |
|
18 | 42 | try : |
|
19 | 43 | for worksheet in nb.worksheets : |
|
20 | 44 | for index, cell in enumerate(worksheet.cells): |
@@ -25,12 +49,24 b' class ConfigurableTransformers(Configurable):' | |||
|
25 | 49 | |
|
26 | 50 | def cell_transform(self, cell, other, index): |
|
27 | 51 | """ |
|
28 | Overwrite if you want to apply a transformation on each cell | |
|
52 | Overwrite if you want to apply a transformation on each cell, | |
|
53 | ||
|
54 | receive the current cell, the resource dict and the index of current cell as parameter. | |
|
55 | ||
|
56 | You should return modified cell and resource dict. | |
|
29 | 57 | """ |
|
30 | 58 | raise NotImplementedError('should be implemented by subclass') |
|
59 | return cell, other | |
|
31 | 60 | |
|
32 | 61 | |
|
33 | 62 | class ActivatableTransformer(ConfigurableTransformers): |
|
63 | """A simple ConfigurableTransformers that have an enabled flag | |
|
64 | ||
|
65 | Inherit from that if you just want to have a transformer which is | |
|
66 | no-op by default but can be activated in profiles with | |
|
67 | ||
|
68 | c.YourTransformerName.enabled = True | |
|
69 | """ | |
|
34 | 70 | |
|
35 | 71 | enabled = Bool(False, config=True) |
|
36 | 72 | |
@@ -45,9 +81,9 b' def cell_preprocessor(function):' | |||
|
45 | 81 | """ wrap a function to be executed on all cells of a notebook |
|
46 | 82 | |
|
47 | 83 | wrapped function parameters : |
|
48 | cell : the cell | |
|
49 | other : external resources | |
|
50 | index : index of the cell | |
|
84 | cell : the cell | |
|
85 | other : external resources | |
|
86 | index : index of the cell | |
|
51 | 87 | """ |
|
52 | 88 | def wrappedfunc(nb, other): |
|
53 | 89 | for worksheet in nb.worksheets : |
@@ -100,10 +136,9 b' def coalesce_streams(cell, other, count):' | |||
|
100 | 136 | cell.outputs = new_outputs |
|
101 | 137 | return cell, other |
|
102 | 138 | |
|
103 | ||
|
104 | ||
|
105 | 139 | class ExtractFigureTransformer(ActivatableTransformer): |
|
106 | 140 | |
|
141 | ||
|
107 | 142 | extra_ext_map = Dict({}, |
|
108 | 143 | config=True, |
|
109 | 144 | help="""extra map to override extension based on type. |
@@ -127,6 +162,7 b' class ExtractFigureTransformer(ActivatableTransformer):' | |||
|
127 | 162 | config=True, |
|
128 | 163 | ) |
|
129 | 164 | |
|
165 | ||
|
130 | 166 | #to do change this to .format {} syntax |
|
131 | 167 | default_key_tpl = Unicode('_fig_{count:02d}.{ext}', config=True) |
|
132 | 168 |
General Comments 0
You need to be logged in to leave comments.
Login now