@@ -281,26 +281,32 @@ def test_sticky_basic(styler, index, columns, index_name):
281
281
if columns :
282
282
styler .set_sticky (axis = 1 )
283
283
284
- res = styler .set_uuid ("" ).to_html ()
285
-
286
- css_for_index = (
287
- "tr th:first-child {\n position: sticky;\n background-color: white;\n "
288
- "left: 0px;\n z-index: 1;\n }"
284
+ left_css = (
285
+ "#T_ {0} {{\n position: sticky;\n background-color: white;\n "
286
+ " left: 0px;\n z-index: {1};\n }}"
289
287
)
290
- assert (css_for_index in res ) is index
291
-
292
- css_for_cols_1 = (
293
- "thead tr:first-child {\n position: sticky;\n background-color: white;\n "
294
- "top: 0px;\n z-index: 2;\n "
288
+ top_css = (
289
+ "#T_ {0} {{\n position: sticky;\n background-color: white;\n "
290
+ " top: {1}px;\n z-index: {2};\n {3}}}"
295
291
)
296
- css_for_cols_1 += " height: 25px;\n }" if index_name else "}"
297
- assert (css_for_cols_1 in res ) is columns
298
292
299
- css_for_cols_2 = (
300
- "thead tr:nth-child(2) {\n position: sticky;\n background-color: white;\n "
301
- "top: 25px;\n z-index: 2;\n height: 25px;\n }"
293
+ res = styler .set_uuid ("" ).to_html ()
294
+
295
+ # test index stickys over thead and tbody
296
+ assert (left_css .format ("thead tr th:nth-child(1)" , "3 !important" ) in res ) is index
297
+ assert (left_css .format ("tbody tr th:nth-child(1)" , "1" ) in res ) is index
298
+
299
+ # test column stickys including if name row
300
+ assert (
301
+ top_css .format ("thead tr:nth-child(1) th" , "0" , "2" , " height: 25px;\n " ) in res
302
+ ) is (columns and index_name )
303
+ assert (
304
+ top_css .format ("thead tr:nth-child(2) th" , "25" , "2" , " height: 25px;\n " )
305
+ in res
306
+ ) is (columns and index_name )
307
+ assert (top_css .format ("thead tr:nth-child(1) th" , "0" , "2" , "" ) in res ) is (
308
+ columns and not index_name
302
309
)
303
- assert (css_for_cols_2 in res ) is (index_name and columns )
304
310
305
311
306
312
@pytest .mark .parametrize ("index" , [False , True ])
@@ -311,73 +317,30 @@ def test_sticky_mi(styler_mi, index, columns):
311
317
if columns :
312
318
styler_mi .set_sticky (axis = 1 )
313
319
314
- res = styler_mi .set_uuid ("" ).to_html ()
315
- assert (
316
- (
317
- dedent (
318
- """\
319
- #T_ tbody th.level0 {
320
- position: sticky;
321
- left: 0px;
322
- min-width: 75px;
323
- max-width: 75px;
324
- background-color: white;
325
- }
326
- """
327
- )
328
- in res
329
- )
330
- is index
320
+ left_css = (
321
+ "#T_ {0} {{\n position: sticky;\n background-color: white;\n "
322
+ " left: {1}px;\n min-width: 75px;\n max-width: 75px;\n z-index: {2};\n }}"
331
323
)
332
- assert (
333
- (
334
- dedent (
335
- """\
336
- #T_ tbody th.level1 {
337
- position: sticky;
338
- left: 75px;
339
- min-width: 75px;
340
- max-width: 75px;
341
- background-color: white;
342
- }
343
- """
344
- )
345
- in res
346
- )
347
- is index
324
+ top_css = (
325
+ "#T_ {0} {{\n position: sticky;\n background-color: white;\n "
326
+ " top: {1}px;\n height: 25px;\n z-index: {2};\n }}"
348
327
)
328
+
329
+ res = styler_mi .set_uuid ("" ).to_html ()
330
+
331
+ # test the index stickys for thead and tbody over both levels
349
332
assert (
350
- (
351
- dedent (
352
- """\
353
- #T_ thead th.level0 {
354
- position: sticky;
355
- top: 0px;
356
- height: 25px;
357
- background-color: white;
358
- }
359
- """
360
- )
361
- in res
362
- )
363
- is columns
364
- )
333
+ left_css .format ("thead tr th:nth-child(1)" , "0" , "3 !important" ) in res
334
+ ) is index
335
+ assert (left_css .format ("tbody tr th.level0" , "0" , "1" ) in res ) is index
365
336
assert (
366
- (
367
- dedent (
368
- """\
369
- #T_ thead th.level1 {
370
- position: sticky;
371
- top: 25px;
372
- height: 25px;
373
- background-color: white;
374
- }
375
- """
376
- )
377
- in res
378
- )
379
- is columns
380
- )
337
+ left_css .format ("thead tr th:nth-child(2)" , "75" , "3 !important" ) in res
338
+ ) is index
339
+ assert (left_css .format ("tbody tr th.level1" , "75" , "1" ) in res ) is index
340
+
341
+ # test the column stickys for each level row
342
+ assert (top_css .format ("thead tr:nth-child(1) th" , "0" , "2" ) in res ) is columns
343
+ assert (top_css .format ("thead tr:nth-child(2) th" , "25" , "2" ) in res ) is columns
381
344
382
345
383
346
@pytest .mark .parametrize ("index" , [False , True ])
@@ -388,43 +351,29 @@ def test_sticky_levels(styler_mi, index, columns):
388
351
if columns :
389
352
styler_mi .set_sticky (axis = 1 , levels = [1 ])
390
353
391
- res = styler_mi .set_uuid ("" ).to_html ()
392
- assert "#T_ tbody th.level0 {" not in res
393
- assert "#T_ thead th.level0 {" not in res
394
- assert (
395
- (
396
- dedent (
397
- """\
398
- #T_ tbody th.level1 {
399
- position: sticky;
400
- left: 0px;
401
- min-width: 75px;
402
- max-width: 75px;
403
- background-color: white;
404
- }
405
- """
406
- )
407
- in res
408
- )
409
- is index
354
+ left_css = (
355
+ "#T_ {0} {{\n position: sticky;\n background-color: white;\n "
356
+ " left: {1}px;\n min-width: 75px;\n max-width: 75px;\n z-index: {2};\n }}"
410
357
)
411
- assert (
412
- (
413
- dedent (
414
- """\
415
- #T_ thead th.level1 {
416
- position: sticky;
417
- top: 0px;
418
- height: 25px;
419
- background-color: white;
420
- }
421
- """
422
- )
423
- in res
424
- )
425
- is columns
358
+ top_css = (
359
+ "#T_ {0} {{\n position: sticky;\n background-color: white;\n "
360
+ " top: {1}px;\n height: 25px;\n z-index: {2};\n }}"
426
361
)
427
362
363
+ res = styler_mi .set_uuid ("" ).to_html ()
364
+
365
+ # test no sticking of level0
366
+ assert "#T_ thead tr th:nth-child(1)" not in res
367
+ assert "#T_ tbody tr th.level0" not in res
368
+ assert "#T_ thead tr:nth-child(1) th" not in res
369
+
370
+ # test sticking level1
371
+ assert (
372
+ left_css .format ("thead tr th:nth-child(2)" , "0" , "3 !important" ) in res
373
+ ) is index
374
+ assert (left_css .format ("tbody tr th.level1" , "0" , "1" ) in res ) is index
375
+ assert (top_css .format ("thead tr:nth-child(2) th" , "0" , "2" ) in res ) is columns
376
+
428
377
429
378
def test_sticky_raises (styler ):
430
379
with pytest .raises (ValueError , match = "`axis` must be" ):
0 commit comments