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