Show More
@@ -1,77 +1,75 b'' | |||||
1 | """Module containing a transformer that converts outputs in the notebook from |
|
1 | """Module containing a transformer that converts outputs in the notebook from | |
2 | one format to another. |
|
2 | one format to another. | |
3 | """ |
|
3 | """ | |
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (c) 2013, the IPython Development Team. |
|
5 | # Copyright (c) 2013, the IPython Development Team. | |
6 | # |
|
6 | # | |
7 | # Distributed under the terms of the Modified BSD License. |
|
7 | # Distributed under the terms of the Modified BSD License. | |
8 | # |
|
8 | # | |
9 | # The full license is in the file COPYING.txt, distributed with this software. |
|
9 | # The full license is in the file COPYING.txt, distributed with this software. | |
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 |
|
11 | |||
12 | #TODO: Come after extract |
|
|||
13 |
|
||||
14 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
15 | # Imports |
|
13 | # Imports | |
16 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
17 |
|
15 | |||
18 | from .base import ConfigurableTransformer |
|
16 | from .base import ConfigurableTransformer | |
19 |
|
17 | |||
20 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
21 | # Classes |
|
19 | # Classes | |
22 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
23 |
|
21 | |||
24 | class ConvertFiguresTransformer(ConfigurableTransformer): |
|
22 | class ConvertFiguresTransformer(ConfigurableTransformer): | |
25 | """ |
|
23 | """ | |
26 | Converts all of the outputs in a notebook from one format to another. |
|
24 | Converts all of the outputs in a notebook from one format to another. | |
27 | """ |
|
25 | """ | |
28 |
|
26 | |||
29 |
|
27 | |||
30 | def __init__(self, from_formats, to_format, **kw): |
|
28 | def __init__(self, from_formats, to_format, **kw): | |
31 | """ |
|
29 | """ | |
32 | Public constructor |
|
30 | Public constructor | |
33 |
|
31 | |||
34 | Parameters |
|
32 | Parameters | |
35 | ---------- |
|
33 | ---------- | |
36 | from_formats : list [of string] |
|
34 | from_formats : list [of string] | |
37 | Formats that the converter can convert from |
|
35 | Formats that the converter can convert from | |
38 | to_format : string |
|
36 | to_format : string | |
39 | Format that the converter converts to |
|
37 | Format that the converter converts to | |
40 | config : Config |
|
38 | config : Config | |
41 | Configuration file structure |
|
39 | Configuration file structure | |
42 | **kw : misc |
|
40 | **kw : misc | |
43 | Additional arguments |
|
41 | Additional arguments | |
44 | """ |
|
42 | """ | |
45 | super(ConvertFiguresTransformer, self).__init__(**kw) |
|
43 | super(ConvertFiguresTransformer, self).__init__(**kw) | |
46 |
|
44 | |||
47 | #TODO: Configurable, singular |
|
45 | #TODO: Configurable, singular | |
48 | self._from_formats = from_formats |
|
46 | self._from_formats = from_formats | |
49 | self._to_format = to_format |
|
47 | self._to_format = to_format | |
50 |
|
48 | |||
51 |
|
49 | |||
52 | def convert_figure(self, data_format, data): |
|
50 | def convert_figure(self, data_format, data): | |
53 | raise NotImplementedError() |
|
51 | raise NotImplementedError() | |
54 |
|
52 | |||
55 |
|
53 | |||
56 | def transform_cell(self, cell, resources, cell_index): |
|
54 | def transform_cell(self, cell, resources, cell_index): | |
57 | """ |
|
55 | """ | |
58 | Apply a transformation on each cell, |
|
56 | Apply a transformation on each cell, | |
59 |
|
57 | |||
60 | See base.py |
|
58 | See base.py | |
61 | """ |
|
59 | """ | |
62 |
|
60 | |||
63 | #Loop through all of the datatypes of the outputs in the cell. |
|
61 | #Loop through all of the datatypes of the outputs in the cell. | |
64 | for index, cell_out in enumerate(cell.get('outputs', [])): |
|
62 | for index, cell_out in enumerate(cell.get('outputs', [])): | |
65 | for data_type, data in cell_out.items(): |
|
63 | for data_type, data in cell_out.items(): | |
66 | self._convert_figure(cell_out, data_type, data) |
|
64 | self._convert_figure(cell_out, data_type, data) | |
67 | return cell, resources |
|
65 | return cell, resources | |
68 |
|
66 | |||
69 |
|
67 | |||
70 | def _convert_figure(self, cell_out, data_type, data): |
|
68 | def _convert_figure(self, cell_out, data_type, data): | |
71 | """ |
|
69 | """ | |
72 | Convert a figure and output the results to the cell output |
|
70 | Convert a figure and output the results to the cell output | |
73 | """ |
|
71 | """ | |
74 |
|
72 | |||
75 | if not self._to_format in cell_out: |
|
73 | if not self._to_format in cell_out: | |
76 | if data_type in self._from_formats: |
|
74 | if data_type in self._from_formats: | |
77 | cell_out[self._to_format] = self.convert_figure(data_type, data) |
|
75 | cell_out[self._to_format] = self.convert_figure(data_type, data) |
General Comments 0
You need to be logged in to leave comments.
Login now