Show More
@@ -1,113 +1,113 b'' | |||
|
1 | 1 | """ |
|
2 | 2 | Module with tests for exporter.py |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | 5 | #----------------------------------------------------------------------------- |
|
6 | 6 | # Copyright (c) 2013, the IPython Development Team. |
|
7 | 7 | # |
|
8 | 8 | # Distributed under the terms of the Modified BSD License. |
|
9 | 9 | # |
|
10 | 10 | # The full license is in the file COPYING.txt, distributed with this software. |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | # Imports |
|
15 | 15 | #----------------------------------------------------------------------------- |
|
16 | 16 | |
|
17 | 17 | from IPython.config import Config |
|
18 | 18 | |
|
19 | 19 | from .base import ExportersTestsBase |
|
20 | 20 | from .cheese import CheeseTransformer |
|
21 | 21 | from ..exporter import Exporter |
|
22 | 22 | |
|
23 | 23 | |
|
24 | 24 | #----------------------------------------------------------------------------- |
|
25 | 25 | # Class |
|
26 | 26 | #----------------------------------------------------------------------------- |
|
27 | 27 | |
|
28 | 28 | class TestExporter(ExportersTestsBase): |
|
29 | 29 | """Contains test functions for exporter.py""" |
|
30 | 30 | |
|
31 | 31 | |
|
32 | 32 | def test_constructor(self): |
|
33 | 33 | """ |
|
34 | 34 | Can an Exporter be constructed? |
|
35 | 35 | """ |
|
36 | 36 | Exporter() |
|
37 | 37 | |
|
38 | 38 | |
|
39 | 39 | def test_export(self): |
|
40 | 40 | """ |
|
41 | 41 | Can an Exporter export something? |
|
42 | 42 | """ |
|
43 | 43 | exporter = self._make_exporter() |
|
44 | 44 | (output, resources) = exporter.from_filename(self._get_notebook()) |
|
45 | 45 | assert len(output) > 0 |
|
46 | 46 | |
|
47 | 47 | |
|
48 |
def test_extract_ |
|
|
48 | def test_extract_outputs(self): | |
|
49 | 49 | """ |
|
50 |
If the ExtractOutputTransformer is enabled, are |
|
|
50 | If the ExtractOutputTransformer is enabled, are outputs extracted? | |
|
51 | 51 | """ |
|
52 | 52 | config = Config({'ExtractOutputTransformer': {'enabled': True}}) |
|
53 | 53 | exporter = self._make_exporter(config=config) |
|
54 | 54 | (output, resources) = exporter.from_filename(self._get_notebook()) |
|
55 | 55 | assert resources is not None |
|
56 |
assert ' |
|
|
57 |
assert len(resources[' |
|
|
56 | assert 'outputs' in resources | |
|
57 | assert len(resources['outputs']) > 0 | |
|
58 | 58 | |
|
59 | 59 | |
|
60 | 60 | def test_transformer_class(self): |
|
61 | 61 | """ |
|
62 | 62 | Can a transformer be added to the transformers list by class type? |
|
63 | 63 | """ |
|
64 | 64 | config = Config({'Exporter': {'transformers': [CheeseTransformer]}}) |
|
65 | 65 | exporter = self._make_exporter(config=config) |
|
66 | 66 | (output, resources) = exporter.from_filename(self._get_notebook()) |
|
67 | 67 | assert resources is not None |
|
68 | 68 | assert 'cheese' in resources |
|
69 | 69 | assert resources['cheese'] == 'real' |
|
70 | 70 | |
|
71 | 71 | |
|
72 | 72 | def test_transformer_instance(self): |
|
73 | 73 | """ |
|
74 | 74 | Can a transformer be added to the transformers list by instance? |
|
75 | 75 | """ |
|
76 | 76 | config = Config({'Exporter': {'transformers': [CheeseTransformer()]}}) |
|
77 | 77 | exporter = self._make_exporter(config=config) |
|
78 | 78 | (output, resources) = exporter.from_filename(self._get_notebook()) |
|
79 | 79 | assert resources is not None |
|
80 | 80 | assert 'cheese' in resources |
|
81 | 81 | assert resources['cheese'] == 'real' |
|
82 | 82 | |
|
83 | 83 | |
|
84 | 84 | def test_transformer_dottedobjectname(self): |
|
85 | 85 | """ |
|
86 | 86 | Can a transformer be added to the transformers list by dotted object name? |
|
87 | 87 | """ |
|
88 | 88 | config = Config({'Exporter': {'transformers': ['IPython.nbconvert.exporters.tests.cheese.CheeseTransformer']}}) |
|
89 | 89 | exporter = self._make_exporter(config=config) |
|
90 | 90 | (output, resources) = exporter.from_filename(self._get_notebook()) |
|
91 | 91 | assert resources is not None |
|
92 | 92 | assert 'cheese' in resources |
|
93 | 93 | assert resources['cheese'] == 'real' |
|
94 | 94 | |
|
95 | 95 | |
|
96 | 96 | def test_transformer_via_method(self): |
|
97 | 97 | """ |
|
98 | 98 | Can a transformer be added via the Exporter convinience method? |
|
99 | 99 | """ |
|
100 | 100 | exporter = self._make_exporter() |
|
101 | 101 | exporter.register_transformer(CheeseTransformer, enabled=True) |
|
102 | 102 | (output, resources) = exporter.from_filename(self._get_notebook()) |
|
103 | 103 | assert resources is not None |
|
104 | 104 | assert 'cheese' in resources |
|
105 | 105 | assert resources['cheese'] == 'real' |
|
106 | 106 | |
|
107 | 107 | |
|
108 | 108 | def _make_exporter(self, config=None): |
|
109 | 109 | #Create the exporter instance, make sure to set a template name since |
|
110 | 110 | #the base Exporter doesn't have a template associated with it. |
|
111 | 111 | exporter = Exporter(config=config) |
|
112 | 112 | exporter.template_file = 'python' |
|
113 | 113 | return exporter No newline at end of file |
@@ -1,64 +1,64 b'' | |||
|
1 | 1 | """Module containing a transformer that converts outputs in the notebook from |
|
2 | 2 | one format to another. |
|
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 | from .base import Transformer |
|
17 | 17 | from IPython.utils.traitlets import Unicode |
|
18 | 18 | |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | # Classes |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | |
|
23 | 23 | class ConvertFiguresTransformer(Transformer): |
|
24 | 24 | """ |
|
25 | 25 | Converts all of the outputs in a notebook from one format to another. |
|
26 | 26 | """ |
|
27 | 27 | |
|
28 | 28 | from_format = Unicode(config=True, help='Format the converter accepts') |
|
29 | 29 | to_format = Unicode(config=True, help='Format the converter writes') |
|
30 | 30 | |
|
31 | 31 | def __init__(self, **kw): |
|
32 | 32 | """ |
|
33 | 33 | Public constructor |
|
34 | 34 | """ |
|
35 | 35 | super(ConvertFiguresTransformer, self).__init__(**kw) |
|
36 | 36 | |
|
37 | 37 | |
|
38 | 38 | def convert_figure(self, data_format, data): |
|
39 | 39 | raise NotImplementedError() |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | def transform_cell(self, cell, resources, cell_index): |
|
43 | 43 | """ |
|
44 | 44 | Apply a transformation on each cell, |
|
45 | 45 | |
|
46 | 46 | See base.py |
|
47 | 47 | """ |
|
48 | 48 | |
|
49 | #Loop through all of the datatypes of the outputs in the cell. | |
|
49 | # Loop through all of the datatypes of the outputs in the cell. | |
|
50 | 50 | for index, cell_out in enumerate(cell.get('outputs', [])): |
|
51 | 51 | for data_type, data in cell_out.items(): |
|
52 |
# this must run *before* extract |
|
|
52 | # this must run *before* extract outputs, | |
|
53 | 53 | # so figure_name and filename do not exist |
|
54 | 54 | self._convert_figure(cell_out, resources, data_type, data) |
|
55 | 55 | return cell, resources |
|
56 | 56 | |
|
57 | 57 | |
|
58 | 58 | def _convert_figure(self, cell_out, resources, data_type, data): |
|
59 | 59 | """ |
|
60 | 60 | Convert a figure and output the results to the cell output |
|
61 | 61 | """ |
|
62 | 62 | if not self.to_format in cell_out and data_type == self.from_format: |
|
63 | 63 | data = self.convert_figure(data_type, data) |
|
64 | 64 | cell_out[self.to_format] = data |
General Comments 0
You need to be logged in to leave comments.
Login now