##// END OF EJS Templates
Extract output preprocessor only extracts specified formats
Thomas Kluyver -
Show More
@@ -1,4 +1,5 b''
1 # coding: utf-8
1 # coding: utf-8
2 import base64
2 import io
3 import io
3 import json
4 import json
4 import os
5 import os
@@ -37,6 +38,10 b' class NbconvertAPI(object):'
37 def list_formats(self):
38 def list_formats(self):
38 return self._req('GET', '')
39 return self._req('GET', '')
39
40
41 png_green_pixel = base64.encodestring(b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00'
42 b'\x00\x00\x01\x00\x00x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDAT'
43 b'\x08\xd7c\x90\xfb\xcf\x00\x00\x02\\\x01\x1e.~d\x87\x00\x00\x00\x00IEND\xaeB`\x82')
44
40 class APITest(NotebookTestBase):
45 class APITest(NotebookTestBase):
41 def setUp(self):
46 def setUp(self):
42 nbdir = self.notebook_dir.name
47 nbdir = self.notebook_dir.name
@@ -51,6 +56,7 b' class APITest(NotebookTestBase):'
51 ws.cells.append(new_heading_cell(u'Created by test ³'))
56 ws.cells.append(new_heading_cell(u'Created by test ³'))
52 cc1 = new_code_cell(input=u'print(2*6)')
57 cc1 = new_code_cell(input=u'print(2*6)')
53 cc1.outputs.append(new_output(output_text=u'12'))
58 cc1.outputs.append(new_output(output_text=u'12'))
59 cc1.outputs.append(new_output(output_png=png_green_pixel, output_type='pyout'))
54 ws.cells.append(cc1)
60 ws.cells.append(cc1)
55
61
56 with io.open(pjoin(nbdir, 'foo', 'testnb.ipynb'), 'w',
62 with io.open(pjoin(nbdir, 'foo', 'testnb.ipynb'), 'w',
@@ -17,7 +17,7 b' import base64'
17 import sys
17 import sys
18 import os
18 import os
19
19
20 from IPython.utils.traitlets import Unicode
20 from IPython.utils.traitlets import Unicode, Set
21 from .base import Preprocessor
21 from .base import Preprocessor
22 from IPython.utils import py3compat
22 from IPython.utils import py3compat
23
23
@@ -34,6 +34,7 b' class ExtractOutputPreprocessor(Preprocessor):'
34 output_filename_template = Unicode(
34 output_filename_template = Unicode(
35 "{unique_key}_{cell_index}_{index}.{extension}", config=True)
35 "{unique_key}_{cell_index}_{index}.{extension}", config=True)
36
36
37 extract_output_types = Set({'png', 'jpg', 'svg', 'pdf'}, config=True)
37
38
38 def preprocess_cell(self, cell, resources, cell_index):
39 def preprocess_cell(self, cell, resources, cell_index):
39 """
40 """
@@ -63,8 +64,8 b' class ExtractOutputPreprocessor(Preprocessor):'
63 #Loop through all of the outputs in the cell
64 #Loop through all of the outputs in the cell
64 for index, out in enumerate(cell.get('outputs', [])):
65 for index, out in enumerate(cell.get('outputs', [])):
65
66
66 #Get the output in data formats that the template is interested in.
67 #Get the output in data formats that the template needs extracted
67 for out_type in self.display_data_priority:
68 for out_type in self.extract_output_types:
68 if out.hasattr(out_type):
69 if out.hasattr(out_type):
69 data = out[out_type]
70 data = out[out_type]
70
71
@@ -27,7 +27,7 b' it introduces a new line'
27 {# .... #}
27 {# .... #}
28
28
29 {% block pyout %}
29 {% block pyout %}
30 {{ output.text | indent | comment_lines }}
30 {{ output.text or '' | indent | comment_lines }}
31 {% endblock pyout %}
31 {% endblock pyout %}
32
32
33 {% block stream %}
33 {% block stream %}
@@ -48,4 +48,4 b' it introduces a new line'
48
48
49 {% block unknowncell scoped %}
49 {% block unknowncell scoped %}
50 unknown type {{ cell.type }}
50 unknown type {{ cell.type }}
51 {% endblock unknowncell %} No newline at end of file
51 {% endblock unknowncell %}
General Comments 0
You need to be logged in to leave comments. Login now