# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import json
import os
import warnings
from unittest import mock
import nose.tools as nt
from IPython import display
from IPython.core.getipython import get_ipython
from IPython.utils.io import capture_output
from IPython.utils.tempdir import NamedFileInTemporaryDirectory
from IPython import paths as ipath
from IPython.testing.tools import AssertNotPrints
import IPython.testing.decorators as dec
def test_image_size():
"""Simple test for display.Image(args, width=x,height=y)"""
thisurl = 'http://www.google.fr/images/srpr/logo3w.png'
img = display.Image(url=thisurl, width=200, height=200)
nt.assert_equal(u'' % (thisurl), img._repr_html_())
img = display.Image(url=thisurl, metadata={'width':200, 'height':200})
nt.assert_equal(u'' % (thisurl), img._repr_html_())
img = display.Image(url=thisurl, width=200)
nt.assert_equal(u'' % (thisurl), img._repr_html_())
img = display.Image(url=thisurl)
nt.assert_equal(u'' % (thisurl), img._repr_html_())
img = display.Image(url=thisurl, unconfined=True)
nt.assert_equal(u'' % (thisurl), img._repr_html_())
def test_image_mimes():
fmt = get_ipython().display_formatter.format
for format in display.Image._ACCEPTABLE_EMBEDDINGS:
mime = display.Image._MIMETYPES[format]
img = display.Image(b'garbage', format=format)
data, metadata = fmt(img)
nt.assert_equal(sorted(data), sorted([mime, 'text/plain']))
def test_geojson():
gj = display.GeoJSON(data={
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-81.327, 296.038]
},
"properties": {
"name": "Inca City"
}
},
url_template="http://s3-eu-west-1.amazonaws.com/whereonmars.cartodb.net/{basemap_id}/{z}/{x}/{y}.png",
layer_options={
"basemap_id": "celestia_mars-shaded-16k_global",
"attribution": "Celestia/praesepe",
"minZoom": 0,
"maxZoom": 18,
})
nt.assert_equal(u'', str(gj))
def test_retina_png():
here = os.path.dirname(__file__)
img = display.Image(os.path.join(here, "2x2.png"), retina=True)
nt.assert_equal(img.height, 1)
nt.assert_equal(img.width, 1)
data, md = img._repr_png_()
nt.assert_equal(md['width'], 1)
nt.assert_equal(md['height'], 1)
def test_embed_svg_url():
import gzip
from io import BytesIO
svg_data = b''
url = 'http://test.com/circle.svg'
gzip_svg = BytesIO()
with gzip.open(gzip_svg, 'wb') as fp:
fp.write(svg_data)
gzip_svg = gzip_svg.getvalue()
def mocked_urlopen(*args, **kwargs):
class MockResponse:
def __init__(self, svg):
self._svg_data = svg
self.headers = {'content-type': 'image/svg+xml'}
def read(self):
return self._svg_data
if args[0] == url:
return MockResponse(svg_data)
elif args[0] == url + 'z':
ret= MockResponse(gzip_svg)
ret.headers['content-encoding']= 'gzip'
return ret
return MockResponse(None)
with mock.patch('urllib.request.urlopen', side_effect=mocked_urlopen):
svg = display.SVG(url=url)
nt.assert_true(svg._repr_svg_().startswith('