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