-
Notifications
You must be signed in to change notification settings - Fork 168
feat: Use _repr_html_ when native supports it
#2776
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
base: main
Are you sure you want to change the base?
Changes from 17 commits
9ad45ba
04e1f51
bf7e87a
edb7ae4
3769326
3559cf8
ea42e83
3748f23
5bb73d8
e77c8de
d8f09a4
4227f10
690e6e4
6f2ab6b
61497d6
c4dca25
f73d564
e32e139
57e333d
7758265
dfb5f5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1606,6 +1606,48 @@ def generate_repr(header: str, native_repr: str) -> str: | |
| ) | ||
|
|
||
|
|
||
| # NOTE: Unsure on how to test this reliably | ||
| def generate_repr_html( | ||
| header: Literal["Narwhals DataFrame", "Narwhals LazyFrame", "Narwhals Series"], | ||
| native_html: str, | ||
| ) -> str | None: # pragma: no cover | ||
| if header == "Narwhals LazyFrame" and "LazyFrame" in native_html: | ||
| html = native_html.replace("LazyFrame", "LazyFrame.to_native()") | ||
| return f"{html}<p><b>{header}</b></p>" | ||
|
||
| import io | ||
| import xml.etree.ElementTree as ET | ||
|
|
||
| style_css = ( | ||
| ".dataframe caption { " | ||
| "caption-side: bottom; " | ||
| "text-align: center; " | ||
| "font-weight: bold; " | ||
| "padding-top: 8px;" | ||
| "}" | ||
| ) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If anyone has any suggestions for styling - feel free to experiment/comment π The only decision I'd made so far was putting the With the default There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's very reasonable! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| try: | ||
| tree = ET.parse(io.StringIO(native_html.replace("<style scoped>", "<style>"))) # noqa: S314 | ||
| except (SyntaxError, TypeError): | ||
| return None | ||
| table = tree.find("table") | ||
| if table is None: | ||
| return None | ||
| caption = ET.Element("caption") | ||
| caption.text = header | ||
| table.insert(0, caption) | ||
| style = tree.find("style") | ||
| if style is not None: | ||
| text_existing = style.text or "" | ||
| style.text = f"{text_existing}\n{style_css}" | ||
| else: | ||
| style = ET.Element("style") | ||
| style.text = style_css | ||
| tree.getroot().insert(0, style) | ||
| buf = io.BytesIO() | ||
| tree.write(buf, "utf-8", method="html") | ||
| return buf.getvalue().decode() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything else in this function is a new language to me - I am not very helpful There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yeah I had to learn a bit of To simplify this:
So I'm essentially doing a fancy find/replace, but trying to preserve the structure of the document |
||
|
|
||
|
|
||
| def check_columns_exist( | ||
| subset: Collection[str], /, *, available: Collection[str] | ||
| ) -> ColumnNotFoundError | None: | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had to add this branch for
pl.LazyFrameas it wasn't parsing with my naive wrapper:Seems to fail on the first
<p>in https://github.com/pola-rs/polars/blob/dfa5efe71156c654a1ba3a54b865eae723a818e9/py-polars/polars/lazyframe/frame.py#L783