@@ -424,25 +424,20 @@ def _generate_col_header_row(self, iter: tuple, max_cols: int, col_lengths: dict
424
424
)
425
425
]
426
426
427
- column_headers , visible_col_count = [], 0
427
+ column_headers : list = []
428
+ visible_col_count : int = 0
428
429
for c , value in enumerate (clabels [r ]):
429
430
header_element_visible = _is_visible (c , r , col_lengths )
430
431
if header_element_visible :
431
432
visible_col_count += col_lengths .get ((r , c ), 0 )
432
- if visible_col_count > max_cols :
433
- # add an extra column with `...` value to indicate trimming
434
- column_headers .append (
435
- _element (
436
- "th" ,
437
- (
438
- f"{ self .css ['col_heading' ]} { self .css ['level' ]} { r } "
439
- f"{ self .css ['col_trim' ]} "
440
- ),
441
- "..." ,
442
- True ,
443
- attributes = "" ,
444
- )
445
- )
433
+ if self ._check_trim (
434
+ visible_col_count ,
435
+ max_cols ,
436
+ column_headers ,
437
+ "th" ,
438
+ f"{ self .css ['col_heading' ]} { self .css ['level' ]} { r } "
439
+ f"{ self .css ['col_trim' ]} " ,
440
+ ):
446
441
break
447
442
448
443
header_element = _element (
@@ -509,26 +504,22 @@ def _generate_index_names_row(self, iter: tuple, max_cols: int, col_lengths: dic
509
504
for c , name in enumerate (self .data .index .names )
510
505
]
511
506
512
- column_blanks , visible_col_count = [], 0
507
+ column_blanks : list = []
508
+ visible_col_count : int = 0
513
509
if clabels :
514
510
last_level = self .columns .nlevels - 1 # use last level since never sparsed
515
511
for c , value in enumerate (clabels [last_level ]):
516
512
header_element_visible = _is_visible (c , last_level , col_lengths )
517
513
if header_element_visible :
518
514
visible_col_count += 1
519
- if visible_col_count > max_cols :
520
- column_blanks .append (
521
- _element (
522
- "th" ,
523
- (
524
- f"{ self .css ['blank' ]} { self .css ['col' ]} { c } "
525
- f"{ self .css ['col_trim' ]} "
526
- ),
527
- self .css ["blank_value" ],
528
- True ,
529
- attributes = "" ,
530
- )
531
- )
515
+ if self ._check_trim (
516
+ visible_col_count ,
517
+ max_cols ,
518
+ column_blanks ,
519
+ "th" ,
520
+ f"{ self .css ['blank' ]} { self .css ['col' ]} { c } { self .css ['col_trim' ]} " ,
521
+ self .css ["blank_value" ],
522
+ ):
532
523
break
533
524
534
525
column_blanks .append (
@@ -568,21 +559,66 @@ def _translate_body(self, idx_lengths: dict, max_rows: int, max_cols: int):
568
559
if not isinstance (self .data .index , MultiIndex ):
569
560
rlabels = [[x ] for x in rlabels ]
570
561
571
- body , row_count = [], 0
562
+ body : list = []
563
+ visible_row_count : int = 0
572
564
for r , row_tup in [
573
565
z for z in enumerate (self .data .itertuples ()) if z [0 ] not in self .hidden_rows
574
566
]:
575
- row_count += 1
576
- if row_count > max_rows : # used only to add a '...' trimmed row:
577
- trimmed_row = self ._generate_trimmed_row (max_cols )
578
- body .append (trimmed_row )
567
+ visible_row_count += 1
568
+ if self ._check_trim (
569
+ visible_row_count ,
570
+ max_rows ,
571
+ body ,
572
+ "row" ,
573
+ ):
579
574
break
575
+
580
576
body_row = self ._generate_body_row (
581
577
(r , row_tup , rlabels ), max_cols , idx_lengths
582
578
)
583
579
body .append (body_row )
584
580
return body
585
581
582
+ def _check_trim (
583
+ self ,
584
+ count ,
585
+ max ,
586
+ obj ,
587
+ element ,
588
+ css = None ,
589
+ value = "..." ,
590
+ ):
591
+ """
592
+ Indicates whether to break render loops and append a trimming indicator
593
+
594
+ Parameters
595
+ ----------
596
+ count : int
597
+ The loop count of previous visible items.
598
+ max : int
599
+ The allowable rendered items in the loop.
600
+ obj : list
601
+ The current render collection of the rendered items.
602
+ element : str
603
+ The type of element to append in the case a trimming indicator is needed.
604
+ css : str, optional
605
+ The css to add to the trimming indicator element.
606
+ value : str, optional
607
+ The value of the elements display if necessary.
608
+
609
+ Returns
610
+ -------
611
+ result : bool
612
+ Whether a trimming element was required and appended.
613
+ """
614
+ if count > max :
615
+ if element == "row" :
616
+ obj .append (self ._generate_trimmed_row (max ))
617
+ else :
618
+ obj .append (_element (element , css , value , True , attributes = "" ))
619
+ return True
620
+ return False
621
+
586
622
def _generate_trimmed_row (self , max_cols : int ) -> list :
587
623
"""
588
624
When a render has too many rows we generate a trimming row containing "..."
@@ -610,24 +646,19 @@ def _generate_trimmed_row(self, max_cols: int) -> list:
610
646
for c in range (self .data .index .nlevels )
611
647
]
612
648
613
- data , visible_col_count = [], 0
649
+ data : list = []
650
+ visible_col_count : int = 0
614
651
for c , _ in enumerate (self .columns ):
615
652
data_element_visible = c not in self .hidden_columns
616
653
if data_element_visible :
617
654
visible_col_count += 1
618
- if visible_col_count > max_cols :
619
- data .append (
620
- _element (
621
- "td" ,
622
- (
623
- f"{ self .css ['data' ]} { self .css ['row_trim' ]} "
624
- f"{ self .css ['col_trim' ]} "
625
- ),
626
- "..." ,
627
- True ,
628
- attributes = "" ,
629
- )
630
- )
655
+ if self ._check_trim (
656
+ visible_col_count ,
657
+ max_cols ,
658
+ data ,
659
+ "td" ,
660
+ f"{ self .css ['data' ]} { self .css ['row_trim' ]} { self .css ['col_trim' ]} " ,
661
+ ):
631
662
break
632
663
633
664
data .append (
@@ -708,26 +739,21 @@ def _generate_body_row(
708
739
709
740
index_headers .append (header_element )
710
741
711
- data , visible_col_count = [], 0
742
+ data : list = []
743
+ visible_col_count : int = 0
712
744
for c , value in enumerate (row_tup [1 :]):
713
745
data_element_visible = (
714
746
c not in self .hidden_columns and r not in self .hidden_rows
715
747
)
716
748
if data_element_visible :
717
749
visible_col_count += 1
718
- if visible_col_count > max_cols :
719
- data .append (
720
- _element (
721
- "td" ,
722
- (
723
- f"{ self .css ['data' ]} { self .css ['row' ]} { r } "
724
- f"{ self .css ['col_trim' ]} "
725
- ),
726
- "..." ,
727
- True ,
728
- attributes = "" ,
729
- )
730
- )
750
+ if self ._check_trim (
751
+ visible_col_count ,
752
+ max_cols ,
753
+ data ,
754
+ "td" ,
755
+ f"{ self .css ['data' ]} { self .css ['row' ]} { r } { self .css ['col_trim' ]} " ,
756
+ ):
731
757
break
732
758
733
759
# add custom classes from cell context
0 commit comments