Skip to content
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

fix(python): Fix _repr_html_ double-height rows (#5645) #6534

Merged
merged 3 commits into from
Feb 2, 2023
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
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