Show More
@@ -1,490 +1,511 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 | import nose.tools as nt | |
|
10 | import pytest | |
|
11 | 11 | |
|
12 | 12 | from IPython import display |
|
13 | 13 | from IPython.core.getipython import get_ipython |
|
14 | 14 | from IPython.utils.io import capture_output |
|
15 | 15 | from IPython.utils.tempdir import NamedFileInTemporaryDirectory |
|
16 | 16 | from IPython import paths as ipath |
|
17 | 17 | from IPython.testing.tools import AssertNotPrints |
|
18 | 18 | |
|
19 | 19 | import IPython.testing.decorators as dec |
|
20 | 20 | |
|
21 | 21 | def test_image_size(): |
|
22 | 22 | """Simple test for display.Image(args, width=x,height=y)""" |
|
23 | 23 | thisurl = 'http://www.google.fr/images/srpr/logo3w.png' |
|
24 | 24 | img = display.Image(url=thisurl, width=200, height=200) |
|
25 |
|
|
|
25 | assert '<img src="%s" width="200" height="200"/>' % (thisurl) == img._repr_html_() | |
|
26 | 26 | img = display.Image(url=thisurl, metadata={'width':200, 'height':200}) |
|
27 |
|
|
|
27 | assert '<img src="%s" width="200" height="200"/>' % (thisurl) == img._repr_html_() | |
|
28 | 28 | img = display.Image(url=thisurl, width=200) |
|
29 |
|
|
|
29 | assert '<img src="%s" width="200"/>' % (thisurl) == img._repr_html_() | |
|
30 | 30 | img = display.Image(url=thisurl) |
|
31 |
|
|
|
31 | assert '<img src="%s"/>' % (thisurl) == img._repr_html_() | |
|
32 | 32 | img = display.Image(url=thisurl, unconfined=True) |
|
33 |
|
|
|
33 | assert '<img src="%s" class="unconfined"/>' % (thisurl) == img._repr_html_() | |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | def test_image_mimes(): |
|
37 | 37 | fmt = get_ipython().display_formatter.format |
|
38 | 38 | for format in display.Image._ACCEPTABLE_EMBEDDINGS: |
|
39 | 39 | mime = display.Image._MIMETYPES[format] |
|
40 | 40 | img = display.Image(b'garbage', format=format) |
|
41 | 41 | data, metadata = fmt(img) |
|
42 |
|
|
|
42 | assert sorted(data) == sorted([mime, "text/plain"]) | |
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | def test_geojson(): |
|
46 | 46 | |
|
47 | 47 | gj = display.GeoJSON(data={ |
|
48 | 48 | "type": "Feature", |
|
49 | 49 | "geometry": { |
|
50 | 50 | "type": "Point", |
|
51 | 51 | "coordinates": [-81.327, 296.038] |
|
52 | 52 | }, |
|
53 | 53 | "properties": { |
|
54 | 54 | "name": "Inca City" |
|
55 | 55 | } |
|
56 | 56 | }, |
|
57 | 57 | url_template="http://s3-eu-west-1.amazonaws.com/whereonmars.cartodb.net/{basemap_id}/{z}/{x}/{y}.png", |
|
58 | 58 | layer_options={ |
|
59 | 59 | "basemap_id": "celestia_mars-shaded-16k_global", |
|
60 | 60 | "attribution": "Celestia/praesepe", |
|
61 | 61 | "minZoom": 0, |
|
62 | 62 | "maxZoom": 18, |
|
63 |
} |
|
|
64 | nt.assert_equal(u'<IPython.core.display.GeoJSON object>', str(gj)) | |
|
63 | }, | |
|
64 | ) | |
|
65 | assert "<IPython.core.display.GeoJSON object>" == str(gj) | |
|
66 | ||
|
65 | 67 | |
|
66 | 68 | def test_retina_png(): |
|
67 | 69 | here = os.path.dirname(__file__) |
|
68 | 70 | img = display.Image(os.path.join(here, "2x2.png"), retina=True) |
|
69 |
|
|
|
70 |
|
|
|
71 | assert img.height == 1 | |
|
72 | assert img.width == 1 | |
|
71 | 73 | data, md = img._repr_png_() |
|
72 |
|
|
|
73 |
|
|
|
74 | assert md["width"] == 1 | |
|
75 | assert md["height"] == 1 | |
|
76 | ||
|
74 | 77 | |
|
75 | 78 | def test_embed_svg_url(): |
|
76 | 79 | import gzip |
|
77 | 80 | from io import BytesIO |
|
78 | 81 | svg_data = b'<svg><circle x="0" y="0" r="1"/></svg>' |
|
79 | 82 | url = 'http://test.com/circle.svg' |
|
80 | 83 | |
|
81 | 84 | gzip_svg = BytesIO() |
|
82 | 85 | with gzip.open(gzip_svg, 'wb') as fp: |
|
83 | 86 | fp.write(svg_data) |
|
84 | 87 | gzip_svg = gzip_svg.getvalue() |
|
85 | 88 | |
|
86 | 89 | def mocked_urlopen(*args, **kwargs): |
|
87 | 90 | class MockResponse: |
|
88 | 91 | def __init__(self, svg): |
|
89 | 92 | self._svg_data = svg |
|
90 | 93 | self.headers = {'content-type': 'image/svg+xml'} |
|
91 | 94 | |
|
92 | 95 | def read(self): |
|
93 | 96 | return self._svg_data |
|
94 | 97 | |
|
95 | 98 | if args[0] == url: |
|
96 | 99 | return MockResponse(svg_data) |
|
97 | 100 | elif args[0] == url + "z": |
|
98 | 101 | ret = MockResponse(gzip_svg) |
|
99 | 102 | ret.headers["content-encoding"] = "gzip" |
|
100 | 103 | return ret |
|
101 | 104 | return MockResponse(None) |
|
102 | 105 | |
|
103 | 106 | with mock.patch('urllib.request.urlopen', side_effect=mocked_urlopen): |
|
104 | 107 | svg = display.SVG(url=url) |
|
105 |
|
|
|
106 |
svg = display.SVG(url=url + |
|
|
107 |
|
|
|
108 | assert svg._repr_svg_().startswith("<svg") is True | |
|
109 | svg = display.SVG(url=url + "z") | |
|
110 | assert svg._repr_svg_().startswith("<svg") is True | |
|
111 | ||
|
108 | 112 | |
|
109 | 113 | def test_retina_jpeg(): |
|
110 | 114 | here = os.path.dirname(__file__) |
|
111 | 115 | img = display.Image(os.path.join(here, "2x2.jpg"), retina=True) |
|
112 |
|
|
|
113 |
|
|
|
116 | assert img.height == 1 | |
|
117 | assert img.width == 1 | |
|
114 | 118 | data, md = img._repr_jpeg_() |
|
115 |
|
|
|
116 |
|
|
|
119 | assert md["width"] == 1 | |
|
120 | assert md["height"] == 1 | |
|
121 | ||
|
117 | 122 | |
|
118 | 123 | def test_base64image(): |
|
119 | 124 | display.Image("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAWJLR0QAiAUdSAAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94BCRQnOqNu0b4AAAAKSURBVAjXY2AAAAACAAHiIbwzAAAAAElFTkSuQmCC") |
|
120 | 125 | |
|
121 | 126 | def test_image_filename_defaults(): |
|
122 | 127 | '''test format constraint, and validity of jpeg and png''' |
|
123 | 128 | tpath = ipath.get_ipython_package_dir() |
|
124 | nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.zip'), | |
|
125 | embed=True) | |
|
126 | nt.assert_raises(ValueError, display.Image) | |
|
127 | nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True) | |
|
128 | # check both paths to allow packages to test at build and install time | |
|
129 | pytest.raises( | |
|
130 | ValueError, | |
|
131 | display.Image, | |
|
132 | filename=os.path.join(tpath, "testing/tests/badformat.zip"), | |
|
133 | embed=True, | |
|
134 | ) | |
|
135 | pytest.raises(ValueError, display.Image) | |
|
136 | pytest.raises( | |
|
137 | ValueError, | |
|
138 | display.Image, | |
|
139 | data="this is not an image", | |
|
140 | format="badformat", | |
|
141 | embed=True, | |
|
142 | ) | |
|
143 | # check boths paths to allow packages to test at build and install time | |
|
129 | 144 | imgfile = os.path.join(tpath, 'core/tests/2x2.png') |
|
130 | 145 | img = display.Image(filename=imgfile) |
|
131 |
|
|
|
132 |
|
|
|
133 | img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False) | |
|
134 | nt.assert_equal('jpeg', img.format) | |
|
135 | nt.assert_is_none(img._repr_jpeg_()) | |
|
146 | assert "png" == img.format | |
|
147 | assert img._repr_png_() is not None | |
|
148 | img = display.Image( | |
|
149 | filename=os.path.join(tpath, "testing/tests/logo.jpg"), embed=False | |
|
150 | ) | |
|
151 | assert "jpeg" == img.format | |
|
152 | assert img._repr_jpeg_() is None | |
|
136 | 153 | |
|
137 | 154 | def _get_inline_config(): |
|
138 | 155 | from matplotlib_inline.config import InlineBackend |
|
139 | 156 | return InlineBackend.instance() |
|
140 | 157 | |
|
141 | 158 | |
|
142 | 159 | @dec.skip_without("ipykernel") |
|
143 | 160 | @dec.skip_without("matplotlib") |
|
144 | 161 | def test_set_matplotlib_close(): |
|
145 | 162 | cfg = _get_inline_config() |
|
146 | 163 | cfg.close_figures = False |
|
147 | 164 | display.set_matplotlib_close() |
|
148 | 165 | assert cfg.close_figures |
|
149 | 166 | display.set_matplotlib_close(False) |
|
150 | 167 | assert not cfg.close_figures |
|
151 | 168 | |
|
152 | 169 | _fmt_mime_map = { |
|
153 | 170 | 'png': 'image/png', |
|
154 | 171 | 'jpeg': 'image/jpeg', |
|
155 | 172 | 'pdf': 'application/pdf', |
|
156 | 173 | 'retina': 'image/png', |
|
157 | 174 | 'svg': 'image/svg+xml', |
|
158 | 175 | } |
|
159 | 176 | |
|
160 | 177 | @dec.skip_without('matplotlib') |
|
161 | 178 | def test_set_matplotlib_formats(): |
|
162 | 179 | from matplotlib.figure import Figure |
|
163 | 180 | formatters = get_ipython().display_formatter.formatters |
|
164 | 181 | for formats in [ |
|
165 | 182 | ('png',), |
|
166 | 183 | ('pdf', 'svg'), |
|
167 | 184 | ('jpeg', 'retina', 'png'), |
|
168 | 185 | (), |
|
169 | 186 | ]: |
|
170 | 187 | active_mimes = {_fmt_mime_map[fmt] for fmt in formats} |
|
171 | 188 | display.set_matplotlib_formats(*formats) |
|
172 | 189 | for mime, f in formatters.items(): |
|
173 | 190 | if mime in active_mimes: |
|
174 |
|
|
|
191 | assert Figure in f | |
|
175 | 192 | else: |
|
176 |
|
|
|
193 | assert Figure not in f | |
|
177 | 194 | |
|
178 | 195 | |
|
179 | 196 | @dec.skip_without("ipykernel") |
|
180 | 197 | @dec.skip_without("matplotlib") |
|
181 | 198 | def test_set_matplotlib_formats_kwargs(): |
|
182 | 199 | from matplotlib.figure import Figure |
|
183 | 200 | ip = get_ipython() |
|
184 | 201 | cfg = _get_inline_config() |
|
185 | 202 | cfg.print_figure_kwargs.update(dict(foo='bar')) |
|
186 | 203 | kwargs = dict(dpi=150) |
|
187 | 204 | display.set_matplotlib_formats('png', **kwargs) |
|
188 | 205 | formatter = ip.display_formatter.formatters['image/png'] |
|
189 | 206 | f = formatter.lookup_by_type(Figure) |
|
190 | 207 | formatter_kwargs = f.keywords |
|
191 | 208 | expected = kwargs |
|
192 | 209 | expected["base64"] = True |
|
193 | 210 | expected["fmt"] = "png" |
|
194 | 211 | expected.update(cfg.print_figure_kwargs) |
|
195 |
|
|
|
212 | assert formatter_kwargs == expected | |
|
196 | 213 | |
|
197 | 214 | def test_display_available(): |
|
198 | 215 | """ |
|
199 | 216 | Test that display is available without import |
|
200 | 217 | |
|
201 | 218 | We don't really care if it's in builtin or anything else, but it should |
|
202 | 219 | always be available. |
|
203 | 220 | """ |
|
204 | 221 | ip = get_ipython() |
|
205 | 222 | with AssertNotPrints('NameError'): |
|
206 | 223 | ip.run_cell('display') |
|
207 | 224 | try: |
|
208 | 225 | ip.run_cell('del display') |
|
209 | 226 | except NameError: |
|
210 | 227 | pass # it's ok, it might be in builtins |
|
211 | 228 | # even if deleted it should be back |
|
212 | 229 | with AssertNotPrints('NameError'): |
|
213 | 230 | ip.run_cell('display') |
|
214 | 231 | |
|
215 | 232 | def test_textdisplayobj_pretty_repr(): |
|
216 | 233 |
|
|
217 |
|
|
|
218 |
|
|
|
234 | assert repr(p) == "<IPython.core.display.Pretty object>" | |
|
235 | assert p.data == "This is a simple test" | |
|
219 | 236 | |
|
220 | 237 |
|
|
221 |
|
|
|
238 | assert repr(p) == object.__repr__(p) | |
|
239 | ||
|
222 | 240 | |
|
223 | 241 | def test_displayobject_repr(): |
|
224 |
h = display.HTML( |
|
|
225 |
|
|
|
242 | h = display.HTML("<br />") | |
|
243 | assert repr(h) == "<IPython.core.display.HTML object>" | |
|
226 | 244 | h._show_mem_addr = True |
|
227 |
|
|
|
245 | assert repr(h) == object.__repr__(h) | |
|
228 | 246 | h._show_mem_addr = False |
|
229 |
|
|
|
247 | assert repr(h) == "<IPython.core.display.HTML object>" | |
|
230 | 248 | |
|
231 |
j = display.Javascript( |
|
|
232 |
|
|
|
249 | j = display.Javascript("") | |
|
250 | assert repr(j) == "<IPython.core.display.Javascript object>" | |
|
233 | 251 | j._show_mem_addr = True |
|
234 |
|
|
|
252 | assert repr(j) == object.__repr__(j) | |
|
235 | 253 | j._show_mem_addr = False |
|
236 |
|
|
|
254 | assert repr(j) == "<IPython.core.display.Javascript object>" | |
|
237 | 255 | |
|
238 | 256 | @mock.patch('warnings.warn') |
|
239 | 257 | def test_encourage_iframe_over_html(m_warn): |
|
240 | 258 | display.HTML() |
|
241 | 259 | m_warn.assert_not_called() |
|
242 | 260 | |
|
243 | 261 | display.HTML('<br />') |
|
244 | 262 | m_warn.assert_not_called() |
|
245 | 263 | |
|
246 | 264 | display.HTML('<html><p>Lots of content here</p><iframe src="http://a.com"></iframe>') |
|
247 | 265 | m_warn.assert_not_called() |
|
248 | 266 | |
|
249 | 267 | display.HTML('<iframe src="http://a.com"></iframe>') |
|
250 | 268 | m_warn.assert_called_with('Consider using IPython.display.IFrame instead') |
|
251 | 269 | |
|
252 | 270 | m_warn.reset_mock() |
|
253 | 271 | display.HTML('<IFRAME SRC="http://a.com"></IFRAME>') |
|
254 | 272 | m_warn.assert_called_with('Consider using IPython.display.IFrame instead') |
|
255 | 273 | |
|
256 | 274 | def test_progress(): |
|
257 | 275 | p = display.ProgressBar(10) |
|
258 |
|
|
|
259 |
p.html_width = |
|
|
276 | assert "0/10" in repr(p) | |
|
277 | p.html_width = "100%" | |
|
260 | 278 | p.progress = 5 |
|
261 | nt.assert_equal(p._repr_html_(), "<progress style='width:100%' max='10' value='5'></progress>") | |
|
279 | assert ( | |
|
280 | p._repr_html_() == "<progress style='width:100%' max='10' value='5'></progress>" | |
|
281 | ) | |
|
282 | ||
|
262 | 283 | |
|
263 | 284 | def test_progress_iter(): |
|
264 | 285 | with capture_output(display=False) as captured: |
|
265 | 286 | for i in display.ProgressBar(5): |
|
266 | 287 | out = captured.stdout |
|
267 |
|
|
|
288 | assert "{0}/5".format(i) in out | |
|
268 | 289 | out = captured.stdout |
|
269 |
|
|
|
290 | assert "5/5" in out | |
|
291 | ||
|
270 | 292 | |
|
271 | 293 | def test_json(): |
|
272 | 294 | d = {'a': 5} |
|
273 | 295 | lis = [d] |
|
274 | 296 | metadata = [ |
|
275 | 297 | {'expanded': False, 'root': 'root'}, |
|
276 | 298 | {'expanded': True, 'root': 'root'}, |
|
277 | 299 | {'expanded': False, 'root': 'custom'}, |
|
278 | 300 | {'expanded': True, 'root': 'custom'}, |
|
279 | 301 | ] |
|
280 | 302 | json_objs = [ |
|
281 | 303 | display.JSON(d), |
|
282 | 304 | display.JSON(d, expanded=True), |
|
283 | 305 | display.JSON(d, root='custom'), |
|
284 | 306 | display.JSON(d, expanded=True, root='custom'), |
|
285 | 307 | ] |
|
286 | 308 | for j, md in zip(json_objs, metadata): |
|
287 |
|
|
|
309 | assert j._repr_json_() == (d, md) | |
|
288 | 310 | |
|
289 | 311 | with warnings.catch_warnings(record=True) as w: |
|
290 | 312 | warnings.simplefilter("always") |
|
291 | 313 | j = display.JSON(json.dumps(d)) |
|
292 |
|
|
|
293 |
|
|
|
314 | assert len(w) == 1 | |
|
315 | assert j._repr_json_() == (d, metadata[0]) | |
|
294 | 316 | |
|
295 | 317 | json_objs = [ |
|
296 | 318 | display.JSON(lis), |
|
297 | 319 | display.JSON(lis, expanded=True), |
|
298 | 320 | display.JSON(lis, root='custom'), |
|
299 | 321 | display.JSON(lis, expanded=True, root='custom'), |
|
300 | 322 | ] |
|
301 | 323 | for j, md in zip(json_objs, metadata): |
|
302 |
|
|
|
324 | assert j._repr_json_() == (lis, md) | |
|
303 | 325 | |
|
304 | 326 | with warnings.catch_warnings(record=True) as w: |
|
305 | 327 | warnings.simplefilter("always") |
|
306 | 328 | j = display.JSON(json.dumps(lis)) |
|
307 |
|
|
|
308 |
|
|
|
329 | assert len(w) == 1 | |
|
330 | assert j._repr_json_() == (lis, metadata[0]) | |
|
331 | ||
|
309 | 332 | |
|
310 | 333 | def test_video_embedding(): |
|
311 | 334 | """use a tempfile, with dummy-data, to ensure that video embedding doesn't crash""" |
|
312 | 335 | v = display.Video("http://ignored") |
|
313 | 336 | assert not v.embed |
|
314 | 337 | html = v._repr_html_() |
|
315 |
|
|
|
316 |
|
|
|
338 | assert 'src="data:' not in html | |
|
339 | assert 'src="http://ignored"' in html | |
|
317 | 340 | |
|
318 |
with |
|
|
341 | with pytest.raises(ValueError): | |
|
319 | 342 | v = display.Video(b'abc') |
|
320 | 343 | |
|
321 | 344 | with NamedFileInTemporaryDirectory('test.mp4') as f: |
|
322 | 345 | f.write(b'abc') |
|
323 | 346 | f.close() |
|
324 | 347 | |
|
325 | 348 | v = display.Video(f.name) |
|
326 | 349 | assert not v.embed |
|
327 | 350 | html = v._repr_html_() |
|
328 |
|
|
|
351 | assert 'src="data:' not in html | |
|
329 | 352 | |
|
330 | 353 | v = display.Video(f.name, embed=True) |
|
331 | 354 | html = v._repr_html_() |
|
332 |
|
|
|
355 | assert 'src="data:video/mp4;base64,YWJj"' in html | |
|
333 | 356 | |
|
334 | 357 | v = display.Video(f.name, embed=True, mimetype='video/other') |
|
335 | 358 | html = v._repr_html_() |
|
336 |
|
|
|
359 | assert 'src="data:video/other;base64,YWJj"' in html | |
|
337 | 360 | |
|
338 | 361 | v = display.Video(b'abc', embed=True, mimetype='video/mp4') |
|
339 | 362 | html = v._repr_html_() |
|
340 |
|
|
|
363 | assert 'src="data:video/mp4;base64,YWJj"' in html | |
|
341 | 364 | |
|
342 | 365 | v = display.Video(u'YWJj', embed=True, mimetype='video/xyz') |
|
343 | 366 | html = v._repr_html_() |
|
344 |
|
|
|
367 | assert 'src="data:video/xyz;base64,YWJj"' in html | |
|
345 | 368 | |
|
346 | 369 | def test_html_metadata(): |
|
347 | 370 | s = "<h1>Test</h1>" |
|
348 | 371 | h = display.HTML(s, metadata={"isolated": True}) |
|
349 |
|
|
|
372 | assert h._repr_html_() == (s, {"isolated": True}) | |
|
373 | ||
|
350 | 374 | |
|
351 | 375 | def test_display_id(): |
|
352 | 376 | ip = get_ipython() |
|
353 | 377 | with mock.patch.object(ip.display_pub, 'publish') as pub: |
|
354 | 378 | handle = display.display('x') |
|
355 |
|
|
|
379 | assert handle is None | |
|
356 | 380 | handle = display.display('y', display_id='secret') |
|
357 |
|
|
|
381 | assert isinstance(handle, display.DisplayHandle) | |
|
358 | 382 | handle2 = display.display('z', display_id=True) |
|
359 |
|
|
|
360 |
|
|
|
383 | assert isinstance(handle2, display.DisplayHandle) | |
|
384 | assert handle.display_id != handle2.display_id | |
|
361 | 385 | |
|
362 |
|
|
|
386 | assert pub.call_count == 3 | |
|
363 | 387 | args, kwargs = pub.call_args_list[0] |
|
364 |
|
|
|
365 |
|
|
|
388 | assert args == () | |
|
389 | assert kwargs == { | |
|
366 | 390 | 'data': { |
|
367 | 391 | 'text/plain': repr('x') |
|
368 | 392 | }, |
|
369 | 393 | 'metadata': {}, |
|
370 |
} |
|
|
394 | } | |
|
371 | 395 | args, kwargs = pub.call_args_list[1] |
|
372 |
|
|
|
373 |
|
|
|
396 | assert args == () | |
|
397 | assert kwargs == { | |
|
374 | 398 | 'data': { |
|
375 | 399 | 'text/plain': repr('y') |
|
376 | 400 | }, |
|
377 | 401 | 'metadata': {}, |
|
378 | 402 | 'transient': { |
|
379 | 403 | 'display_id': handle.display_id, |
|
380 | 404 | }, |
|
381 |
} |
|
|
405 | } | |
|
382 | 406 | args, kwargs = pub.call_args_list[2] |
|
383 |
|
|
|
384 |
|
|
|
407 | assert args == () | |
|
408 | assert kwargs == { | |
|
385 | 409 | 'data': { |
|
386 | 410 | 'text/plain': repr('z') |
|
387 | 411 | }, |
|
388 | 412 | 'metadata': {}, |
|
389 | 413 | 'transient': { |
|
390 | 414 | 'display_id': handle2.display_id, |
|
391 | 415 | }, |
|
392 |
} |
|
|
416 | } | |
|
393 | 417 | |
|
394 | 418 | |
|
395 | 419 | def test_update_display(): |
|
396 | 420 | ip = get_ipython() |
|
397 | 421 | with mock.patch.object(ip.display_pub, 'publish') as pub: |
|
398 |
with |
|
|
422 | with pytest.raises(TypeError): | |
|
399 | 423 | display.update_display('x') |
|
400 | 424 | display.update_display('x', display_id='1') |
|
401 | 425 | display.update_display('y', display_id='2') |
|
402 | 426 | args, kwargs = pub.call_args_list[0] |
|
403 |
|
|
|
404 |
|
|
|
427 | assert args == () | |
|
428 | assert kwargs == { | |
|
405 | 429 | 'data': { |
|
406 | 430 | 'text/plain': repr('x') |
|
407 | 431 | }, |
|
408 | 432 | 'metadata': {}, |
|
409 | 433 | 'transient': { |
|
410 | 434 | 'display_id': '1', |
|
411 | 435 | }, |
|
412 | 436 | 'update': True, |
|
413 |
} |
|
|
437 | } | |
|
414 | 438 | args, kwargs = pub.call_args_list[1] |
|
415 |
|
|
|
416 |
|
|
|
439 | assert args == () | |
|
440 | assert kwargs == { | |
|
417 | 441 | 'data': { |
|
418 | 442 | 'text/plain': repr('y') |
|
419 | 443 | }, |
|
420 | 444 | 'metadata': {}, |
|
421 | 445 | 'transient': { |
|
422 | 446 | 'display_id': '2', |
|
423 | 447 | }, |
|
424 | 448 | 'update': True, |
|
425 |
} |
|
|
449 | } | |
|
426 | 450 | |
|
427 | 451 | |
|
428 | 452 | def test_display_handle(): |
|
429 | 453 | ip = get_ipython() |
|
430 | 454 | handle = display.DisplayHandle() |
|
431 |
|
|
|
432 |
handle = display.DisplayHandle( |
|
|
433 |
|
|
|
434 |
with mock.patch.object(ip.display_pub, |
|
|
435 |
handle.display( |
|
|
436 |
handle.update( |
|
|
455 | assert isinstance(handle.display_id, str) | |
|
456 | handle = display.DisplayHandle("my-id") | |
|
457 | assert handle.display_id == "my-id" | |
|
458 | with mock.patch.object(ip.display_pub, "publish") as pub: | |
|
459 | handle.display("x") | |
|
460 | handle.update("y") | |
|
437 | 461 | |
|
438 | 462 | args, kwargs = pub.call_args_list[0] |
|
439 |
|
|
|
440 |
|
|
|
463 | assert args == () | |
|
464 | assert kwargs == { | |
|
441 | 465 | 'data': { |
|
442 | 466 | 'text/plain': repr('x') |
|
443 | 467 | }, |
|
444 | 468 | 'metadata': {}, |
|
445 | 469 | 'transient': { |
|
446 | 470 | 'display_id': handle.display_id, |
|
447 | 471 | } |
|
448 |
} |
|
|
472 | } | |
|
449 | 473 | args, kwargs = pub.call_args_list[1] |
|
450 |
|
|
|
451 |
|
|
|
474 | assert args == () | |
|
475 | assert kwargs == { | |
|
452 | 476 | 'data': { |
|
453 | 477 | 'text/plain': repr('y') |
|
454 | 478 | }, |
|
455 | 479 | 'metadata': {}, |
|
456 | 480 | 'transient': { |
|
457 | 481 | 'display_id': handle.display_id, |
|
458 | 482 | }, |
|
459 | 483 | 'update': True, |
|
460 |
} |
|
|
484 | } | |
|
461 | 485 | |
|
462 | 486 | |
|
463 | 487 | def test_image_alt_tag(): |
|
464 | 488 | """Simple test for display.Image(args, alt=x,)""" |
|
465 | 489 | thisurl = "http://example.com/image.png" |
|
466 | 490 | img = display.Image(url=thisurl, alt="an image") |
|
467 |
|
|
|
491 | assert '<img src="%s" alt="an image"/>' % (thisurl) == img._repr_html_() | |
|
468 | 492 | img = display.Image(url=thisurl, unconfined=True, alt="an image") |
|
469 |
|
|
|
470 |
|
|
|
471 |
img._repr_html_() |
|
|
493 | assert ( | |
|
494 | '<img src="%s" class="unconfined" alt="an image"/>' % (thisurl) | |
|
495 | == img._repr_html_() | |
|
472 | 496 | ) |
|
473 | 497 | img = display.Image(url=thisurl, alt='>"& <') |
|
474 | nt.assert_equal( | |
|
475 | u'<img src="%s" alt=">"& <"/>' % (thisurl), img._repr_html_() | |
|
476 | ) | |
|
498 | assert '<img src="%s" alt=">"& <"/>' % (thisurl) == img._repr_html_() | |
|
477 | 499 | |
|
478 | 500 | img = display.Image(url=thisurl, metadata={"alt": "an image"}) |
|
479 |
|
|
|
480 | ||
|
501 | assert img.alt == "an image" | |
|
481 | 502 | here = os.path.dirname(__file__) |
|
482 | 503 | img = display.Image(os.path.join(here, "2x2.png"), alt="an image") |
|
483 |
|
|
|
504 | assert img.alt == "an image" | |
|
484 | 505 | _, md = img._repr_png_() |
|
485 |
|
|
|
506 | assert md["alt"] == "an image" | |
|
486 | 507 | |
|
487 | 508 | |
|
488 | @nt.raises(FileNotFoundError) | |
|
489 | 509 | def test_image_bad_filename_raises_proper_exception(): |
|
510 | with pytest.raises(FileNotFoundError): | |
|
490 | 511 | display.Image("/this/file/does/not/exist/")._repr_png_() |
General Comments 0
You need to be logged in to leave comments.
Login now