Show More
@@ -1,99 +1,96 b'' | |||||
1 | """Module containing a transformer that extracts all of the figures from the |
|
1 | """Module containing a transformer that extracts all of the figures from the | |
2 | notebook file. The extracted figures are returned in the 'resources' dictionary. |
|
2 | notebook file. The extracted figures are returned in the 'resources' dictionary. | |
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 | import base64 | |||
16 | import sys |
|
17 | import sys | |
|
18 | ||||
17 | from IPython.utils.traitlets import Unicode |
|
19 | from IPython.utils.traitlets import Unicode | |
18 | from .base import Transformer |
|
20 | from .base import Transformer | |
19 | from IPython.utils import py3compat |
|
21 | from IPython.utils import py3compat | |
20 |
|
22 | |||
21 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
22 | # Classes |
|
24 | # Classes | |
23 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
24 |
|
26 | |||
25 | class ExtractFigureTransformer(Transformer): |
|
27 | class ExtractFigureTransformer(Transformer): | |
26 | """ |
|
28 | """ | |
27 | Extracts all of the figures from the notebook file. The extracted |
|
29 | Extracts all of the figures from the notebook file. The extracted | |
28 | figures are returned in the 'resources' dictionary. |
|
30 | figures are returned in the 'resources' dictionary. | |
29 | """ |
|
31 | """ | |
30 |
|
32 | |||
31 | figure_filename_template = Unicode( |
|
33 | figure_filename_template = Unicode( | |
32 | "{unique_key}_{cell_index}_{index}.{extension}", config=True) |
|
34 | "{unique_key}_{cell_index}_{index}.{extension}", config=True) | |
33 |
|
35 | |||
34 |
|
36 | |||
35 | def transform_cell(self, cell, resources, cell_index): |
|
37 | def transform_cell(self, cell, resources, cell_index): | |
36 | """ |
|
38 | """ | |
37 | Apply a transformation on each cell, |
|
39 | Apply a transformation on each cell, | |
38 |
|
40 | |||
39 | Parameters |
|
41 | Parameters | |
40 | ---------- |
|
42 | ---------- | |
41 | cell : NotebookNode cell |
|
43 | cell : NotebookNode cell | |
42 | Notebook cell being processed |
|
44 | Notebook cell being processed | |
43 | resources : dictionary |
|
45 | resources : dictionary | |
44 | Additional resources used in the conversion process. Allows |
|
46 | Additional resources used in the conversion process. Allows | |
45 | transformers to pass variables into the Jinja engine. |
|
47 | transformers to pass variables into the Jinja engine. | |
46 | cell_index : int |
|
48 | cell_index : int | |
47 | Index of the cell being processed (see base.py) |
|
49 | Index of the cell being processed (see base.py) | |
48 | """ |
|
50 | """ | |
49 |
|
51 | |||
50 | #Get the unique key from the resource dict if it exists. If it does not |
|
52 | #Get the unique key from the resource dict if it exists. If it does not | |
51 | #exist, use 'figure' as the default. |
|
53 | #exist, use 'figure' as the default. | |
52 | unique_key = resources.get('unique_key', 'figure') |
|
54 | unique_key = resources.get('unique_key', 'figure') | |
53 |
|
55 | |||
54 | #Make sure figures key exists |
|
56 | #Make sure figures key exists | |
55 | if not 'figures' in resources: |
|
57 | if not 'figures' in resources: | |
56 | resources['figures'] = {} |
|
58 | resources['figures'] = {} | |
57 |
|
59 | |||
58 | #Loop through all of the outputs in the cell |
|
60 | #Loop through all of the outputs in the cell | |
59 | for index, out in enumerate(cell.get('outputs', [])): |
|
61 | for index, out in enumerate(cell.get('outputs', [])): | |
60 |
|
62 | |||
61 | #Get the output in data formats that the template is interested in. |
|
63 | #Get the output in data formats that the template is interested in. | |
62 | for out_type in self.display_data_priority: |
|
64 | for out_type in self.display_data_priority: | |
63 | if out.hasattr(out_type): |
|
65 | if out.hasattr(out_type): | |
64 | data = out[out_type] |
|
66 | data = out[out_type] | |
65 |
|
67 | |||
66 | #Binary files are base64-encoded, SVG is already XML |
|
68 | #Binary files are base64-encoded, SVG is already XML | |
67 | if out_type in ('png', 'jpg', 'jpeg', 'pdf'): |
|
69 | if out_type in ('png', 'jpg', 'jpeg', 'pdf'): | |
68 |
|
70 | # data is b64-encoded as text (str, unicode) | ||
69 | #Python3 base64 is in a separate library... |
|
71 | # decodestring only accepts bytes | |
70 |
|
|
72 | data = py3compat.cast_bytes(data) | |
71 |
|
|
73 | data = base64.decodestring(data) | |
72 | #Base 64 decode the bytes |
|
|||
73 | import base64 |
|
|||
74 | data = base64.b64decode(data) |
|
|||
75 | else: |
|
|||
76 | data = data.decode('base64') |
|
|||
77 | elif sys.platform == 'win32': |
|
74 | elif sys.platform == 'win32': | |
78 | data = data.replace('\n', '\r\n').encode("UTF-8") |
|
75 | data = data.replace('\n', '\r\n').encode("UTF-8") | |
79 | else: |
|
76 | else: | |
80 | data = data.encode("UTF-8") |
|
77 | data = data.encode("UTF-8") | |
81 |
|
78 | |||
82 | #Build a figure name |
|
79 | #Build a figure name | |
83 | figure_name = self.figure_filename_template.format( |
|
80 | figure_name = self.figure_filename_template.format( | |
84 | unique_key=unique_key, |
|
81 | unique_key=unique_key, | |
85 | cell_index=cell_index, |
|
82 | cell_index=cell_index, | |
86 | index=index, |
|
83 | index=index, | |
87 | extension=out_type) |
|
84 | extension=out_type) | |
88 |
|
85 | |||
89 | #On the cell, make the figure available via |
|
86 | #On the cell, make the figure available via | |
90 | # cell.outputs[i].svg_filename ... etc (svg in example) |
|
87 | # cell.outputs[i].svg_filename ... etc (svg in example) | |
91 | # Where |
|
88 | # Where | |
92 | # cell.outputs[i].svg contains the data |
|
89 | # cell.outputs[i].svg contains the data | |
93 | out[out_type + '_filename'] = figure_name |
|
90 | out[out_type + '_filename'] = figure_name | |
94 |
|
91 | |||
95 | #In the resources, make the figure available via |
|
92 | #In the resources, make the figure available via | |
96 | # resources['figures']['filename'] = data |
|
93 | # resources['figures']['filename'] = data | |
97 | resources['figures'][figure_name] = data |
|
94 | resources['figures'][figure_name] = data | |
98 |
|
95 | |||
99 | return cell, resources |
|
96 | return cell, resources |
General Comments 0
You need to be logged in to leave comments.
Login now