1 7
from __future__ import absolute_import, division, unicode_literals
2

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

7 7
from base64 import b64decode, b64encode
8

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

13

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

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

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

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

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

46

47 7
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 7
def test_imgshape():
61 7
    for t in [PNG, JPG, GIF]:
62 7
        w,h = t._imgshape(b64decode(twopixel[t.name.lower()]))
63 7
        assert w == 2
64 7
        assert h == 1
65

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

70 7
    path = os.path.dirname(__file__)
71 7
    with open(os.path.join(path, '../test_data/logo.png'), 'rb') as image_file:
72 7
        memory.write(image_file.read())
73

74 7
    image_pane = PNG(memory)
75 7
    image_data = image_pane._img()
76 7
    assert b'PNG' in image_data
77

78 7
@pytest.mark.skipif(sys.version_info.major <= 2, reason="Doesn't work with python 2")
79 2
def test_load_from_stringio():
80
    """Testing a loading a image from a StringIO"""
81 7
    memory = StringIO()
82

83 7
    path = os.path.dirname(__file__)
84 7
    with open(os.path.join(path, '../test_data/logo.png'), 'rb') as image_file:
85 7
        memory.write(str(image_file.read()))
86

87 7
    image_pane = PNG(memory)
88 7
    image_data = image_pane._img()
89 7
    assert 'PNG' in image_data
90

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

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

100

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

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

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

111

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

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

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

Read our documentation on viewing source code .

Loading