Show More
@@ -1,63 +1,79 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 | #----------------------------------------------------------------------------- |
|
12 | #----------------------------------------------------------------------------- | |
13 | # Imports |
|
13 | # Imports | |
14 | #----------------------------------------------------------------------------- |
|
14 | #----------------------------------------------------------------------------- | |
15 |
|
15 | |||
16 | from .base import ConfigurableTransformer |
|
16 | from .base import ConfigurableTransformer | |
17 | from IPython.utils.traitlets import Unicode |
|
17 | from IPython.utils.traitlets import Unicode | |
18 |
|
18 | |||
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 | # Classes |
|
20 | # Classes | |
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 |
|
22 | |||
23 | class ConvertFiguresTransformer(ConfigurableTransformer): |
|
23 | class ConvertFiguresTransformer(ConfigurableTransformer): | |
24 | """ |
|
24 | """ | |
25 | Converts all of the outputs in a notebook from one format to another. |
|
25 | Converts all of the outputs in a notebook from one format to another. | |
26 | """ |
|
26 | """ | |
27 |
|
27 | |||
28 | from_format = Unicode(config=True, help='Format the converter accepts') |
|
28 | from_format = Unicode(config=True, help='Format the converter accepts') | |
29 | to_format = Unicode(config=True, help='Format the converter writes') |
|
29 | to_format = Unicode(config=True, help='Format the converter writes') | |
30 |
|
30 | |||
31 | def __init__(self, **kw): |
|
31 | def __init__(self, **kw): | |
32 | """ |
|
32 | """ | |
33 | Public constructor |
|
33 | Public constructor | |
34 | """ |
|
34 | """ | |
35 | super(ConvertFiguresTransformer, self).__init__(**kw) |
|
35 | super(ConvertFiguresTransformer, self).__init__(**kw) | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | def convert_figure(self, data_format, data): |
|
38 | def convert_figure(self, data_format, data): | |
39 | raise NotImplementedError() |
|
39 | raise NotImplementedError() | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | def transform_cell(self, cell, resources, cell_index): |
|
42 | def transform_cell(self, cell, resources, cell_index): | |
43 | """ |
|
43 | """ | |
44 | Apply a transformation on each cell, |
|
44 | Apply a transformation on each cell, | |
45 |
|
45 | |||
46 | See base.py |
|
46 | See base.py | |
47 | """ |
|
47 | """ | |
48 |
|
48 | |||
49 | #Loop through all of the datatypes of the outputs in the cell. |
|
49 | #Loop through all of the datatypes of the outputs in the cell. | |
50 | for index, cell_out in enumerate(cell.get('outputs', [])): |
|
50 | for index, cell_out in enumerate(cell.get('outputs', [])): | |
51 | for data_type, data in cell_out.items(): |
|
51 | for data_type, data in cell_out.items(): | |
52 | self._convert_figure(cell_out, data_type, data) |
|
52 | ||
|
53 | #Get the name of the file exported by the extract figure | |||
|
54 | #transformer. Do not try to convert the figure if the extract | |||
|
55 | #fig transformer hasn't touched it | |||
|
56 | filename = cell_out.get(data_type + '_filename', None) | |||
|
57 | if filename: | |||
|
58 | figure_name = filename[:filename.rfind('.')] | |||
|
59 | self._convert_figure(cell_out, figure_name, resources, data_type, data) | |||
53 | return cell, resources |
|
60 | return cell, resources | |
54 |
|
61 | |||
55 |
|
62 | |||
56 | def _convert_figure(self, cell_out, data_type, data): |
|
63 | def _convert_figure(self, cell_out, resources, figure_name, data_type, data): | |
57 | """ |
|
64 | """ | |
58 | Convert a figure and output the results to the cell output |
|
65 | Convert a figure and output the results to the cell output | |
59 | """ |
|
66 | """ | |
60 |
|
67 | |||
61 |
if not self. |
|
68 | if not self.to_format in cell_out: | |
62 |
if data_type |
|
69 | if data_type == self.from_format: | |
63 | cell_out[self._to_format] = self.convert_figure(data_type, data) |
|
70 | filename = figure_name + '.' + self.to_format | |
|
71 | if filename not in resources['figures']: | |||
|
72 | ||||
|
73 | #On the cell, make the figure available via | |||
|
74 | # cell.outputs[i].pdf_filename ... etc (PDF in example) | |||
|
75 | cell_out[self.to_format + '_filename'] = filename | |||
|
76 | ||||
|
77 | #In the resources, make the figure available via | |||
|
78 | # resources['figures']['filename'] = data | |||
|
79 | resources['figures'][filename] = self.convert_figure(data_type, data) |
General Comments 0
You need to be logged in to leave comments.
Login now