##// END OF EJS Templates
fix file extension when extracting PDF figures
MinRK -
Show More
@@ -16,6 +16,7 b" notebook file. The extracted outputs are returned in the 'resources' dictionary"
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
@@ -32,9 +33,9 b' class ExtractOutputPreprocessor(Preprocessor):'
32 """
33 """
33
34
34 output_filename_template = Unicode(
35 output_filename_template = Unicode(
35 "{unique_key}_{cell_index}_{index}.{extension}", config=True)
36 "{unique_key}_{cell_index}_{index}{extension}", config=True)
36
37
37 extract_output_types = Set({'png', 'jpg', 'svg', 'pdf', 'application/pdf'}, config=True)
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 """
@@ -70,7 +71,7 b' class ExtractOutputPreprocessor(Preprocessor):'
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', '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
@@ -82,11 +83,19 b' class ExtractOutputPreprocessor(Preprocessor):'
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=out_type)
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)
General Comments 0
You need to be logged in to leave comments. Login now