Show More
@@ -1,148 +1,148 | |||||
1 | # Copyright (c) IPython Development Team. |
|
1 | # Copyright (c) IPython Development Team. | |
2 | # Distributed under the terms of the Modified BSD License. |
|
2 | # Distributed under the terms of the Modified BSD License. | |
3 |
|
3 | |||
4 | import json |
|
4 | import json | |
5 | import os |
|
5 | import os | |
6 | import warnings |
|
6 | import warnings | |
7 |
|
7 | |||
8 | import nose.tools as nt |
|
8 | import nose.tools as nt | |
9 |
|
9 | |||
10 | from IPython.core import display |
|
10 | from IPython.core import display | |
11 | from IPython.core.getipython import get_ipython |
|
11 | from IPython.core.getipython import get_ipython | |
12 | from IPython.utils import path as ipath |
|
12 | from IPython.utils import path as ipath | |
13 |
|
13 | |||
14 | import IPython.testing.decorators as dec |
|
14 | import IPython.testing.decorators as dec | |
15 |
|
15 | |||
16 | def test_image_size(): |
|
16 | def test_image_size(): | |
17 | """Simple test for display.Image(args, width=x,height=y)""" |
|
17 | """Simple test for display.Image(args, width=x,height=y)""" | |
18 | thisurl = 'http://www.google.fr/images/srpr/logo3w.png' |
|
18 | thisurl = 'http://www.google.fr/images/srpr/logo3w.png' | |
19 | img = display.Image(url=thisurl, width=200, height=200) |
|
19 | img = display.Image(url=thisurl, width=200, height=200) | |
20 | nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_()) |
|
20 | nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_()) | |
21 | img = display.Image(url=thisurl, width=200) |
|
21 | img = display.Image(url=thisurl, width=200) | |
22 | nt.assert_equal(u'<img src="%s" width="200"/>' % (thisurl), img._repr_html_()) |
|
22 | nt.assert_equal(u'<img src="%s" width="200"/>' % (thisurl), img._repr_html_()) | |
23 | img = display.Image(url=thisurl) |
|
23 | img = display.Image(url=thisurl) | |
24 | nt.assert_equal(u'<img src="%s"/>' % (thisurl), img._repr_html_()) |
|
24 | nt.assert_equal(u'<img src="%s"/>' % (thisurl), img._repr_html_()) | |
25 |
|
25 | |||
26 | def test_retina_png(): |
|
26 | def test_retina_png(): | |
27 | here = os.path.dirname(__file__) |
|
27 | here = os.path.dirname(__file__) | |
28 | img = display.Image(os.path.join(here, "2x2.png"), retina=True) |
|
28 | img = display.Image(os.path.join(here, "2x2.png"), retina=True) | |
29 | nt.assert_equal(img.height, 1) |
|
29 | nt.assert_equal(img.height, 1) | |
30 | nt.assert_equal(img.width, 1) |
|
30 | nt.assert_equal(img.width, 1) | |
31 | data, md = img._repr_png_() |
|
31 | data, md = img._repr_png_() | |
32 | nt.assert_equal(md['width'], 1) |
|
32 | nt.assert_equal(md['width'], 1) | |
33 | nt.assert_equal(md['height'], 1) |
|
33 | nt.assert_equal(md['height'], 1) | |
34 |
|
34 | |||
35 | def test_retina_jpeg(): |
|
35 | def test_retina_jpeg(): | |
36 | here = os.path.dirname(__file__) |
|
36 | here = os.path.dirname(__file__) | |
37 | img = display.Image(os.path.join(here, "2x2.jpg"), retina=True) |
|
37 | img = display.Image(os.path.join(here, "2x2.jpg"), retina=True) | |
38 | nt.assert_equal(img.height, 1) |
|
38 | nt.assert_equal(img.height, 1) | |
39 | nt.assert_equal(img.width, 1) |
|
39 | nt.assert_equal(img.width, 1) | |
40 | data, md = img._repr_jpeg_() |
|
40 | data, md = img._repr_jpeg_() | |
41 | nt.assert_equal(md['width'], 1) |
|
41 | nt.assert_equal(md['width'], 1) | |
42 | nt.assert_equal(md['height'], 1) |
|
42 | nt.assert_equal(md['height'], 1) | |
43 |
|
43 | |||
44 | def test_image_filename_defaults(): |
|
44 | def test_image_filename_defaults(): | |
45 | '''test format constraint, and validity of jpeg and png''' |
|
45 | '''test format constraint, and validity of jpeg and png''' | |
46 | tpath = ipath.get_ipython_package_dir() |
|
46 | tpath = ipath.get_ipython_package_dir() | |
47 | nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.gif'), |
|
47 | nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.gif'), | |
48 | embed=True) |
|
48 | embed=True) | |
49 | nt.assert_raises(ValueError, display.Image) |
|
49 | nt.assert_raises(ValueError, display.Image) | |
50 | nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True) |
|
50 | nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True) | |
51 | from IPython.html import DEFAULT_STATIC_FILES_PATH |
|
51 | from IPython.html import DEFAULT_STATIC_FILES_PATH | |
52 | # check boths paths to allow packages to test at build and install time |
|
52 | # check boths paths to allow packages to test at build and install time | |
53 | imgfile = os.path.join(tpath, 'html/static/base/images/logo.png') |
|
53 | imgfile = os.path.join(tpath, 'html/static/base/images/logo.png') | |
54 | if not os.path.exists(imgfile): |
|
54 | if not os.path.exists(imgfile): | |
55 | imgfile = os.path.join(DEFAULT_STATIC_FILES_PATH, 'base/images/logo.png') |
|
55 | imgfile = os.path.join(DEFAULT_STATIC_FILES_PATH, 'base/images/logo.png') | |
56 | img = display.Image(filename=imgfile) |
|
56 | img = display.Image(filename=imgfile) | |
57 | nt.assert_equal('png', img.format) |
|
57 | nt.assert_equal('png', img.format) | |
58 | nt.assert_is_not_none(img._repr_png_()) |
|
58 | nt.assert_is_not_none(img._repr_png_()) | |
59 | img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False) |
|
59 | img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False) | |
60 | nt.assert_equal('jpeg', img.format) |
|
60 | nt.assert_equal('jpeg', img.format) | |
61 | nt.assert_is_none(img._repr_jpeg_()) |
|
61 | nt.assert_is_none(img._repr_jpeg_()) | |
62 |
|
62 | |||
63 | def _get_inline_config(): |
|
63 | def _get_inline_config(): | |
64 | from IPython.kernel.zmq.pylab.config import InlineBackend |
|
64 | from IPython.kernel.zmq.pylab.config import InlineBackend | |
65 | return InlineBackend.instance() |
|
65 | return InlineBackend.instance() | |
66 |
|
66 | |||
67 | @dec.skip_without('matplotlib') |
|
67 | @dec.skip_without('matplotlib') | |
68 | def test_set_matplotlib_close(): |
|
68 | def test_set_matplotlib_close(): | |
69 | cfg = _get_inline_config() |
|
69 | cfg = _get_inline_config() | |
70 | cfg.close_figures = False |
|
70 | cfg.close_figures = False | |
71 | display.set_matplotlib_close() |
|
71 | display.set_matplotlib_close() | |
72 | assert cfg.close_figures |
|
72 | assert cfg.close_figures | |
73 | display.set_matplotlib_close(False) |
|
73 | display.set_matplotlib_close(False) | |
74 | assert not cfg.close_figures |
|
74 | assert not cfg.close_figures | |
75 |
|
75 | |||
76 | _fmt_mime_map = { |
|
76 | _fmt_mime_map = { | |
77 | 'png': 'image/png', |
|
77 | 'png': 'image/png', | |
78 | 'jpeg': 'image/jpeg', |
|
78 | 'jpeg': 'image/jpeg', | |
79 | 'pdf': 'application/pdf', |
|
79 | 'pdf': 'application/pdf', | |
80 | 'retina': 'image/png', |
|
80 | 'retina': 'image/png', | |
81 | 'svg': 'image/svg+xml', |
|
81 | 'svg': 'image/svg+xml', | |
82 | } |
|
82 | } | |
83 |
|
83 | |||
84 | @dec.skip_without('matplotlib') |
|
84 | @dec.skip_without('matplotlib') | |
85 | def test_set_matplotlib_formats(): |
|
85 | def test_set_matplotlib_formats(): | |
86 | from matplotlib.figure import Figure |
|
86 | from matplotlib.figure import Figure | |
87 | formatters = get_ipython().display_formatter.formatters |
|
87 | formatters = get_ipython().display_formatter.formatters | |
88 | for formats in [ |
|
88 | for formats in [ | |
89 | ('png',), |
|
89 | ('png',), | |
90 | ('pdf', 'svg'), |
|
90 | ('pdf', 'svg'), | |
91 | ('jpeg', 'retina', 'png'), |
|
91 | ('jpeg', 'retina', 'png'), | |
92 | (), |
|
92 | (), | |
93 | ]: |
|
93 | ]: | |
94 | active_mimes = {_fmt_mime_map[fmt] for fmt in formats} |
|
94 | active_mimes = {_fmt_mime_map[fmt] for fmt in formats} | |
95 | display.set_matplotlib_formats(*formats) |
|
95 | display.set_matplotlib_formats(*formats) | |
96 | for mime, f in formatters.items(): |
|
96 | for mime, f in formatters.items(): | |
97 | if mime in active_mimes: |
|
97 | if mime in active_mimes: | |
98 | nt.assert_in(Figure, f) |
|
98 | nt.assert_in(Figure, f) | |
99 | else: |
|
99 | else: | |
100 | nt.assert_not_in(Figure, f) |
|
100 | nt.assert_not_in(Figure, f) | |
101 |
|
101 | |||
102 | @dec.skip_without('matplotlib') |
|
102 | @dec.skip_without('matplotlib') | |
103 | def test_set_matplotlib_formats_kwargs(): |
|
103 | def test_set_matplotlib_formats_kwargs(): | |
104 | from matplotlib.figure import Figure |
|
104 | from matplotlib.figure import Figure | |
105 | ip = get_ipython() |
|
105 | ip = get_ipython() | |
106 | cfg = _get_inline_config() |
|
106 | cfg = _get_inline_config() | |
107 | cfg.print_figure_kwargs.update(dict(foo='bar')) |
|
107 | cfg.print_figure_kwargs.update(dict(foo='bar')) | |
108 | kwargs = dict(quality=10) |
|
108 | kwargs = dict(quality=10) | |
109 | display.set_matplotlib_formats('png', **kwargs) |
|
109 | display.set_matplotlib_formats('png', **kwargs) | |
110 | formatter = ip.display_formatter.formatters['image/png'] |
|
110 | formatter = ip.display_formatter.formatters['image/png'] | |
111 | f = formatter.lookup_by_type(Figure) |
|
111 | f = formatter.lookup_by_type(Figure) | |
112 | cell = f.__closure__[0].cell_contents |
|
112 | cell = f.__closure__[0].cell_contents | |
113 | expected = kwargs |
|
113 | expected = kwargs | |
114 | expected.update(cfg.print_figure_kwargs) |
|
114 | expected.update(cfg.print_figure_kwargs) | |
115 | nt.assert_equal(cell, expected) |
|
115 | nt.assert_equal(cell, expected) | |
116 |
|
116 | |||
117 | def test_displayobject_repr(): |
|
117 | def test_displayobject_repr(): | |
118 | h = display.HTML('<br />') |
|
118 | h = display.HTML('<br />') | |
119 | nt.assert_equal(repr(h), '<IPython.core.display.HTML object>') |
|
119 | nt.assert_equal(repr(h), '<IPython.core.display.HTML object>') | |
120 | h._show_mem_addr = True |
|
120 | h._show_mem_addr = True | |
121 | nt.assert_equal(repr(h), object.__repr__(h)) |
|
121 | nt.assert_equal(repr(h), object.__repr__(h)) | |
122 | h._show_mem_addr = False |
|
122 | h._show_mem_addr = False | |
123 | nt.assert_equal(repr(h), '<IPython.core.display.HTML object>') |
|
123 | nt.assert_equal(repr(h), '<IPython.core.display.HTML object>') | |
124 |
|
124 | |||
125 | j = display.Javascript('') |
|
125 | j = display.Javascript('') | |
126 | nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>') |
|
126 | nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>') | |
127 | j._show_mem_addr = True |
|
127 | j._show_mem_addr = True | |
128 | nt.assert_equal(repr(j), object.__repr__(j)) |
|
128 | nt.assert_equal(repr(j), object.__repr__(j)) | |
129 | j._show_mem_addr = False |
|
129 | j._show_mem_addr = False | |
130 | nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>') |
|
130 | nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>') | |
131 |
|
131 | |||
132 | def test_json(): |
|
132 | def test_json(): | |
133 | d = {'a': 5} |
|
133 | d = {'a': 5} | |
134 | lis = [d] |
|
134 | lis = [d] | |
135 | j = display.JSON(d) |
|
135 | j = display.JSON(d) | |
136 | nt.assert_equal(j._repr_json_(), d) |
|
136 | nt.assert_equal(j._repr_json_(), d) | |
137 | with warnings.catch_warnings(record=True) as w: |
|
137 | with warnings.catch_warnings(record=True) as w: | |
138 | j = display.JSON(json.dumps(d)) |
|
138 | j = display.JSON(json.dumps(d)) | |
139 |
assert |
|
139 | nt.assert_equal(len(w), 1) | |
140 | nt.assert_equal(j._repr_json_(), d) |
|
140 | nt.assert_equal(j._repr_json_(), d) | |
141 | j = display.JSON(lis) |
|
141 | j = display.JSON(lis) | |
142 | nt.assert_equal(j._repr_json_(), lis) |
|
142 | nt.assert_equal(j._repr_json_(), lis) | |
143 | with warnings.catch_warnings(record=True) as w: |
|
143 | with warnings.catch_warnings(record=True) as w: | |
144 | j = display.JSON(json.dumps(lis)) |
|
144 | j = display.JSON(json.dumps(lis)) | |
145 |
assert |
|
145 | nt.assert_equal(len(w), 1) | |
146 | nt.assert_equal(j._repr_json_(), lis) |
|
146 | nt.assert_equal(j._repr_json_(), lis) | |
147 |
|
147 | |||
148 | No newline at end of file |
|
148 |
General Comments 0
You need to be logged in to leave comments.
Login now