Skip to content

REF: render trimming moved to private method #45834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 88 additions & 62 deletions pandas/io/formats/style_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,25 +424,20 @@ def _generate_col_header_row(self, iter: tuple, max_cols: int, col_lengths: dict
)
]

column_headers, visible_col_count = [], 0
column_headers: list = []
visible_col_count: int = 0
for c, value in enumerate(clabels[r]):
header_element_visible = _is_visible(c, r, col_lengths)
if header_element_visible:
visible_col_count += col_lengths.get((r, c), 0)
if visible_col_count > max_cols:
# add an extra column with `...` value to indicate trimming
column_headers.append(
_element(
"th",
(
f"{self.css['col_heading']} {self.css['level']}{r} "
f"{self.css['col_trim']}"
),
"...",
True,
attributes="",
)
)
if self._check_trim(
visible_col_count,
max_cols,
column_headers,
"th",
f"{self.css['col_heading']} {self.css['level']}{r} "
f"{self.css['col_trim']}",
):
break

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

column_blanks, visible_col_count = [], 0
column_blanks: list = []
visible_col_count: int = 0
if clabels:
last_level = self.columns.nlevels - 1 # use last level since never sparsed
for c, value in enumerate(clabels[last_level]):
header_element_visible = _is_visible(c, last_level, col_lengths)
if header_element_visible:
visible_col_count += 1
if visible_col_count > max_cols:
column_blanks.append(
_element(
"th",
(
f"{self.css['blank']} {self.css['col']}{c} "
f"{self.css['col_trim']}"
),
self.css["blank_value"],
True,
attributes="",
)
)
if self._check_trim(
visible_col_count,
max_cols,
column_blanks,
"th",
f"{self.css['blank']} {self.css['col']}{c} {self.css['col_trim']}",
self.css["blank_value"],
):
break

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

body, row_count = [], 0
body: list = []
visible_row_count: int = 0
for r, row_tup in [
z for z in enumerate(self.data.itertuples()) if z[0] not in self.hidden_rows
]:
row_count += 1
if row_count > max_rows: # used only to add a '...' trimmed row:
trimmed_row = self._generate_trimmed_row(max_cols)
body.append(trimmed_row)
visible_row_count += 1
if self._check_trim(
visible_row_count,
max_rows,
body,
"row",
):
break

body_row = self._generate_body_row(
(r, row_tup, rlabels), max_cols, idx_lengths
)
body.append(body_row)
return body

def _check_trim(
self,
count,
max,
obj,
element,
css=None,
value="...",
):
"""
Indicates whether to break render loops and append a trimming indicator

Parameters
----------
count : int
The loop count of previous visible items.
max : int
The allowable rendered items in the loop.
obj : list
The current render collection of the rendered items.
element : str
The type of element to append in the case a trimming indicator is needed.
css : str, optional
The css to add to the trimming indicator element.
value : str, optional
The value of the elements display if necessary.

Returns
-------
result : bool
Whether a trimming element was required and appended.
"""
if count > max:
if element == "row":
obj.append(self._generate_trimmed_row(max))
else:
obj.append(_element(element, css, value, True, attributes=""))
return True
return False

def _generate_trimmed_row(self, max_cols: int) -> list:
"""
When a render has too many rows we generate a trimming row containing "..."
Expand Down Expand Up @@ -610,24 +646,19 @@ def _generate_trimmed_row(self, max_cols: int) -> list:
for c in range(self.data.index.nlevels)
]

data, visible_col_count = [], 0
data: list = []
visible_col_count: int = 0
for c, _ in enumerate(self.columns):
data_element_visible = c not in self.hidden_columns
if data_element_visible:
visible_col_count += 1
if visible_col_count > max_cols:
data.append(
_element(
"td",
(
f"{self.css['data']} {self.css['row_trim']} "
f"{self.css['col_trim']}"
),
"...",
True,
attributes="",
)
)
if self._check_trim(
visible_col_count,
max_cols,
data,
"td",
f"{self.css['data']} {self.css['row_trim']} {self.css['col_trim']}",
):
break

data.append(
Expand Down Expand Up @@ -708,26 +739,21 @@ def _generate_body_row(

index_headers.append(header_element)

data, visible_col_count = [], 0
data: list = []
visible_col_count: int = 0
for c, value in enumerate(row_tup[1:]):
data_element_visible = (
c not in self.hidden_columns and r not in self.hidden_rows
)
if data_element_visible:
visible_col_count += 1
if visible_col_count > max_cols:
data.append(
_element(
"td",
(
f"{self.css['data']} {self.css['row']}{r} "
f"{self.css['col_trim']}"
),
"...",
True,
attributes="",
)
)
if self._check_trim(
visible_col_count,
max_cols,
data,
"td",
f"{self.css['data']} {self.css['row']}{r} {self.css['col_trim']}",
):
break

# add custom classes from cell context
Expand Down