1
1
from textwrap import dedent
2
2
3
+ import numpy as np
3
4
import pytest
4
5
5
- from pandas import DataFrame
6
+ from pandas import (
7
+ DataFrame ,
8
+ MultiIndex ,
9
+ )
6
10
7
11
jinja2 = pytest .importorskip ("jinja2" )
8
12
from pandas .io .formats .style import Styler
@@ -16,6 +20,12 @@ def styler():
16
20
return Styler (DataFrame ([[2.61 ], [2.69 ]], index = ["a" , "b" ], columns = ["A" ]))
17
21
18
22
23
+ @pytest .fixture
24
+ def styler_mi ():
25
+ midx = MultiIndex .from_product ([["a" , "b" ], ["c" , "d" ]])
26
+ return Styler (DataFrame (np .arange (16 ).reshape (4 , 4 ), index = midx , columns = midx ))
27
+
28
+
19
29
@pytest .fixture
20
30
def tpl_style ():
21
31
return env .get_template ("html_style.tpl" )
@@ -236,3 +246,146 @@ def test_from_custom_template(tmpdir):
236
246
def test_caption_as_sequence (styler ):
237
247
styler .set_caption (("full cap" , "short cap" ))
238
248
assert "<caption>full cap</caption>" in styler .render ()
249
+
250
+
251
+ @pytest .mark .parametrize ("index" , [False , True ])
252
+ @pytest .mark .parametrize ("columns" , [False , True ])
253
+ def test_sticky_basic (styler , index , columns ):
254
+ if index :
255
+ styler .set_sticky (axis = 0 )
256
+ if columns :
257
+ styler .set_sticky (axis = 1 )
258
+
259
+ res = styler .set_uuid ("" ).to_html ()
260
+ cs1 = "tbody th {\n position: sticky;\n left: 0px;\n background-color: white;\n }"
261
+ assert (cs1 in res ) is index
262
+ cs2 = "thead th {\n position: sticky;\n top: 0px;\n background-color: white;\n }"
263
+ assert (cs2 in res ) is columns
264
+
265
+
266
+ @pytest .mark .parametrize ("index" , [False , True ])
267
+ @pytest .mark .parametrize ("columns" , [False , True ])
268
+ def test_sticky_mi (styler_mi , index , columns ):
269
+ if index :
270
+ styler_mi .set_sticky (axis = 0 )
271
+ if columns :
272
+ styler_mi .set_sticky (axis = 1 )
273
+
274
+ res = styler_mi .set_uuid ("" ).to_html ()
275
+ assert (
276
+ (
277
+ dedent (
278
+ """\
279
+ #T_ tbody th.level0 {
280
+ position: sticky;
281
+ left: 0px;
282
+ min-width: 75px;
283
+ max-width: 75px;
284
+ background-color: white;
285
+ }
286
+ """
287
+ )
288
+ in res
289
+ )
290
+ is index
291
+ )
292
+ assert (
293
+ (
294
+ dedent (
295
+ """\
296
+ #T_ tbody th.level1 {
297
+ position: sticky;
298
+ left: 75px;
299
+ min-width: 75px;
300
+ max-width: 75px;
301
+ background-color: white;
302
+ }
303
+ """
304
+ )
305
+ in res
306
+ )
307
+ is index
308
+ )
309
+ assert (
310
+ (
311
+ dedent (
312
+ """\
313
+ #T_ thead th.level0 {
314
+ position: sticky;
315
+ top: 0px;
316
+ height: 25px;
317
+ background-color: white;
318
+ }
319
+ """
320
+ )
321
+ in res
322
+ )
323
+ is columns
324
+ )
325
+ assert (
326
+ (
327
+ dedent (
328
+ """\
329
+ #T_ thead th.level1 {
330
+ position: sticky;
331
+ top: 25px;
332
+ height: 25px;
333
+ background-color: white;
334
+ }
335
+ """
336
+ )
337
+ in res
338
+ )
339
+ is columns
340
+ )
341
+
342
+
343
+ @pytest .mark .parametrize ("index" , [False , True ])
344
+ @pytest .mark .parametrize ("columns" , [False , True ])
345
+ def test_sticky_levels (styler_mi , index , columns ):
346
+ if index :
347
+ styler_mi .set_sticky (axis = 0 , levels = [1 ])
348
+ if columns :
349
+ styler_mi .set_sticky (axis = 1 , levels = [1 ])
350
+
351
+ res = styler_mi .set_uuid ("" ).to_html ()
352
+ assert "#T_ tbody th.level0 {" not in res
353
+ assert "#T_ thead th.level0 {" not in res
354
+ assert (
355
+ (
356
+ dedent (
357
+ """\
358
+ #T_ tbody th.level1 {
359
+ position: sticky;
360
+ left: 0px;
361
+ min-width: 75px;
362
+ max-width: 75px;
363
+ background-color: white;
364
+ }
365
+ """
366
+ )
367
+ in res
368
+ )
369
+ is index
370
+ )
371
+ assert (
372
+ (
373
+ dedent (
374
+ """\
375
+ #T_ thead th.level1 {
376
+ position: sticky;
377
+ top: 0px;
378
+ height: 25px;
379
+ background-color: white;
380
+ }
381
+ """
382
+ )
383
+ in res
384
+ )
385
+ is columns
386
+ )
387
+
388
+
389
+ def test_sticky_raises (styler ):
390
+ with pytest .raises (ValueError , match = "`axis` must be" ):
391
+ styler .set_sticky (axis = "bad" )
0 commit comments