1
"""
2
These that verify Templates are working correctly.
3
"""
4 2
from __future__ import absolute_import, division, unicode_literals
5

6 2
from distutils.version import LooseVersion
7

8 2
try:
9 2
    import holoviews as hv
10 0
except Exception:
11 0
    hv = None
12

13 2
import param
14 2
import pytest
15

16 2
latest_param = pytest.mark.skipif(LooseVersion(param.__version__) < '1.10.0a4',
17
                                  reason="requires param>=1.10.0a4")
18

19 2
from panel.layout import Row
20 2
from panel.pane import HoloViews, Markdown
21 2
from panel.template import Template
22 2
from panel.template.base import BasicTemplate
23 2
from panel.widgets import FloatSlider
24

25 2
from .util import hv_available
26

27 2
template = """
28
{% extends base %}
29

30
{% block contents %}
31
{{ embed(roots.A) }}
32
{{ embed(roots.B) }}
33
{% endblock %}
34
"""
35

36

37 2
@hv_available
38
def test_template_links_axes(document, comm):
39 2
    tmplt = Template(template)
40

41 2
    p1 = HoloViews(hv.Curve([1, 2, 3]))
42 2
    p2 = HoloViews(hv.Curve([1, 2, 3]))
43 2
    p3 = HoloViews(hv.Curve([1, 2, 3]))
44 2
    row = Row(p2, p3)
45

46 2
    tmplt.add_panel('A', p1)
47 2
    tmplt.add_panel('B', row)
48

49 2
    tmplt._init_doc(document, comm, notebook=True)
50

51 2
    (_, (m1, _)) = list(p1._models.items())[0]
52 2
    (_, (m2, _)) = list(p2._models.items())[0]
53 2
    (_, (m3, _)) = list(p3._models.items())[0]
54 2
    assert m1.x_range is m2.x_range
55 2
    assert m1.y_range is m2.y_range
56 2
    assert m2.x_range is m3.x_range
57 2
    assert m2.y_range is m3.y_range
58

59

60 2
def test_template_session_destroy(document, comm):
61 2
    tmplt = Template(template)
62

63 2
    widget = FloatSlider()
64 2
    row = Row('A', 'B')
65
    
66 2
    tmplt.add_panel('A', widget)
67 2
    tmplt.add_panel('B', row)
68

69 2
    tmplt._init_doc(document, comm, notebook=True)
70 2
    session_context = param.Parameterized()
71 2
    session_context._document = document
72

73 2
    assert len(widget._models) == 2
74 2
    assert len(row._models) == 2
75 2
    assert len(row[0]._models) == 2
76 2
    assert len(row[1]._models) == 2
77

78 2
    for cb in document.session_destroyed_callbacks:
79 2
        cb(session_context)
80

81 2
    assert len(widget._models) == 0
82 2
    assert len(row._models) == 0
83 2
    assert len(row[0]._models) == 0
84 2
    assert len(row[1]._models) == 0
85

86

87 2
@latest_param
88 2
@pytest.mark.parametrize('template', list(param.concrete_descendents(BasicTemplate).values()))
89
def test_basic_template(template, document, comm):
90 2
    tmplt = template(title='BasicTemplate', header_background='blue', header_color='red')
91

92 2
    tvars = tmplt._render_variables
93

94 2
    assert tvars['app_title'] == 'BasicTemplate'
95 2
    assert tvars['header_background'] == 'blue'
96 2
    assert tvars['header_color'] == 'red'
97 2
    assert tvars['nav'] == False
98 2
    assert tvars['busy'] == True
99 2
    assert tvars['header'] == False
100

101 2
    titems = tmplt._render_items
102

103 2
    assert titems['busy_indicator'] == (tmplt.busy_indicator, [])
104

105 2
    markdown = Markdown('# Some title')
106 2
    tmplt.main.append(markdown)
107

108 2
    assert titems[str(id(markdown))] == (markdown, ['main'])
109

110 2
    slider = FloatSlider()
111 2
    tmplt.sidebar.append(slider)
112

113 2
    assert titems[str(id(slider))] == (slider, ['nav'])
114 2
    assert tvars['nav'] == True
115
    
116 2
    tmplt.sidebar[:] = []
117 2
    assert tvars['nav'] == False
118 2
    assert str(id(slider)) not in titems
119

120 2
    subtitle = Markdown('## Some subtitle')
121 2
    tmplt.header.append(subtitle)
122

123 2
    assert titems[str(id(subtitle))] == (subtitle, ['header'])
124 2
    assert tvars['header'] == True
125

126 2
    tmplt.header[:] = []
127 2
    assert str(id(subtitle)) not in titems
128 2
    assert tvars['header'] == False
129

130
    

Read our documentation on viewing source code .

Loading