Skip to content

Commit 3944249

Browse files
committed
feat: Refactor HTML rendering and document JS tests
1 parent f32a53f commit 3944249

File tree

2 files changed

+447
-402
lines changed

2 files changed

+447
-402
lines changed

bigframes/display/html.py

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -115,44 +115,59 @@ def _render_table_body(
115115

116116
row = dataframe.iloc[i]
117117
for col_name, value in row.items():
118-
col_name_str = str(col_name)
119-
if is_continuation and col_name_str in clear_on_continuation:
120-
body_parts.append(" <td></td>")
121-
continue
122118
dtype = dataframe.dtypes.loc[col_name] # type: ignore
123-
124-
if col_name_str in nested_originated_columns:
125-
align = "left"
126-
else:
127-
align = "right" if _is_dtype_numeric(dtype) else "left"
128-
129-
cell_content = ""
130-
if pandas.api.types.is_scalar(value) and pd.isna(value):
131-
if is_continuation:
132-
# For padding nulls in continuation rows, show empty cell
133-
body_parts.append(f' <td class="cell-align-{align}"></td>')
134-
else:
135-
# For primary nulls, keep showing the <NA> indicator but maybe styled
136-
body_parts.append(
137-
f' <td class="cell-align-{align}">'
138-
'<em class="null-value">&lt;NA&gt;</em></td>'
139-
)
140-
continue
141-
elif isinstance(value, float):
142-
cell_content = f"{value:.{precision}f}"
143-
else:
144-
cell_content = str(value)
145-
146-
# Use classes for alignment
147-
body_parts.append(
148-
f' <td class="cell-align-{align}">'
149-
f"{html.escape(cell_content)}</td>"
119+
cell_html = _render_cell(
120+
value,
121+
dtype,
122+
is_continuation,
123+
str(col_name),
124+
clear_on_continuation,
125+
nested_originated_columns,
126+
precision,
150127
)
128+
body_parts.append(cell_html)
151129
body_parts.append(" </tr>")
152130
body_parts.append(" </tbody>")
153131
return "\n".join(body_parts)
154132

155133

134+
def _render_cell(
135+
value: Any,
136+
dtype: Any,
137+
is_continuation: bool,
138+
col_name_str: str,
139+
clear_on_continuation: list[str],
140+
nested_originated_columns: set[str],
141+
precision: int,
142+
) -> str:
143+
"""Render a single cell of the HTML table."""
144+
if is_continuation and col_name_str in clear_on_continuation:
145+
return " <td></td>"
146+
147+
if col_name_str in nested_originated_columns:
148+
align = "left"
149+
else:
150+
align = "right" if _is_dtype_numeric(dtype) else "left"
151+
152+
if pandas.api.types.is_scalar(value) and pd.isna(value):
153+
if is_continuation:
154+
# For padding nulls in continuation rows, show empty cell
155+
return f' <td class="cell-align-{align}"></td>'
156+
else:
157+
# For primary nulls, keep showing the <NA> indicator but maybe styled
158+
return (
159+
f' <td class="cell-align-{align}">'
160+
'<em class="null-value">&lt;NA&gt;</em></td>'
161+
)
162+
163+
if isinstance(value, float):
164+
cell_content = f"{value:.{precision}f}"
165+
else:
166+
cell_content = str(value)
167+
168+
return f' <td class="cell-align-{align}">' f"{html.escape(cell_content)}</td>"
169+
170+
156171
def _obj_ref_rt_to_html(obj_ref_rt: str) -> str:
157172
obj_ref_rt_json = json.loads(obj_ref_rt)
158173
obj_ref_details = obj_ref_rt_json["objectref"]["details"]

0 commit comments

Comments
 (0)