Show More
@@ -16,6 +16,7 notebook file. The extracted outputs are returned in the 'resources' dictionary | |||
|
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 |
@@ -32,9 +33,9 class ExtractOutputPreprocessor(Preprocessor): | |||
|
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 | """ |
@@ -70,7 +71,7 class ExtractOutputPreprocessor(Preprocessor): | |||
|
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 |
@@ -81,12 +82,20 class ExtractOutputPreprocessor(Preprocessor): | |||
|
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) |
General Comments 0
You need to be logged in to leave comments.
Login now