From 9ad45ba370b3f6a3c7f5d23ff7911a49896f2172 Mon Sep 17 00:00:00 2001 From: dangotbanned <125183946+dangotbanned@users.noreply.github.com> Date: Thu, 3 Jul 2025 14:01:17 +0100 Subject: [PATCH 1/6] feat: Use `_repr_html_` when native supports it - Related #1702 - https://ipython.readthedocs.io/en/stable/config/integrating.html#rich-display - https://github.com/pandas-dev/pandas/blob/22f12fc5d3f7fda3f198760204e7c13150c78581/pandas/core/frame.py#L1189-L1232 - https://github.com/pola-rs/polars/blob/8011fa34e0c5f1270ef52e2d3b0b2946bb2faa72/py-polars/polars/dataframe/frame.py#L1580-L1605 --- narwhals/_utils.py | 35 +++++++++++++++++++++++++++++++++++ narwhals/dataframe.py | 8 ++++++++ 2 files changed, 43 insertions(+) diff --git a/narwhals/_utils.py b/narwhals/_utils.py index 788d82c1c0..d8ac60c834 100644 --- a/narwhals/_utils.py +++ b/narwhals/_utils.py @@ -1562,6 +1562,41 @@ def generate_repr(header: str, native_repr: str) -> str: ) +def generate_repr_html(header: str, native_html: str) -> str | None: + import io + import xml.etree.ElementTree as ET + + style_css = ( + ".dataframe caption { " + "caption-side: bottom; " + "text-align: center; " + "font-weight: bold; " + "padding-top: 8px;" + "}" + ) + try: + tree = ET.parse(io.StringIO(native_html.replace("