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