1
"""
2
These that verify Templates are working correctly.
3
"""
4 7
from distutils.version import LooseVersion
5 7
from panel.layout.grid import GridSpec
6

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

12 7
import param
13 7
import pytest
14

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

18 7
from panel.layout import Row
19 7
from panel.pane import HoloViews, Markdown
20 7
from panel.template import (
21
    BootstrapTemplate, GoldenTemplate, MaterialTemplate, ReactTemplate,
22
    Template, VanillaTemplate
23
)
24 7
from panel.template.base import BasicTemplate
25 7
from panel.widgets import FloatSlider
26

27 7
from .util import hv_available
28

29 7
TEMPLATES = [BootstrapTemplate, GoldenTemplate, MaterialTemplate, ReactTemplate, VanillaTemplate]
30 7
LIST_TEMPLATES = [item for item in TEMPLATES if item is not ReactTemplate]
31

32 7
template = """
33
{% extends base %}
34

35
{% block contents %}
36
{{ embed(roots.A) }}
37
{{ embed(roots.B) }}
38
{% endblock %}
39
"""
40

41

42 7
@hv_available
43 2
def test_template_links_axes(document, comm):
44 7
    tmplt = Template(template)
45

46 7
    p1 = HoloViews(hv.Curve([1, 2, 3]))
47 7
    p2 = HoloViews(hv.Curve([1, 2, 3]))
48 7
    p3 = HoloViews(hv.Curve([1, 2, 3]))
49 7
    row = Row(p2, p3)
50

51 7
    tmplt.add_panel('A', p1)
52 7
    tmplt.add_panel('B', row)
53

54 7
    tmplt._init_doc(document, comm, notebook=True)
55

56 7
    (_, (m1, _)) = list(p1._models.items())[0]
57 7
    (_, (m2, _)) = list(p2._models.items())[0]
58 7
    (_, (m3, _)) = list(p3._models.items())[0]
59 7
    assert m1.x_range is m2.x_range
60 7
    assert m1.y_range is m2.y_range
61 7
    assert m2.x_range is m3.x_range
62 7
    assert m2.y_range is m3.y_range
63

64

65 7
def test_template_session_destroy(document, comm):
66 7
    tmplt = Template(template)
67

68 7
    widget = FloatSlider()
69 7
    row = Row('A', 'B')
70

71 7
    tmplt.add_panel('A', widget)
72 7
    tmplt.add_panel('B', row)
73

74 7
    tmplt._init_doc(document, comm, notebook=True)
75 7
    session_context = param.Parameterized()
76 7
    session_context._document = document
77 7
    session_context.id = 'Some ID'
78

79 7
    assert len(widget._models) == 2
80 7
    assert len(row._models) == 2
81 7
    assert len(row[0]._models) == 2
82 7
    assert len(row[1]._models) == 2
83

84 7
    for cb in document.session_destroyed_callbacks:
85 7
        cb(session_context)
86

87 7
    assert len(widget._models) == 0
88 7
    assert len(row._models) == 0
89 7
    assert len(row[0]._models) == 0
90 7
    assert len(row[1]._models) == 0
91

92

93 7
list_templates = [
94
    t for t in param.concrete_descendents(BasicTemplate).values()
95
    if not issubclass(t, ReactTemplate)
96
]
97

98 7
@latest_param
99 7
@pytest.mark.parametrize('template', list_templates)
100 2
def test_basic_template(template, document, comm):
101 7
    tmplt = template(title='BasicTemplate', header_background='blue', header_color='red')
102

103 7
    tmplt._update_vars()
104 7
    tvars = tmplt._render_variables
105

106 7
    assert tvars['app_title'] == 'BasicTemplate'
107 7
    assert tvars['header_background'] == 'blue'
108 7
    assert tvars['header_color'] == 'red'
109 7
    assert tvars['nav'] == False
110 7
    assert tvars['busy'] == True
111 7
    assert tvars['header'] == False
112

113 7
    titems = tmplt._render_items
114

115 7
    assert titems['busy_indicator'] == (tmplt.busy_indicator, [])
116

117 7
    markdown = Markdown('# Some title')
118 7
    tmplt.main.append(markdown)
119

