Show More
@@ -1,312 +1,334 b'' | |||
|
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 | from unittest import mock |
|
9 | 9 | |
|
10 | 10 | import nose.tools as nt |
|
11 | 11 | |
|
12 | 12 | from IPython.core import display |
|
13 | 13 | from IPython.core.getipython import get_ipython |
|
14 | 14 | from IPython.utils.tempdir import NamedFileInTemporaryDirectory |
|
15 | 15 | from IPython import paths as ipath |
|
16 | 16 | |
|
17 | 17 | import IPython.testing.decorators as dec |
|
18 | 18 | |
|
19 | 19 | def test_image_size(): |
|
20 | 20 | """Simple test for display.Image(args, width=x,height=y)""" |
|
21 | 21 | thisurl = 'http://www.google.fr/images/srpr/logo3w.png' |
|
22 | 22 | img = display.Image(url=thisurl, width=200, height=200) |
|
23 | 23 | nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_()) |
|
24 | 24 | img = display.Image(url=thisurl, width=200) |
|
25 | 25 | nt.assert_equal(u'<img src="%s" width="200"/>' % (thisurl), img._repr_html_()) |
|
26 | 26 | img = display.Image(url=thisurl) |
|
27 | 27 | nt.assert_equal(u'<img src="%s"/>' % (thisurl), img._repr_html_()) |
|
28 | 28 | img = display.Image(url=thisurl, unconfined=True) |
|
29 | 29 | nt.assert_equal(u'<img src="%s" class="unconfined"/>' % (thisurl), img._repr_html_()) |
|
30 | 30 | |
|
31 | ||
|
32 | def test_geojson(): | |
|
33 | ||
|
34 | gj = display.GeoJSON(data={ | |
|
35 | "type": "Feature", | |
|
36 | "geometry": { | |
|
37 | "type": "Point", | |
|
38 | "coordinates": [-81.327, 296.038] | |
|
39 | }, | |
|
40 | "properties": { | |
|
41 | "name": "Inca City" | |
|
42 | } | |
|
43 | }, | |
|
44 | url_template="http://s3-eu-west-1.amazonaws.com/whereonmars.cartodb.net/{basemap_id}/{z}/{x}/{y}.png", | |
|
45 | layer_options={ | |
|
46 | "basemap_id": "celestia_mars-shaded-16k_global", | |
|
47 | "attribution": "Celestia/praesepe", | |
|
48 | "minZoom": 0, | |
|
49 | "maxZoom": 18, | |
|
50 | }) | |
|
51 | nt.assert_equal(u'<IPython.core.display.GeoJSON object>', str(g)) | |
|
52 | ||
|
31 | 53 | def test_retina_png(): |
|
32 | 54 | here = os.path.dirname(__file__) |
|
33 | 55 | img = display.Image(os.path.join(here, "2x2.png"), retina=True) |
|
34 | 56 | nt.assert_equal(img.height, 1) |
|
35 | 57 | nt.assert_equal(img.width, 1) |
|
36 | 58 | data, md = img._repr_png_() |
|
37 | 59 | nt.assert_equal(md['width'], 1) |
|
38 | 60 | nt.assert_equal(md['height'], 1) |
|
39 | 61 | |
|
40 | 62 | def test_retina_jpeg(): |
|
41 | 63 | here = os.path.dirname(__file__) |
|
42 | 64 | img = display.Image(os.path.join(here, "2x2.jpg"), retina=True) |
|
43 | 65 | nt.assert_equal(img.height, 1) |
|
44 | 66 | nt.assert_equal(img.width, 1) |
|
45 | 67 | data, md = img._repr_jpeg_() |
|
46 | 68 | nt.assert_equal(md['width'], 1) |
|
47 | 69 | nt.assert_equal(md['height'], 1) |
|
48 | 70 | |
|
49 | 71 | def test_base64image(): |
|
50 | 72 | display.Image("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAWJLR0QAiAUdSAAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94BCRQnOqNu0b4AAAAKSURBVAjXY2AAAAACAAHiIbwzAAAAAElFTkSuQmCC") |
|
51 | 73 | |
|
52 | 74 | def test_image_filename_defaults(): |
|
53 | 75 | '''test format constraint, and validity of jpeg and png''' |
|
54 | 76 | tpath = ipath.get_ipython_package_dir() |
|
55 | 77 | nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.gif'), |
|
56 | 78 | embed=True) |
|
57 | 79 | nt.assert_raises(ValueError, display.Image) |
|
58 | 80 | nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True) |
|
59 | 81 | # check boths paths to allow packages to test at build and install time |
|
60 | 82 | imgfile = os.path.join(tpath, 'core/tests/2x2.png') |
|
61 | 83 | img = display.Image(filename=imgfile) |
|
62 | 84 | nt.assert_equal('png', img.format) |
|
63 | 85 | nt.assert_is_not_none(img._repr_png_()) |
|
64 | 86 | img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False) |
|
65 | 87 | nt.assert_equal('jpeg', img.format) |
|
66 | 88 | nt.assert_is_none(img._repr_jpeg_()) |
|
67 | 89 | |
|
68 | 90 | def _get_inline_config(): |
|
69 | 91 | from ipykernel.pylab.config import InlineBackend |
|
70 | 92 | return InlineBackend.instance() |
|
71 | 93 | |
|
72 | 94 | @dec.skip_without('matplotlib') |
|
73 | 95 | def test_set_matplotlib_close(): |
|
74 | 96 | cfg = _get_inline_config() |
|
75 | 97 | cfg.close_figures = False |
|
76 | 98 | display.set_matplotlib_close() |
|
77 | 99 | assert cfg.close_figures |
|
78 | 100 | display.set_matplotlib_close(False) |
|
79 | 101 | assert not cfg.close_figures |
|
80 | 102 | |
|
81 | 103 | _fmt_mime_map = { |
|
82 | 104 | 'png': 'image/png', |
|
83 | 105 | 'jpeg': 'image/jpeg', |
|
84 | 106 | 'pdf': 'application/pdf', |
|
85 | 107 | 'retina': 'image/png', |
|
86 | 108 | 'svg': 'image/svg+xml', |
|
87 | 109 | } |
|
88 | 110 | |
|
89 | 111 | @dec.skip_without('matplotlib') |
|
90 | 112 | def test_set_matplotlib_formats(): |
|
91 | 113 | from matplotlib.figure import Figure |
|
92 | 114 | formatters = get_ipython().display_formatter.formatters |
|
93 | 115 | for formats in [ |
|
94 | 116 | ('png',), |
|
95 | 117 | ('pdf', 'svg'), |
|
96 | 118 | ('jpeg', 'retina', 'png'), |
|
97 | 119 | (), |
|
98 | 120 | ]: |
|
99 | 121 | active_mimes = {_fmt_mime_map[fmt] for fmt in formats} |
|
100 | 122 | display.set_matplotlib_formats(*formats) |
|
101 | 123 | for mime, f in formatters.items(): |
|
102 | 124 | if mime in active_mimes: |
|
103 | 125 | nt.assert_in(Figure, f) |
|
104 | 126 | else: |
|
105 | 127 | nt.assert_not_in(Figure, f) |
|
106 | 128 | |
|
107 | 129 | @dec.skip_without('matplotlib') |
|
108 | 130 | def test_set_matplotlib_formats_kwargs(): |
|
109 | 131 | from matplotlib.figure import Figure |
|
110 | 132 | ip = get_ipython() |
|
111 | 133 | cfg = _get_inline_config() |
|
112 | 134 | cfg.print_figure_kwargs.update(dict(foo='bar')) |
|
113 | 135 | kwargs = dict(quality=10) |
|
114 | 136 | display.set_matplotlib_formats('png', **kwargs) |
|
115 | 137 | formatter = ip.display_formatter.formatters['image/png'] |
|
116 | 138 | f = formatter.lookup_by_type(Figure) |
|
117 | 139 | cell = f.__closure__[0].cell_contents |
|
118 | 140 | expected = kwargs |
|
119 | 141 | expected.update(cfg.print_figure_kwargs) |
|
120 | 142 | nt.assert_equal(cell, expected) |
|
121 | 143 | |
|
122 | 144 | def test_displayobject_repr(): |
|
123 | 145 | h = display.HTML('<br />') |
|
124 | 146 | nt.assert_equal(repr(h), '<IPython.core.display.HTML object>') |
|
125 | 147 | h._show_mem_addr = True |
|
126 | 148 | nt.assert_equal(repr(h), object.__repr__(h)) |
|
127 | 149 | h._show_mem_addr = False |
|
128 | 150 | nt.assert_equal(repr(h), '<IPython.core.display.HTML object>') |
|
129 | 151 | |
|
130 | 152 | j = display.Javascript('') |
|
131 | 153 | nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>') |
|
132 | 154 | j._show_mem_addr = True |
|
133 | 155 | nt.assert_equal(repr(j), object.__repr__(j)) |
|
134 | 156 | j._show_mem_addr = False |
|
135 | 157 | nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>') |
|
136 | 158 | |
|
137 | 159 | def test_json(): |
|
138 | 160 | d = {'a': 5} |
|
139 | 161 | lis = [d] |
|
140 | 162 | md = {'expanded': False} |
|
141 | 163 | md2 = {'expanded': True} |
|
142 | 164 | j = display.JSON(d) |
|
143 | 165 | j2 = display.JSON(d, expanded=True) |
|
144 | 166 | nt.assert_equal(j._repr_json_(), (d, md)) |
|
145 | 167 | nt.assert_equal(j2._repr_json_(), (d, md2)) |
|
146 | 168 | |
|
147 | 169 | with warnings.catch_warnings(record=True) as w: |
|
148 | 170 | warnings.simplefilter("always") |
|
149 | 171 | j = display.JSON(json.dumps(d)) |
|
150 | 172 | nt.assert_equal(len(w), 1) |
|
151 | 173 | nt.assert_equal(j._repr_json_(), (d, md)) |
|
152 | 174 | nt.assert_equal(j2._repr_json_(), (d, md2)) |
|
153 | 175 | |
|
154 | 176 | j = display.JSON(lis) |
|
155 | 177 | j2 = display.JSON(lis, expanded=True) |
|
156 | 178 | nt.assert_equal(j._repr_json_(), (lis, md)) |
|
157 | 179 | nt.assert_equal(j2._repr_json_(), (lis, md2)) |
|
158 | 180 | |
|
159 | 181 | with warnings.catch_warnings(record=True) as w: |
|
160 | 182 | warnings.simplefilter("always") |
|
161 | 183 | j = display.JSON(json.dumps(lis)) |
|
162 | 184 | nt.assert_equal(len(w), 1) |
|
163 | 185 | nt.assert_equal(j._repr_json_(), (lis, md)) |
|
164 | 186 | nt.assert_equal(j2._repr_json_(), (lis, md2)) |
|
165 | 187 | |
|
166 | 188 | def test_video_embedding(): |
|
167 | 189 | """use a tempfile, with dummy-data, to ensure that video embedding doesn't crash""" |
|
168 | 190 | v = display.Video("http://ignored") |
|
169 | 191 | assert not v.embed |
|
170 | 192 | html = v._repr_html_() |
|
171 | 193 | nt.assert_not_in('src="data:', html) |
|
172 | 194 | nt.assert_in('src="http://ignored"', html) |
|
173 | 195 | |
|
174 | 196 | with nt.assert_raises(ValueError): |
|
175 | 197 | v = display.Video(b'abc') |
|
176 | 198 | |
|
177 | 199 | with NamedFileInTemporaryDirectory('test.mp4') as f: |
|
178 | 200 | f.write(b'abc') |
|
179 | 201 | f.close() |
|
180 | 202 | |
|
181 | 203 | v = display.Video(f.name) |
|
182 | 204 | assert not v.embed |
|
183 | 205 | html = v._repr_html_() |
|
184 | 206 | nt.assert_not_in('src="data:', html) |
|
185 | 207 | |
|
186 | 208 | v = display.Video(f.name, embed=True) |
|
187 | 209 | html = v._repr_html_() |
|
188 | 210 | nt.assert_in('src="data:video/mp4;base64,YWJj"',html) |
|
189 | 211 | |
|
190 | 212 | v = display.Video(f.name, embed=True, mimetype='video/other') |
|
191 | 213 | html = v._repr_html_() |
|
192 | 214 | nt.assert_in('src="data:video/other;base64,YWJj"',html) |
|
193 | 215 | |
|
194 | 216 | v = display.Video(b'abc', embed=True, mimetype='video/mp4') |
|
195 | 217 | html = v._repr_html_() |
|
196 | 218 | nt.assert_in('src="data:video/mp4;base64,YWJj"',html) |
|
197 | 219 | |
|
198 | 220 | v = display.Video(u'YWJj', embed=True, mimetype='video/xyz') |
|
199 | 221 | html = v._repr_html_() |
|
200 | 222 | nt.assert_in('src="data:video/xyz;base64,YWJj"',html) |
|
201 | 223 | |
|
202 | 224 | |
|
203 | 225 | def test_display_id(): |
|
204 | 226 | ip = get_ipython() |
|
205 | 227 | with mock.patch.object(ip.display_pub, 'publish') as pub: |
|
206 | 228 | handle = display.display('x') |
|
207 | 229 | nt.assert_is(handle, None) |
|
208 | 230 | handle = display.display('y', display_id='secret') |
|
209 | 231 | nt.assert_is_instance(handle, display.DisplayHandle) |
|
210 | 232 | handle2 = display.display('z', display_id=True) |
|
211 | 233 | nt.assert_is_instance(handle2, display.DisplayHandle) |
|
212 | 234 | nt.assert_not_equal(handle.display_id, handle2.display_id) |
|
213 | 235 | |
|
214 | 236 | nt.assert_equal(pub.call_count, 3) |
|
215 | 237 | args, kwargs = pub.call_args_list[0] |
|
216 | 238 | nt.assert_equal(args, ()) |
|
217 | 239 | nt.assert_equal(kwargs, { |
|
218 | 240 | 'data': { |
|
219 | 241 | 'text/plain': repr('x') |
|
220 | 242 | }, |
|
221 | 243 | 'metadata': {}, |
|
222 | 244 | }) |
|
223 | 245 | args, kwargs = pub.call_args_list[1] |
|
224 | 246 | nt.assert_equal(args, ()) |
|
225 | 247 | nt.assert_equal(kwargs, { |
|
226 | 248 | 'data': { |
|
227 | 249 | 'text/plain': repr('y') |
|
228 | 250 | }, |
|
229 | 251 | 'metadata': {}, |
|
230 | 252 | 'transient': { |
|
231 | 253 | 'display_id': handle.display_id, |
|
232 | 254 | }, |
|
233 | 255 | }) |
|
234 | 256 | args, kwargs = pub.call_args_list[2] |
|
235 | 257 | nt.assert_equal(args, ()) |
|
236 | 258 | nt.assert_equal(kwargs, { |
|
237 | 259 | 'data': { |
|
238 | 260 | 'text/plain': repr('z') |
|
239 | 261 | }, |
|
240 | 262 | 'metadata': {}, |
|
241 | 263 | 'transient': { |
|
242 | 264 | 'display_id': handle2.display_id, |
|
243 | 265 | }, |
|
244 | 266 | }) |
|
245 | 267 | |
|
246 | 268 | |
|
247 | 269 | def test_update_display(): |
|
248 | 270 | ip = get_ipython() |
|
249 | 271 | with mock.patch.object(ip.display_pub, 'publish') as pub: |
|
250 | 272 | with nt.assert_raises(TypeError): |
|
251 | 273 | display.update_display('x') |
|
252 | 274 | display.update_display('x', display_id='1') |
|
253 | 275 | display.update_display('y', display_id='2') |
|
254 | 276 | args, kwargs = pub.call_args_list[0] |
|
255 | 277 | nt.assert_equal(args, ()) |
|
256 | 278 | nt.assert_equal(kwargs, { |
|
257 | 279 | 'data': { |
|
258 | 280 | 'text/plain': repr('x') |
|
259 | 281 | }, |
|
260 | 282 | 'metadata': {}, |
|
261 | 283 | 'transient': { |
|
262 | 284 | 'display_id': '1', |
|
263 | 285 | }, |
|
264 | 286 | 'update': True, |
|
265 | 287 | }) |
|
266 | 288 | args, kwargs = pub.call_args_list[1] |
|
267 | 289 | nt.assert_equal(args, ()) |
|
268 | 290 | nt.assert_equal(kwargs, { |
|
269 | 291 | 'data': { |
|
270 | 292 | 'text/plain': repr('y') |
|
271 | 293 | }, |
|
272 | 294 | 'metadata': {}, |
|
273 | 295 | 'transient': { |
|
274 | 296 | 'display_id': '2', |
|
275 | 297 | }, |
|
276 | 298 | 'update': True, |
|
277 | 299 | }) |
|
278 | 300 | |
|
279 | 301 | |
|
280 | 302 | def test_display_handle(): |
|
281 | 303 | ip = get_ipython() |
|
282 | 304 | handle = display.DisplayHandle() |
|
283 | 305 | nt.assert_is_instance(handle.display_id, str) |
|
284 | 306 | handle = display.DisplayHandle('my-id') |
|
285 | 307 | nt.assert_equal(handle.display_id, 'my-id') |
|
286 | 308 | with mock.patch.object(ip.display_pub, 'publish') as pub: |
|
287 | 309 | handle.display('x') |
|
288 | 310 | handle.update('y') |
|
289 | 311 | |
|
290 | 312 | args, kwargs = pub.call_args_list[0] |
|
291 | 313 | nt.assert_equal(args, ()) |
|
292 | 314 | nt.assert_equal(kwargs, { |
|
293 | 315 | 'data': { |
|
294 | 316 | 'text/plain': repr('x') |
|
295 | 317 | }, |
|
296 | 318 | 'metadata': {}, |
|
297 | 319 | 'transient': { |
|
298 | 320 | 'display_id': handle.display_id, |
|
299 | 321 | } |
|
300 | 322 | }) |
|
301 | 323 | args, kwargs = pub.call_args_list[1] |
|
302 | 324 | nt.assert_equal(args, ()) |
|
303 | 325 | nt.assert_equal(kwargs, { |
|
304 | 326 | 'data': { |
|
305 | 327 | 'text/plain': repr('y') |
|
306 | 328 | }, |
|
307 | 329 | 'metadata': {}, |
|
308 | 330 | 'transient': { |
|
309 | 331 | 'display_id': handle.display_id, |
|
310 | 332 | }, |
|
311 | 333 | 'update': True, |
|
312 | 334 | }) |
General Comments 0
You need to be logged in to leave comments.
Login now