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