Skip to content

Commit

Permalink
fix(python): Fix _repr_html_ double-height rows (#5645) (#6534)
Browse files Browse the repository at this point in the history
  • Loading branch information
2-5 authored Feb 2, 2023
1 parent a477a85 commit a08c7ba
Showing 1 changed file with 9 additions and 42 deletions.
51 changes: 9 additions & 42 deletions py-polars/polars/_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def write(self, inner: str) -> None:
self.elements.append(inner)

def render(self) -> list[str]:
with Tag(self.elements, "table", {"border": "1", "class": "dataframe"}):
with Tag(self.elements, "table", {"border": "1", "class": "pl-dataframe"}):
self.write_header()
self.write_body()
return self.elements
Expand All @@ -134,51 +134,18 @@ class NotebookFormatter(HTMLFormatter):
"""

def write_style(self) -> None:
# SNIPPET Forked from pandas.

# We use the "scoped" attribute here so that the desired
# style properties for the data frame are not then applied
# throughout the entire notebook.
template_first = """\
<style scoped>"""
template_last = """\
</style>"""
template_select = """\
.dataframe %s {
%s: %s;
}"""
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"),
]
# vs code cannot deal with that css line
# see #4102
if not in_vscode_notebook():
element_props.append(("td", "line-height", "95%"))

template_mid = "\n\n".join(template_select % t for t in element_props)
template = dedent("\n".join((template_first, template_mid, template_last)))
self.write(template)
style = """\
<style>
.pl-dataframe > thead > tr > th {
text-align: right;
}
</style>
"""
self.write(dedent(style))

def render(self) -> list[str]:
"""Return the lines needed to render a HTML table."""
with Tag(self.elements, "div"):
self.write_style()
super().render()
return self.elements


def in_vscode_notebook() -> bool:
try:
# autoimported by notebooks
get_ipython() # type: ignore[name-defined]
os.environ["VSCODE_CWD"]
except NameError:
return False # not in notebook
except KeyError:
return False # not in VSCode
return True

0 comments on commit a08c7ba

Please sign in to comment.