120 7
    assert titems[str(id(markdown))] == (markdown, ['main'])
121

122 7
    slider = FloatSlider()
123 7
    tmplt.sidebar.append(slider)
124

125 7
    assert titems[str(id(slider))] == (slider, ['nav'])
126 7
    assert tvars['nav'] == True
127

128 7
    tmplt.sidebar[:] = []
129 7
    assert tvars['nav'] == False
130 7
    assert str(id(slider)) not in titems
131

132 7
    subtitle = Markdown('## Some subtitle')
133 7
    tmplt.header.append(subtitle)
134

135 7
    assert titems[str(id(subtitle))] == (subtitle, ['header'])
136 7
    assert tvars['header'] == True
137

138 7
    tmplt.header[:] = []
139 7
    assert str(id(subtitle)) not in titems
140 7
    assert tvars['header'] == False
141

142

143 7
def test_react_template(document, comm):
144 7
    tmplt = ReactTemplate(title='BasicTemplate', header_background='blue', header_color='red')
145

146 7
    tmplt._update_vars()
147 7
    tvars = tmplt._render_variables
148

149 7
    assert tvars['app_title'] == 'BasicTemplate'
150 7
    assert tvars['header_background'] == 'blue'
151 7
    assert tvars['header_color'] == 'red'
152 7
    assert tvars['nav'] == False
153 7
    assert tvars['busy'] == True
154 7
    assert tvars['header'] == False
155 7
    assert tvars['rowHeight'] == tmplt.row_height
156 7
    assert tvars['breakpoints'] == tmplt.breakpoints
157 7
    assert tvars['cols'] == tmplt.cols
158

159 7
    markdown = Markdown('# Some title')
160 7
    tmplt.main[:4, :6] = markdown
161

162 7
    markdown2 = Markdown('# Some title')
163 7
    tmplt.main[:4, 6:] = markdown2
164

165 7
    assert tvars['layouts'] == {
166
        'lg': [{'h': 4, 'i': '1', 'w': 6, 'x': 0, 'y': 0},
167
               {'h': 4, 'i': '2', 'w': 6, 'x': 6, 'y': 0}],
168
        'md': [{'h': 4, 'i': '1', 'w': 6, 'x': 0, 'y': 0},
169
               {'h': 4, 'i': '2', 'w': 6, 'x': 6, 'y': 0}]
170
    }
171

172 7
@pytest.mark.parametrize(["template_class"], [(t,) for t in LIST_TEMPLATES])
173 2
def test_list_template_insert_order(template_class):
174 7
    template = template_class()
175

176 7
    template.main.append(1)
177

178 7
    template.main.insert(0, 0)
179

180 7
    template.main.extend([2, 3])
181

182 7
    objs = list(template._render_items.values())[2:]
183 7
    ((obj1, tag1), (obj2, tag2), (obj3, tag3), (obj4, tag4)) = objs
184

185 7
    assert tag1 == tag2 == tag3 == tag4 == ['main']
186 7
    assert obj1.object == 0
187 7
    assert obj2.object == 1
188 7
    assert obj3.object == 2
189 7
    assert obj4.object == 3
190

191 7
@pytest.mark.parametrize(["template_class"], [(item,) for item in TEMPLATES])
192 2
def test_constructor(template_class):
193 7
    item = Markdown("Hello World")
194 7
    items = [item]
195 7
    template_class(header=item, sidebar=item, main=item)
196 7
    template_class(header=items, sidebar=items, main=items)
197

198 7
def test_constructor_grid_spec():
199 7
    item = Markdown("Hello World")
200 7
    grid = GridSpec(ncols=12)
201 7
    grid[0:2, 3:4]=item
202 7
    ReactTemplate(main=grid)
203

204 7
def test_grid_template_override():
205 7
    item = Markdown("First")
206 7
    override = Markdown("Second")
207 7
    template = ReactTemplate()
208 7
    template.main[0, 0] = item
209 7
    template.main[0, 0] = override
210

211 7
    objs = list(template._render_items.values())[2:]
212 7
    assert len(objs) == 1
213 7
    ((obj, tags),) = objs
214

215 7
    assert obj.object == "Second"
216 7
    assert tags == ['main']

Read our documentation on viewing source code .

Loading