1 2
import pytest
2

3 2
from bokeh.models import Div
4

5 2
from panel.depends import depends
6 2
from panel.layout import GridBox, GridSpec, Spacer
7 2
from panel.widgets import IntSlider
8

9

10 2
def test_gridspec_cleanup(document, comm):
11 2
    spacer = Spacer()
12 2
    gspec = GridSpec()
13 2
    gspec[0, 0] = spacer
14

15 2
    model = gspec.get_root(document, comm)
16

17 2
    ref = model.ref['id']
18 2
    assert ref in gspec._models
19 2
    assert ref in spacer._models
20

21 2
    gspec._cleanup(model)
22 2
    assert ref not in gspec._models
23 2
    assert ref not in spacer._models
24

25

26 2
def test_gridspec_integer_setitem():
27 2
    div = Div()
28 2
    gspec = GridSpec()
29 2
    gspec[0, 0] = div
30

31 2
    assert list(gspec.objects) == [(0, 0, 1, 1)]
32

33

34 2
def test_gridspec_clone():
35 2
    div = Div()
36 2
    gspec = GridSpec()
37 2
    gspec[0, 0] = div
38 2
    clone = gspec.clone()
39

40 2
    assert gspec.objects == clone.objects
41 2
    assert gspec.param.get_param_values() == clone.param.get_param_values()
42

43

44 2
def test_gridspec_slice_setitem():
45 2
    div = Div()
46 2
    gspec = GridSpec()
47 2
    gspec[0, :] = div
48

49 2
    assert list(gspec.objects) == [(0, None, 1, None)]
50

51

52 2
def test_gridspec_setitem_int_overlap():
53 2
    div = Div()
54 2
    gspec = GridSpec(mode='error')
55 2
    gspec[0, 0] = div
56 2
    with pytest.raises(IndexError):
57 2
        gspec[0, 0] = 'String'
58

59

60 2
def test_gridspec_setitem_slice_overlap():
61 2
    div = Div()
62 2
    gspec = GridSpec(mode='error')
63 2
    gspec[0, :] = div
64 2
    with pytest.raises(IndexError):
65 2
        gspec[0, 1] = div
66

67

68 2
def test_gridspec_setitem_cell_override():
69 2
    div = Div()
70 2
    div2 = Div()
71 2
    gspec = GridSpec()
72 2
    gspec[0, 0] = div
73 2
    gspec[0, 0] = div2
74 2
    assert (0, 0, 1, 1) in gspec.objects
75 2
    assert gspec.objects[(0, 0, 1, 1)].object is div2
76

77

78 2
def test_gridspec_setitem_span_override():
79 2
    div = Div()
80 2
    div2 = Div()
81 2
    gspec = GridSpec()
82 2
    gspec[0, :] = div
83 2
    gspec[0, 0] = div2
84 2
    assert (0, 0, 1, 1) in gspec.objects
85 2
    assert gspec.objects[(0, 0, 1, 1)].object is div2
86

87

88 2
def test_gridspec_fixed_with_int_setitem(document, comm):
89 2
    div1 = Div()
90 2
    div2 = Div()
91 2
    gspec = GridSpec(width=800, height=500)
92

93 2
    gspec[0, 0] = div1
94 2
    gspec[1, 1] = div2
95

96 2
    model = gspec.get_root(document, comm=comm)
97 2
    assert model.children == [(div1, 0, 0, 1, 1), (div2, 1, 1, 1, 1)]
98 2
    assert div1.width == 400
99 2
    assert div1.height == 250
100 2
    assert div2.width == 400
101 2
    assert div2.height == 250
102

103

104 2
def test_gridspec_fixed_with_slice_setitem(document, comm):
105 2
    div1 = Div()
106 2
    div2 = Div()
107 2
    gspec = GridSpec(width=900, height=500)
108

109 2
    gspec[0, 0:2] = div1
110 2
    gspec[1, 2] = div2
111

112 2
    model = gspec.get_root(document, comm=comm)
113 2
    assert model.children == [(div1, 0, 0, 1, 2), (div2, 1, 2, 1, 1)]
114 2
    assert div1.width == 600
115 2
    assert div1.height == 250
116 2
    assert div2.width == 300
117 2
    assert div2.height == 250
118

119

120 2
def test_gridspec_fixed_with_upper_partial_slice_setitem(document, comm):
121 2
    div1 = Div()
122 2
    div2 = Div()
123 2
    gspec = GridSpec(width=900, height=500)
124

125 2
    gspec[0, :2] = div1
126 2
    gspec[1, 2] = div2
127

128 2
    model = gspec.get_root(document, comm=comm)
129 2
    assert model.children == [(div1, 0, 0, 1, 2), (div2, 1, 2, 1, 1)]
130 2
    assert div1.width == 600
131 2
    assert div1.height == 250
132 2
    assert div2.width == 300
133 2
    assert div2.height == 250
134

135

136 2
def test_gridspec_fixed_with_lower_partial_slice_setitem(document, comm):
137 2
    div1 = Div()
138 2
    div2 = Div()
139 2
    gspec = GridSpec(width=900, height=500)
140

141 2
    gspec[0, 1:] = div1
142 2
    gspec[1, 2] = div2
143

144 2
    model = gspec.get_root(document, comm=comm)
145 2
    assert model.children == [(div1, 0, 1, 1, 2), (div2, 1, 2, 1, 1)]
146 2
    assert div1.width == 600
