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