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