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