Show More
@@ -1,110 +1,110 b'' | |||
|
1 | 1 | """ |
|
2 | 2 | Module that re-groups transformer that would be applied to ipynb files |
|
3 | 3 | before going through the templating machinery. |
|
4 | 4 | |
|
5 | 5 | It exposes a convenient class to inherit from to access configurability. |
|
6 | 6 | """ |
|
7 | 7 | #----------------------------------------------------------------------------- |
|
8 | 8 | # Copyright (c) 2013, the IPython Development Team. |
|
9 | 9 | # |
|
10 | 10 | # Distributed under the terms of the Modified BSD License. |
|
11 | 11 | # |
|
12 | 12 | # The full license is in the file COPYING.txt, distributed with this software. |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | # Imports |
|
17 | 17 | #----------------------------------------------------------------------------- |
|
18 | 18 | |
|
19 | 19 | from ..utils.base import NbConvertBase |
|
20 | 20 | from IPython.utils.traitlets import Bool |
|
21 | 21 | |
|
22 | 22 | #----------------------------------------------------------------------------- |
|
23 | 23 | # Classes and Functions |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | |
|
26 | 26 | class Transformer(NbConvertBase): |
|
27 | 27 | """ A configurable transformer |
|
28 | 28 | |
|
29 | 29 | Inherit from this class if you wish to have configurability for your |
|
30 | 30 | transformer. |
|
31 | 31 | |
|
32 | 32 | Any configurable traitlets this class exposed will be configurable in profiles |
|
33 | 33 | using c.SubClassName.atribute=value |
|
34 | 34 | |
|
35 | you can overwrite `transform_cell` to apply a transformation independently on each cell | |
|
36 | or `call` if you prefer your own logic. See corresponding docstring for informations. | |
|
35 | you can overwrite :meth:`transform_cell` to apply a transformation independently on each cell | |
|
36 | or :meth:`call` if you prefer your own logic. See corresponding docstring for informations. | |
|
37 | 37 | |
|
38 | 38 | Disabled by default and can be enabled via the config by |
|
39 | 39 | 'c.YourTransformerName.enabled = True' |
|
40 | 40 | """ |
|
41 | 41 | |
|
42 | 42 | enabled = Bool(False, config=True) |
|
43 | 43 | |
|
44 | 44 | def __init__(self, **kw): |
|
45 | 45 | """ |
|
46 | 46 | Public constructor |
|
47 | 47 | |
|
48 | 48 | Parameters |
|
49 | 49 | ---------- |
|
50 | 50 | config : Config |
|
51 | 51 | Configuration file structure |
|
52 | 52 | **kw : misc |
|
53 | 53 | Additional arguments |
|
54 | 54 | """ |
|
55 | 55 | |
|
56 | 56 | super(Transformer, self).__init__(**kw) |
|
57 | 57 | |
|
58 | 58 | |
|
59 | 59 | def __call__(self, nb, resources): |
|
60 | 60 | if self.enabled: |
|
61 | 61 | return self.call(nb,resources) |
|
62 | 62 | else: |
|
63 | 63 | return nb, resources |
|
64 | 64 | |
|
65 | 65 | |
|
66 | 66 | def call(self, nb, resources): |
|
67 | 67 | """ |
|
68 | 68 | Transformation to apply on each notebook. |
|
69 | 69 | |
|
70 | 70 | You should return modified nb, resources. |
|
71 | 71 | If you wish to apply your transform on each cell, you might want to |
|
72 | 72 | overwrite transform_cell method instead. |
|
73 | 73 | |
|
74 | 74 | Parameters |
|
75 | 75 | ---------- |
|
76 | 76 | nb : NotebookNode |
|
77 | 77 | Notebook being converted |
|
78 | 78 | resources : dictionary |
|
79 | 79 | Additional resources used in the conversion process. Allows |
|
80 | 80 | transformers to pass variables into the Jinja engine. |
|
81 | 81 | """ |
|
82 | 82 | self.log.debug("Applying transform: %s", self.__class__.__name__) |
|
83 | 83 | try : |
|
84 | 84 | for worksheet in nb.worksheets: |
|
85 | 85 | for index, cell in enumerate(worksheet.cells): |
|
86 | 86 | worksheet.cells[index], resources = self.transform_cell(cell, resources, index) |
|
87 | 87 | return nb, resources |
|
88 | 88 | except NotImplementedError: |
|
89 | 89 | raise NotImplementedError('should be implemented by subclass') |
|
90 | 90 | |
|
91 | 91 | |
|
92 | 92 | def transform_cell(self, cell, resources, index): |
|
93 | 93 | """ |
|
94 | 94 | Overwrite if you want to apply a transformation on each cell. You |
|
95 | 95 | should return modified cell and resource dictionary. |
|
96 | 96 | |
|
97 | 97 | Parameters |
|
98 | 98 | ---------- |
|
99 | 99 | cell : NotebookNode cell |
|
100 | 100 | Notebook cell being processed |
|
101 | 101 | resources : dictionary |
|
102 | 102 | Additional resources used in the conversion process. Allows |
|
103 | 103 | transformers to pass variables into the Jinja engine. |
|
104 | 104 | index : int |
|
105 | 105 | Index of the cell being processed |
|
106 | 106 | """ |
|
107 | 107 | |
|
108 | 108 | raise NotImplementedError('should be implemented by subclass') |
|
109 | 109 | return cell, resources |
|
110 | 110 |
General Comments 0
You need to be logged in to leave comments.
Login now