Skip to content

Commit

Permalink
python: improve html render (#3667)
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Jun 11, 2022
1 parent c627af4 commit a5ea32b
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions py-polars/polars/_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __enter__(self) -> None:
if self.attributes is not None:
s = f"<{self.tag} "
for k, v in self.attributes.items():
s += f'{k}="{v} "'
s += f'{k}="{v}" '
s += ">"
self.elements.append(s)
else:
Expand Down Expand Up @@ -67,6 +67,7 @@ def write_header(self) -> None:
"""
Writes the header of an HTML table.
"""
self.elements.append(f"<small>shape: {self.df.shape}</small>")
with Tag(self.elements, "thead"):
with Tag(self.elements, "tr"):
columns = self.df.columns
Expand Down Expand Up @@ -103,9 +104,7 @@ def write_body(self) -> None:
if series.dtype == Object:
self.elements.append(f"{series[r]}")
else:
self.elements.append(
f"<pre>{series._s.get_fmt(r)}</pre>"
)
self.elements.append(f"{series._s.get_fmt(r)}")

def write(self, inner: str) -> None:
self.elements.append(inner)
Expand Down Expand Up @@ -141,8 +140,11 @@ def write_style(self) -> None:
element_props = [
("tbody tr th:only-of-type", "vertical-align", "middle"),
("tbody tr th", "vertical-align", "top"),
("thead th", "text-align", "right"),
("td", "white-space", "pre"),
("td", "padding-top", "0"),
("td", "padding-bottom", "0"),
]
element_props.append(("thead th", "text-align", "right"))
template_mid = "\n\n".join(map(lambda t: template_select % t, element_props))
template = dedent("\n".join((template_first, template_mid, template_last)))
self.write(template)
Expand Down

0 comments on commit a5ea32b

Please sign in to comment.