1 2
from __future__ import absolute_import, division, unicode_literals
2

3 2
import os
4 2
import sys
5 2
import pytest
6

7 2
from base64 import b64decode, b64encode
8

9 2
from panel.pane import GIF, JPG, PNG, SVG
10 2
from panel.pane.markup import escape
11 2
from io import BytesIO, StringIO
12

13

14 2
def test_svg_pane(document, comm):
15 2
    rect = """
16
    <svg xmlns="http://www.w3.org/2000/svg">
17
      <rect x="10" y="10" height="100" width="100"/>
18
    </svg>
19
    """
20 2
    pane = SVG(rect, encode=True)
21

22
    # Create pane
23 2
    model = pane.get_root(document, comm=comm)
24 2
    assert pane._models[model.ref['id']][0] is model
25 2
    assert model.text.startswith('&lt;img src=&#x27;data:image/svg+xml;base64')
26 2
    assert b64encode(rect.encode('utf-8')).decode('utf-8') in model.text
27

28
    # Replace Pane.object
29 2
    circle = """
30
    <svg xmlns="http://www.w3.org/2000/svg" height="100">
31
      <circle cx="50" cy="50" r="40" />
32
    </svg>
33
    """
34 2
    pane.object = circle
35 2
    assert pane._models[model.ref['id']][0] is model
36 2
    assert model.text.startswith('&lt;img src=&#x27;data:image/svg+xml;base64')
37 2
    assert b64encode(circle.encode('utf-8')).decode('utf-8') in model.text
38

39 2
    pane.encode = False
40 2
    assert model.text == escape(circle)
41

42
    # Cleanup
43 2
    pane._cleanup(model)
44 2
    assert pane._models == {}
45

46

47 2
twopixel = dict(\
48
    gif = b'R0lGODlhAgABAPAAAEQ6Q2NYYCH5BAAAAAAAIf8LSW1hZ2VNYWdpY2sNZ2FtbWE' + \
49
          b'9MC40NTQ1NQAsAAAAAAIAAQAAAgIMCgA7',
50
    png = b'iVBORw0KGgoAAAANSUhEUgAAAAIAAAABCAYAAAD0In+KAAAAFElEQVQIHQEJAPb' + \
51
          b'/AWNYYP/h4uMAFL0EwlEn99gAAAAASUVORK5CYII=',
52
    jpg = b'/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQE' + \
53
          b'BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQ' + \
54
          b'EBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA' + \
55
          b'QEBAQEBAQEBAQEBAQEBAQH/wAARCAABAAIDAREAAhEBAxEB/8QAFAABAAAAAAAA' + \
56
          b'AAAAAAAAAAAACf/EABoQAAEFAQAAAAAAAAAAAAAAAAYABAU2dbX/xAAVAQEBAAA' + \
57
          b'AAAAAAAAAAAAAAAAFBv/EABkRAAEFAAAAAAAAAAAAAAAAAAEAAjFxsf/aAAwDAQ' + \
58
          b'ACEQMRAD8AA0qs5HvTHQcJdsChioXSbOr/2Q==')
59

60 2
def test_imgshape():
61 2
    for t in [PNG, JPG, GIF]:
62 2
        w,h = t._imgshape(b64decode(twopixel[t.name.lower()]))
63 2
        assert w == 2
64 2
        assert h == 1
65

66 2
def test_load_from_byteio():
67
    """Testing a loading a image from a ByteIo"""
68 2
    memory = BytesIO()
69

70 2
    path = os.path.dirname(__file__)
71 2
    with open(os.path.join(path, '../test_data/logo.png'), 'rb') as image_file:
72 2
        memory.write(image_file.read())
73 2
    memory.seek(0)
74 2
    image_pane = PNG(memory)
75 2
    image_data = image_pane._img()
76 2
    assert b'PNG' in image_data
77

78 2
@pytest.mark.skipif(sys.version_info.major <= 2, reason="Doesn't work with python 2")
79
def test_load_from_stringio():
80
    """Testing a loading a image from a StringIO"""
81 2
    memory = StringIO()
82
    
83 2
    path = os.path.dirname(__file__)
84 2
    with open(os.path.join(path, '../test_data/logo.png'), 'rb') as image_file:
85 2
        memory.write(str(image_file.read()))
86 2
    memory.seek(0)
87 2
    image_pane = PNG(memory)
88 2
    image_data = image_pane._img()
89 2
    assert 'PNG' in image_data
90

91 2
def test_loading_a_image_from_url():
92
    """Tests the loading of a image from a url"""
93 2
    url = 'https://upload.wikimedia.org/wikipedia/commons/7/71/' \
94
          '1700_CE_world_map.PNG'
95

96 2
    image_pane = PNG(url)
97 2
    image_data = image_pane._img()
98 2
    assert b'PNG' in image_data
99

100

101 2
def test_image_alt_text(document, comm):
102
    """Tests the loading of a image from a url"""
103 2
    url = 'https://upload.wikimedia.org/wikipedia/commons/7/71/' \
104
          '1700_CE_world_map.PNG'
105

106 2
    image_pane = PNG(url, embed=False, alt_text="Some alt text")
107 2
    model = image_pane.get_root(document, comm)
108

109 2
    assert 'alt=&quot;Some alt text&quot;' in model.text
110

111

112 2
def test_image_link_url(document, comm):
113
    """Tests the loading of a image from a url"""
114 2
    url = 'https://upload.wikimedia.org/wikipedia/commons/7/71/' \
115
          '1700_CE_world_map.PNG'
116

117 2
    image_pane = PNG(url, embed=False, link_url="http://anaconda.org")
118 2
    model = image_pane.get_root(document, comm)
119

120 2
    assert model.text.startswith('&lt;a href=&quot;http://anaconda.org&quot;')

Read our documentation on viewing source code .

Loading