Skip to content

Commit 622ed63

Browse files
committed
refactor: improve cell rendering logic in DataFrameHtmlFormatter by utilizing raw values for custom cell builders and optimizing expandable cell creation
1 parent ecab831 commit 622ed63

1 file changed

Lines changed: 24 additions & 31 deletions

File tree

python/datafusion/html_formatter.py

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -213,26 +213,32 @@ def _build_table_body(self, batches: list, table_uuid: str) -> List[str]:
213213
html.append("<tr>")
214214

215215
for col_idx, column in enumerate(batch.columns):
216+
# Get the raw value from the column
216217
raw_value = self._get_cell_value(column, row_idx)
217-
formatted_value = self._format_cell_value(raw_value)
218218

219-
if (
220-
len(str(formatted_value)) > self.max_cell_length
221-
and self.enable_cell_expansion
222-
):
219+
# If we have a custom cell builder, use it directly with the raw value
220+
if self._custom_cell_builder:
223221
html.append(
224-
self._build_expandable_cell(
225-
raw_value,
226-
formatted_value,
227-
row_count,
228-
col_idx,
229-
table_uuid,
222+
self._custom_cell_builder(
223+
raw_value, row_count, col_idx, table_uuid
230224
)
231225
)
232226
else:
233-
html.append(
234-
self._build_regular_cell(raw_value, formatted_value)
235-
)
227+
# Format the value using type formatters
228+
formatted_value = self._format_cell_value(raw_value)
229+
230+
# Build the appropriate cell based on length and settings
231+
if (
232+
len(str(raw_value)) > self.max_cell_length
233+
and self.enable_cell_expansion
234+
):
235+
html.append(
236+
self._build_expandable_cell(
237+
formatted_value, row_count, col_idx, table_uuid
238+
)
239+
)
240+
else:
241+
html.append(self._build_regular_cell(formatted_value))
236242

237243
html.append("</tr>")
238244

@@ -270,22 +276,14 @@ def _format_cell_value(self, value: Any) -> str:
270276
if isinstance(value, type_cls):
271277
return formatter(value)
272278

279+
# If no formatter matched, return string representation
273280
return str(value)
274281

275282
def _build_expandable_cell(
276-
self,
277-
raw_value: Any,
278-
formatted_value: str,
279-
row_count: int,
280-
col_idx: int,
281-
table_uuid: str,
283+
self, formatted_value: str, row_count: int, col_idx: int, table_uuid: str
282284
) -> str:
283285
"""Build an expandable cell for long content."""
284-
# If custom cell builder is provided, use it
285-
if self._custom_cell_builder:
286-
return self._custom_cell_builder(raw_value, row_count, col_idx, table_uuid)
287-
288-
short_value = formatted_value[: self.max_cell_length]
286+
short_value = str(formatted_value)[: self.max_cell_length]
289287
return (
290288
f"<td style='{self.style_provider.get_cell_style()}'>"
291289
f"<div class='expandable-container'>"
@@ -300,13 +298,8 @@ def _build_expandable_cell(
300298
f"</td>"
301299
)
302300

303-
def _build_regular_cell(self, raw_value: Any, formatted_value: str) -> str:
301+
def _build_regular_cell(self, formatted_value: str) -> str:
304302
"""Build a regular table cell."""
305-
# If custom cell builder is provided, use it with dummy row/col values
306-
if self._custom_cell_builder:
307-
# Use 0, 0, "" as dummy values since this isn't an expandable cell
308-
return self._custom_cell_builder(raw_value, 0, 0, "")
309-
310303
return (
311304
f"<td style='{self.style_provider.get_cell_style()}'>{formatted_value}</td>"
312305
)

0 commit comments

Comments
 (0)