147 2
    assert div1.height == 250
148 2
    assert div2.width == 300
149 2
    assert div2.height == 250
150

151

152 2
def test_gridspec_fixed_with_empty_slice_setitem(document, comm):
153 2
    div1 = Div()
154 2
    div2 = Div()
155 2
    gspec = GridSpec(width=900, height=500)
156

157 2
    gspec[0, :] = div1
158 2
    gspec[1, 2] = div2
159

160 2
    model = gspec.get_root(document, comm=comm)
161 2
    assert model.children == [(div1, 0, 0, 1, 3), (div2, 1, 2, 1, 1)]
162 2
    assert div1.width == 900
163 2
    assert div1.height == 250
164 2
    assert div2.width == 300
165 2
    assert div2.height == 250
166

167

168 2
def test_gridspec_stretch_with_int_setitem(document, comm):
169 2
    div1 = Div()
170 2
    div2 = Div()
171 2
    gspec = GridSpec(sizing_mode='stretch_both')
172

173 2
    gspec[0, 0] = div1
174 2
    gspec[1, 1] = div2
175

176 2
    model = gspec.get_root(document, comm=comm)
177 2
    assert model.children == [(div1, 0, 0, 1, 1), (div2, 1, 1, 1, 1)]
178 2
    assert div1.sizing_mode == 'stretch_both'
179 2
    assert div2.sizing_mode == 'stretch_both'
180

181

182 2
def test_gridspec_stretch_with_slice_setitem(document, comm):
183 2
    div1 = Div()
184 2
    div2 = Div()
185 2
    gspec = GridSpec(sizing_mode='stretch_both')
186

187 2
    gspec[0, 0:2] = div1
188 2
    gspec[1, 2] = div2
189

190 2
    model = gspec.get_root(document, comm=comm)
191 2
    assert model.children == [(div1, 0, 0, 1, 2), (div2, 1, 2, 1, 1)]
192 2
    assert div1.sizing_mode == 'stretch_both'
193 2
    assert div2.sizing_mode == 'stretch_both'
194

195

196 2
def test_gridspec_fixed_with_replacement_pane(document, comm):
197 2
    slider = IntSlider(start=0, end=2)
198

199 2
    @depends(slider)
200
    def div(value):
201 2
        return Div(text=str(value))
202

203 2
    gspec = GridSpec()
204

205 2
    gspec[0, 0:2] = Div()
206 2
    gspec[1, 2] = div
207

208 2
    model = gspec.get_root(document, comm=comm)
209 2
    ((div1, _, _, _, _), (row, _, _, _, _)) = model.children
210 2
    div2 = row.children[0]
211 2
    assert div1.width == 400
212 2
    assert div1.height == 300
213 2
    assert div2.width == 200
214 2
    assert div2.height == 300
215

216 2
    slider.value = 1
217 2
    assert row.children[0] is not div2
218 2
    assert row.children[0].width == 200
219 2
    assert row.children[0].height == 300
220

221

222 2
def test_gridspec_stretch_with_replacement_pane(document, comm):
223 2
    slider = IntSlider(start=0, end=2)
224

225 2
    @depends(slider)
226
    def div(value):
227 2
        return Div(text=str(value))
228

229 2
    gspec = GridSpec(sizing_mode='stretch_width')
230

231 2
    gspec[0, 0:2] = Div()
232 2
    gspec[1, 2] = div
233

234 2
    model = gspec.get_root(document, comm=comm)
235 2
    ((div1, _, _, _, _), (row, _, _, _, _)) = model.children
236 2
    div2 = row.children[0]
237 2
    assert div1.sizing_mode == 'stretch_width'
238 2
    assert div1.height == 300
239 2
    assert div2.sizing_mode == 'stretch_width'
240 2
    assert div2.height == 300
241

242 2
    slider.value = 1
243 2
    assert row.children[0] is not div2
244 2
    assert row.children[0].sizing_mode == 'stretch_width'
245 2
    assert row.children[0].height == 300
246
    
247

248

249 2
def test_gridbox_ncols(document, comm):
250 2
    grid_box = GridBox(Div(), Div(), Div(), Div(), Div(), Div(), Div(), Div(), ncols=3)
251

252 2
    model = grid_box.get_root(document, comm=comm)
253

254 2
    assert len(model.children) == 8
255 2
    coords = [
256
        (0, 0, 1, 1), (0, 1, 1, 1), (0, 2, 1, 1),
257
        (1, 0, 1, 1), (1, 1, 1, 1), (1, 2, 1, 1),
258
        (2, 0, 1, 1), (2, 1, 1, 1)
259
    ]
260 2
    for child, coord in zip(model.children, coords):
261 2
        assert child[1:] == coord
262

263

264 2
def test_gridbox_nrows(document, comm):
265 2
    grid_box = GridBox(Div(), Div(), Div(), Div(), Div(), Div(), Div(), Div(), nrows=2)
266

267 2
    model = grid_box.get_root(document, comm=comm)
268

269 2
    assert len(model.children) == 8
270

271 2
    coords = [
272
        (0, 0, 1, 1), (0, 1, 1, 1), (0, 2, 1, 1), (0, 3, 1, 1),
273
        (1, 0, 1, 1), (1, 1, 1, 1), (1, 2, 1, 1), (1, 3, 1, 1)
274
    ]
275 2
    for child, coord in zip(model.children, coords):
276 2
        assert child[1:] == coord

Read our documentation on viewing source code .

Loading