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