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