/
test_template.py
230 lines (168 loc) · 6.32 KB
/
test_template.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""
These that verify Templates are working correctly.
"""
import json
try:
import holoviews as hv
except Exception:
hv = None
import param
import pytest
from bokeh.document import Document
from bokeh.io.doc import patch_curdoc
from panel.layout import GridSpec, Row
from panel.pane import HoloViews, Markdown
from panel.template import (
BootstrapTemplate, GoldenTemplate, MaterialTemplate, ReactTemplate,
Template, VanillaTemplate,
)
from panel.template.base import BasicTemplate
from panel.widgets import FloatSlider
from .util import hv_available
TEMPLATES = [BootstrapTemplate, GoldenTemplate, MaterialTemplate, ReactTemplate, VanillaTemplate]
LIST_TEMPLATES = [item for item in TEMPLATES if item is not ReactTemplate]
template = """
{% extends base %}
{% block contents %}
{{ embed(roots.A) }}
{{ embed(roots.B) }}
{% endblock %}
"""
@hv_available
def test_template_links_axes(document, comm):
tmplt = Template(template)
p1 = HoloViews(hv.Curve([1, 2, 3]), backend='bokeh')
p2 = HoloViews(hv.Curve([1, 2, 3]), backend='bokeh')
p3 = HoloViews(hv.Curve([1, 2, 3]), backend='bokeh')
row = Row(p2, p3)
tmplt.add_panel('A', p1)
tmplt.add_panel('B', row)
tmplt._init_doc(document, comm, notebook=True)
(_, (m1, _)) = list(p1._models.items())[0]
(_, (m2, _)) = list(p2._models.items())[0]
(_, (m3, _)) = list(p3._models.items())[0]
assert m1.x_range is m2.x_range
assert m1.y_range is m2.y_range
assert m2.x_range is m3.x_range
assert m2.y_range is m3.y_range
def test_template_session_destroy(document, comm):
tmplt = Template(template)
widget = FloatSlider()
row = Row('A', 'B')
tmplt.add_panel('A', widget)
tmplt.add_panel('B', row)
tmplt._init_doc(document, comm, notebook=True)
session_context = param.Parameterized()
session_context._document = document
session_context.id = 'Some ID'
assert len(widget._models) == 2
assert len(row._models) == 2
assert len(row[0]._models) == 2
assert len(row[1]._models) == 2
for cb in document.session_destroyed_callbacks:
cb(session_context)
assert len(widget._models) == 0
assert len(row._models) == 0
assert len(row[0]._models) == 0
assert len(row[1]._models) == 0
list_templates = [
t for t in param.concrete_descendents(BasicTemplate).values()
if not issubclass(t, ReactTemplate)
]
@pytest.mark.parametrize('template', list_templates)
def test_basic_template(template, document, comm):
tmplt = template(title='BasicTemplate', header_background='blue', header_color='red')
tmplt._update_vars()
tvars = tmplt._render_variables
assert tvars['app_title'] == 'BasicTemplate'
assert tvars['header_background'] == 'blue'
assert tvars['header_color'] == 'red'
assert tvars['nav'] == False
assert tvars['busy'] == True
assert tvars['header'] == False
titems = tmplt._render_items
assert titems['busy_indicator'] == (tmplt.busy_indicator, [])
markdown = Markdown('# Some title')
tmplt.main.append(markdown)
assert titems[f'main-{id(markdown)}'] == (markdown, ['main'])
slider = FloatSlider()
tmplt.sidebar.append(slider)
assert titems[f'nav-{id(slider)}'] == (slider, ['nav'])
assert tvars['nav'] == True
tmplt.sidebar[:] = []
assert tvars['nav'] == False
assert f'nav-{id(slider)}' not in titems
subtitle = Markdown('## Some subtitle')
tmplt.header.append(subtitle)
assert titems[f'header-{id(subtitle)}'] == (subtitle, ['header'])
assert tvars['header'] == True
tmplt.header[:] = []
assert f'header-{id(subtitle)}' not in titems
assert tvars['header'] == False
def test_template_server_title():
tmpl = VanillaTemplate(title='Main title')
doc = Document()
with patch_curdoc(doc):
doc = tmpl.server_doc(title='Ignored title')
assert doc.title == 'Main title'
def test_react_template(document, comm):
tmplt = ReactTemplate(title='BasicTemplate', header_background='blue', header_color='red')
tmplt._update_vars()
tvars = tmplt._render_variables
assert tvars['app_title'] == 'BasicTemplate'
assert tvars['header_background'] == 'blue'
assert tvars['header_color'] == 'red'
assert tvars['nav'] == False
assert tvars['busy'] == True
assert tvars['header'] == False
assert tvars['rowHeight'] == tmplt.row_height
assert tvars['breakpoints'] == tmplt.breakpoints
assert tvars['cols'] == tmplt.cols
markdown = Markdown('# Some title')
tmplt.main[:4, :6] = markdown
markdown2 = Markdown('# Some title')
tmplt.main[:4, 6:] = markdown2
layouts = {'lg': [{'h': 4, 'i': '1', 'w': 6, 'x': 0, 'y': 0},
{'h': 4, 'i': '2', 'w': 6, 'x': 6, 'y': 0}],
'md': [{'h': 4, 'i': '1', 'w': 6, 'x': 0, 'y': 0},
{'h': 4, 'i': '2', 'w': 6, 'x': 6, 'y': 0}]
}
for size in layouts:
for layout in layouts[size]:
layout.update({'minW': 0, 'minH': 0})
assert json.loads(tvars['layouts']) == layouts
@pytest.mark.parametrize(["template_class"], [(t,) for t in LIST_TEMPLATES])
def test_list_template_insert_order(template_class):
template = template_class()
template.main.append(1)
template.main.insert(0, 0)
template.main.extend([2, 3])
objs = list(template._render_items.values())[4:]
((obj1, tag1), (obj2, tag2), (obj3, tag3), (obj4, tag4)) = objs
assert tag1 == tag2 == tag3 == tag4 == ['main']
assert obj1.object == 0
assert obj2.object == 1
assert obj3.object == 2
assert obj4.object == 3
@pytest.mark.parametrize(["template_class"], [(item,) for item in TEMPLATES])
def test_constructor(template_class):
item = Markdown("Hello World")
items = [item]
template_class(header=item, sidebar=item, main=item)
template_class(header=items, sidebar=items, main=items)
def test_constructor_grid_spec():
item = Markdown("Hello World")
grid = GridSpec(ncols=12)
grid[0:2, 3:4]=item
ReactTemplate(main=grid)
def test_grid_template_override():
item = Markdown("First")
override = Markdown("Second")
template = ReactTemplate()
template.main[0, 0] = item
template.main[0, 0] = override
objs = list(template._render_items.values())[4:]
assert len(objs) == 1
((obj, tags),) = objs
assert obj.object == "Second"
assert tags == ['main']