Skip to content

Commit b7f0a2d

Browse files
authored
REF: render trimming moved to private method (#45834)
1 parent c055dc4 commit b7f0a2d

File tree

1 file changed

+88
-62
lines changed

1 file changed

+88
-62
lines changed

pandas/io/formats/style_render.py

+88-62
Original file line numberDiff line numberDiff line change
@@ -424,25 +424,20 @@ def _generate_col_header_row(self, iter: tuple, max_cols: int, col_lengths: dict
424424
)
425425
]
426426

427-
column_headers, visible_col_count = [], 0
427+
column_headers: list = []
428+
visible_col_count: int = 0
428429
for c, value in enumerate(clabels[r]):
429430
header_element_visible = _is_visible(c, r, col_lengths)
430431
if header_element_visible:
431432
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+
):
446441
break
447442

448443
header_element = _element(
@@ -509,26 +504,22 @@ def _generate_index_names_row(self, iter: tuple, max_cols: int, col_lengths: dic
509504
for c, name in enumerate(self.data.index.names)
510505
]
511506

512-
column_blanks, visible_col_count = [], 0
507+
column_blanks: list = []
508+
visible_col_count: int = 0
513509
if clabels:
514510
last_level = self.columns.nlevels - 1 # use last level since never sparsed
515511
for c, value in enumerate(clabels[last_level]):
516512
header_element_visible = _is_visible(c, last_level, col_lengths)
517513
if header_element_visible:
518514
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+
):
532523
break
533524

534525
column_blanks.append(
@@ -568,21 +559,66 @@ def _translate_body(self, idx_lengths: dict, max_rows: int, max_cols: int):
568559
if not isinstance(self.data.index, MultiIndex):
569560
rlabels = [[x] for x in rlabels]
570561

571-
body, row_count = [], 0
562+
body: list = []
563+
visible_row_count: int = 0
572564
for r, row_tup in [
573565
z for z in enumerate(self.data.itertuples()) if z[0] not in self.hidden_rows
574566
]:
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+
):
579574
break
575+
580576
body_row = self._generate_body_row(
581577
(r, row_tup, rlabels), max_cols, idx_lengths
582578
)
583579
body.append(body_row)
584580
return body
585581

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+
586622
def _generate_trimmed_row(self, max_cols: int) -> list:
587623
"""
588624
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:
610646
for c in range(self.data.index.nlevels)
611647
]
612648

613-
data, visible_col_count = [], 0
649+
data: list = []
650+
visible_col_count: int = 0
614651
for c, _ in enumerate(self.columns):
615652
data_element_visible = c not in self.hidden_columns
616653
if data_element_visible:
617654
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+
):
631662
break
632663

633664
data.append(
@@ -708,26 +739,21 @@ def _generate_body_row(
708739

709740
index_headers.append(header_element)
710741

711-
data, visible_col_count = [], 0
742+
data: list = []
743+
visible_col_count: int = 0
712744
for c, value in enumerate(row_tup[1:]):
713745
data_element_visible = (
714746
c not in self.hidden_columns and r not in self.hidden_rows
715747
)
716748
if data_element_visible:
717749
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+
):
731757
break
732758

733759
# add custom classes from cell context

0 commit comments

Comments
 (0)