@@ -193,7 +193,7 @@ def __init__(
193
193
self .hidden_index : bool = False
194
194
self .hidden_columns : Sequence [int ] = []
195
195
self .ctx : DefaultDict [Tuple [int , int ], CSSList ] = defaultdict (list )
196
- self .cell_context : Dict [ str , Any ] = {}
196
+ self .cell_context : DefaultDict [ Tuple [ int , int ], str ] = defaultdict ( str )
197
197
self ._todo : List [Tuple [Callable , Tuple , Dict ]] = []
198
198
self .tooltips : Optional [_Tooltips ] = None
199
199
def_precision = get_option ("display.precision" )
@@ -420,19 +420,11 @@ def _translate(self):
420
420
421
421
if clabels :
422
422
for c , value in enumerate (clabels [r ]):
423
- cs = [
424
- COL_HEADING_CLASS ,
425
- f"level{ r } " ,
426
- f"col{ c } " ,
427
- ]
428
- cs .extend (
429
- cell_context .get ("col_headings" , {}).get (r , {}).get (c , [])
430
- )
431
423
es = {
432
424
"type" : "th" ,
433
425
"value" : value ,
434
426
"display_value" : value ,
435
- "class" : " " . join ( cs ) ,
427
+ "class" : f" { COL_HEADING_CLASS } level { r } col { c } " ,
436
428
"is_visible" : _is_visible (c , r , col_lengths ),
437
429
}
438
430
colspan = col_lengths .get ((r , c ), 0 )
@@ -492,7 +484,6 @@ def _translate(self):
492
484
row_es .append (es )
493
485
494
486
for c , value in enumerate (row_tup [1 :]):
495
- cs = [DATA_CLASS , f"row{ r } " , f"col{ c } " ]
496
487
formatter = self ._display_funcs [(r , c )]
497
488
row_dict = {
498
489
"type" : "td" ,
@@ -505,12 +496,14 @@ def _translate(self):
505
496
# only add an id if the cell has a style
506
497
props : CSSList = []
507
498
if self .cell_ids or (r , c ) in ctx :
508
- row_dict ["id" ] = "_" . join ( cs [ 1 :])
499
+ row_dict ["id" ] = f"row { r } _col { c } "
509
500
props .extend (ctx [r , c ])
510
501
511
502
# add custom classes from cell context
512
- cs .extend (cell_context .get ("data" , {}).get (r , {}).get (c , []))
513
- row_dict ["class" ] = " " .join (cs )
503
+ cls = ""
504
+ if (r , c ) in cell_context :
505
+ cls = " " + cell_context [r , c ]
506
+ row_dict ["class" ] = f"{ DATA_CLASS } row{ r } col{ c } { cls } "
514
507
515
508
row_es .append (row_dict )
516
509
if props : # (), [] won't be in cellstyle_map, cellstyle respectively
@@ -736,15 +729,10 @@ def set_td_classes(self, classes: DataFrame) -> Styler:
736
729
"""
737
730
classes = classes .reindex_like (self .data )
738
731
739
- mask = (classes .isna ()) | (classes .eq ("" ))
740
- self .cell_context ["data" ] = {
741
- r : {
742
- c : [str (classes .iloc [r , c ])]
743
- for c , cn in enumerate (classes .columns )
744
- if not mask .iloc [r , c ]
745
- }
746
- for r , rn in enumerate (classes .index )
747
- }
732
+ for r , row_tup in enumerate (classes .itertuples ()):
733
+ for c , value in enumerate (row_tup [1 :]):
734
+ if not (pd .isna (value ) or value == "" ):
735
+ self .cell_context [(r , c )] = str (value )
748
736
749
737
return self
750
738
@@ -859,7 +847,7 @@ def clear(self) -> None:
859
847
"""
860
848
self .ctx .clear ()
861
849
self .tooltips = None
862
- self .cell_context = {}
850
+ self .cell_context . clear ()
863
851
self ._todo = []
864
852
865
853
def _compute (self ):
0 commit comments