holoviz / panel
1 7
import os
2 7
import sys
3 7
import pytest
4

5 7
from base64 import b64decode, b64encode
6

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

11

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

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

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

37 7
    pane.encode = False
38 7
    assert model.text == escape(circle)
39

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

44

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

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

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

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

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

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

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

85 7
    image_pane = PNG(memory)
86 7
    image_data = image_pane._img()
87 7
    assert 'PNG' in image_data
88

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

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

98

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

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

107 7
    assert 'alt=&quot;Some alt text&quot;' in model.text
108

109

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

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

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

Read our documentation on viewing source code .

Loading