forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_html.py
453 lines (400 loc) · 11.8 KB
/
test_html.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
from textwrap import dedent
import numpy as np
import pytest
from pandas import (
DataFrame,
MultiIndex,
option_context,
)
jinja2 = pytest.importorskip("jinja2")
from pandas.io.formats.style import Styler
loader = jinja2.PackageLoader("pandas", "io/formats/templates")
env = jinja2.Environment(loader=loader, trim_blocks=True)
@pytest.fixture
def styler():
return Styler(DataFrame([[2.61], [2.69]], index=["a", "b"], columns=["A"]))
@pytest.fixture
def styler_mi():
midx = MultiIndex.from_product([["a", "b"], ["c", "d"]])
return Styler(DataFrame(np.arange(16).reshape(4, 4), index=midx, columns=midx))
@pytest.fixture
def tpl_style():
return env.get_template("html_style.tpl")
@pytest.fixture
def tpl_table():
return env.get_template("html_table.tpl")
def test_html_template_extends_options():
# make sure if templates are edited tests are updated as are setup fixtures
# to understand the dependency
with open("pandas/io/formats/templates/html.tpl") as file:
result = file.read()
assert "{% include html_style_tpl %}" in result
assert "{% include html_table_tpl %}" in result
def test_exclude_styles(styler):
result = styler.to_html(exclude_styles=True, doctype_html=True)
expected = dedent(
"""\
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<table>
<thead>
<tr>
<th > </th>
<th >A</th>
</tr>
</thead>
<tbody>
<tr>
<th >a</th>
<td >2.610000</td>
</tr>
<tr>
<th >b</th>
<td >2.690000</td>
</tr>
</tbody>
</table>
</body>
</html>
"""
)
assert result == expected
def test_w3_html_format(styler):
styler.set_uuid("").set_table_styles(
[{"selector": "th", "props": "att2:v2;"}]
).applymap(lambda x: "att1:v1;").set_table_attributes(
'class="my-cls1" style="attr3:v3;"'
).set_td_classes(
DataFrame(["my-cls2"], index=["a"], columns=["A"])
).format(
"{:.1f}"
).set_caption(
"A comprehensive test"
)
expected = dedent(
"""\
<style type="text/css">
#T_ th {
att2: v2;
}
#T_row0_col0, #T_row1_col0 {
att1: v1;
}
</style>
<table id="T_" class="my-cls1" style="attr3:v3;">
<caption>A comprehensive test</caption>
<thead>
<tr>
<th class="blank level0" > </th>
<th class="col_heading level0 col0" >A</th>
</tr>
</thead>
<tbody>
<tr>
<th id="T_level0_row0" class="row_heading level0 row0" >a</th>
<td id="T_row0_col0" class="data row0 col0 my-cls2" >2.6</td>
</tr>
<tr>
<th id="T_level0_row1" class="row_heading level0 row1" >b</th>
<td id="T_row1_col0" class="data row1 col0" >2.7</td>
</tr>
</tbody>
</table>
"""
)
assert expected == styler.render()
def test_colspan_w3():
# GH 36223
df = DataFrame(data=[[1, 2]], columns=[["l0", "l0"], ["l1a", "l1b"]])
styler = Styler(df, uuid="_", cell_ids=False)
assert '<th class="col_heading level0 col0" colspan="2">l0</th>' in styler.render()
def test_rowspan_w3():
# GH 38533
df = DataFrame(data=[[1, 2]], index=[["l0", "l0"], ["l1a", "l1b"]])
styler = Styler(df, uuid="_", cell_ids=False)
assert (
'<th id="T___level0_row0" class="row_heading '
'level0 row0" rowspan="2">l0</th>' in styler.render()
)
def test_styles(styler):
styler.set_uuid("abc_")
styler.set_table_styles([{"selector": "td", "props": "color: red;"}])
result = styler.to_html(doctype_html=True)
expected = dedent(
"""\
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style type="text/css">
#T_abc_ td {
color: red;
}
</style>
</head>
<body>
<table id="T_abc_">
<thead>
<tr>
<th class="blank level0" > </th>
<th class="col_heading level0 col0" >A</th>
</tr>
</thead>
<tbody>
<tr>
<th id="T_abc_level0_row0" class="row_heading level0 row0" >a</th>
<td id="T_abc_row0_col0" class="data row0 col0" >2.610000</td>
</tr>
<tr>
<th id="T_abc_level0_row1" class="row_heading level0 row1" >b</th>
<td id="T_abc_row1_col0" class="data row1 col0" >2.690000</td>
</tr>
</tbody>
</table>
</body>
</html>
"""
)
assert result == expected
def test_doctype(styler):
result = styler.to_html(doctype_html=False)
assert "<html>" not in result
assert "<body>" not in result
assert "<!DOCTYPE html>" not in result
assert "<head>" not in result
def test_block_names(tpl_style, tpl_table):
# catch accidental removal of a block
expected_style = {
"before_style",
"style",
"table_styles",
"before_cellstyle",
"cellstyle",
}
expected_table = {
"before_table",
"table",
"caption",
"thead",
"tbody",
"after_table",
"before_head_rows",
"head_tr",
"after_head_rows",
"before_rows",
"tr",
"after_rows",
}
result1 = set(tpl_style.blocks)
assert result1 == expected_style
result2 = set(tpl_table.blocks)
assert result2 == expected_table
def test_from_custom_template_table(tmpdir):
p = tmpdir.mkdir("tpl").join("myhtml_table.tpl")
p.write(
dedent(
"""\
{% extends "html_table.tpl" %}
{% block table %}
<h1>{{custom_title}}</h1>
{{ super() }}
{% endblock table %}"""
)
)
result = Styler.from_custom_template(str(tmpdir.join("tpl")), "myhtml_table.tpl")
assert issubclass(result, Styler)
assert result.env is not Styler.env
assert result.template_html_table is not Styler.template_html_table
styler = result(DataFrame({"A": [1, 2]}))
assert "<h1>My Title</h1>\n\n\n<table" in styler.render(custom_title="My Title")
def test_from_custom_template_style(tmpdir):
p = tmpdir.mkdir("tpl").join("myhtml_style.tpl")
p.write(
dedent(
"""\
{% extends "html_style.tpl" %}
{% block style %}
<link rel="stylesheet" href="mystyle.css">
{{ super() }}
{% endblock style %}"""
)
)
result = Styler.from_custom_template(
str(tmpdir.join("tpl")), html_style="myhtml_style.tpl"
)
assert issubclass(result, Styler)
assert result.env is not Styler.env
assert result.template_html_style is not Styler.template_html_style
styler = result(DataFrame({"A": [1, 2]}))
assert '<link rel="stylesheet" href="mystyle.css">\n\n<style' in styler.render()
def test_caption_as_sequence(styler):
styler.set_caption(("full cap", "short cap"))
assert "<caption>full cap</caption>" in styler.render()
@pytest.mark.parametrize("index", [False, True])
@pytest.mark.parametrize("columns", [False, True])
@pytest.mark.parametrize("index_name", [True, False])
def test_sticky_basic(styler, index, columns, index_name):
if index_name:
styler.index.name = "some text"
if index:
styler.set_sticky(axis=0)
if columns:
styler.set_sticky(axis=1)
res = styler.set_uuid("").to_html()
css_for_index = (
"tr th:first-child {\n position: sticky;\n background-color: white;\n "
"left: 0px;\n z-index: 1;\n}"
)
assert (css_for_index in res) is index
css_for_cols_1 = (
"thead tr:first-child {\n position: sticky;\n background-color: white;\n "
"top: 0px;\n z-index: 2;\n"
)
css_for_cols_1 += " height: 25px;\n}" if index_name else "}"
assert (css_for_cols_1 in res) is columns
css_for_cols_2 = (
"thead tr:nth-child(2) {\n position: sticky;\n background-color: white;\n "
"top: 25px;\n z-index: 2;\n height: 25px;\n}"
)
assert (css_for_cols_2 in res) is (index_name and columns)
@pytest.mark.parametrize("index", [False, True])
@pytest.mark.parametrize("columns", [False, True])
def test_sticky_mi(styler_mi, index, columns):
if index:
styler_mi.set_sticky(axis=0)
if columns:
styler_mi.set_sticky(axis=1)
res = styler_mi.set_uuid("").to_html()
assert (
(
dedent(
"""\
#T_ tbody th.level0 {
position: sticky;
left: 0px;
min-width: 75px;
max-width: 75px;
background-color: white;
}
"""
)
in res
)
is index
)
assert (
(
dedent(
"""\
#T_ tbody th.level1 {
position: sticky;
left: 75px;
min-width: 75px;
max-width: 75px;
background-color: white;
}
"""
)
in res
)
is index
)
assert (
(
dedent(
"""\
#T_ thead th.level0 {
position: sticky;
top: 0px;
height: 25px;
background-color: white;
}
"""
)
in res
)
is columns
)
assert (
(
dedent(
"""\
#T_ thead th.level1 {
position: sticky;
top: 25px;
height: 25px;
background-color: white;
}
"""
)
in res
)
is columns
)
@pytest.mark.parametrize("index", [False, True])
@pytest.mark.parametrize("columns", [False, True])
def test_sticky_levels(styler_mi, index, columns):
if index:
styler_mi.set_sticky(axis=0, levels=[1])
if columns:
styler_mi.set_sticky(axis=1, levels=[1])
res = styler_mi.set_uuid("").to_html()
assert "#T_ tbody th.level0 {" not in res
assert "#T_ thead th.level0 {" not in res
assert (
(
dedent(
"""\
#T_ tbody th.level1 {
position: sticky;
left: 0px;
min-width: 75px;
max-width: 75px;
background-color: white;
}
"""
)
in res
)
is index
)
assert (
(
dedent(
"""\
#T_ thead th.level1 {
position: sticky;
top: 0px;
height: 25px;
background-color: white;
}
"""
)
in res
)
is columns
)
def test_sticky_raises(styler):
with pytest.raises(ValueError, match="`axis` must be"):
styler.set_sticky(axis="bad")
@pytest.mark.parametrize(
"sparse_index, sparse_columns",
[(True, True), (True, False), (False, True), (False, False)],
)
def test_sparse_options(sparse_index, sparse_columns):
cidx = MultiIndex.from_tuples([("Z", "a"), ("Z", "b"), ("Y", "c")])
ridx = MultiIndex.from_tuples([("A", "a"), ("A", "b"), ("B", "c")])
df = DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=ridx, columns=cidx)
styler = df.style
default_html = styler.to_html() # defaults under pd.options to (True , True)
with option_context(
"styler.sparse.index", sparse_index, "styler.sparse.columns", sparse_columns
):
html1 = styler.to_html()
assert (html1 == default_html) is (sparse_index and sparse_columns)
html2 = styler.to_html(sparse_index=sparse_index, sparse_columns=sparse_columns)
assert html1 == html2