Skip to content

Commit cb68cb0

Browse files
committed
Add HTML representation methods for Jupyter notebook display in LabeledEnum and DataSet
1 parent a49f248 commit cb68cb0

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

doc/release_notes/release_3.14.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44

55
✨ New features:
66

7+
* **Jupyter notebook HTML representation**: Added rich HTML display support for DataSet and LabeledEnum objects
8+
* New `_repr_html_()` method on `DataSet` class for styled HTML table rendering in Jupyter notebooks
9+
* New `_repr_html_()` method on `LabeledEnum` class for formatted display of enum values with labels
10+
* CSS styling with `.guidata-dataset-table` class for consistent visual appearance
11+
* Automatically called by Jupyter when displaying objects as cell output
12+
713
* **Secure build utility**: Added `--prebuild` option to run commands before package build
814
* New `--prebuild` command-line argument for `guidata.utils.securebuild`
915
* Executes custom commands in the temporary build directory before `python -m build`

guidata/dataset/dataitems.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,27 @@ def __hash__(self) -> int:
178178
return hash(self._value_)
179179

180180

181+
# Add _repr_html_ after class definition to avoid Enum member name restrictions
182+
def _labeled_enum_repr_html(self) -> str:
183+
"""Return HTML representation for Jupyter notebook display.
184+
185+
This method is automatically called by Jupyter when displaying the object
186+
as a cell output, providing a styled representation of the enum value.
187+
188+
Returns:
189+
HTML representation of the enum value with styling.
190+
"""
191+
enum_class = self.__class__.__name__
192+
return (
193+
f'<span style="color: #5294e2; font-family: monospace;">'
194+
f"{enum_class}.<b>{self.name}</b></span> "
195+
f'(<code>{self._value_!r}</code>: "{self.label}")'
196+
)
197+
198+
199+
LabeledEnum._repr_html_ = _labeled_enum_repr_html
200+
201+
181202
class NumericTypeItem(DataItem):
182203
"""Numeric data item
183204

guidata/dataset/datatypes.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,29 @@
8282

8383
DEBUG_DESERIALIZE = False
8484

85+
# CSS styling for HTML tables in Jupyter notebooks
86+
DATASET_TABLE_CSS = """
87+
<style>
88+
.guidata-dataset-table {
89+
border-collapse: collapse;
90+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
91+
'Helvetica Neue', Arial, sans-serif;
92+
font-size: 13px;
93+
margin: 10px 0;
94+
}
95+
.guidata-dataset-table td {
96+
border: 1px solid #dee2e6;
97+
padding: 8px 12px;
98+
}
99+
.guidata-dataset-table tr:nth-child(even) {
100+
background-color: #f8f9fa;
101+
}
102+
.guidata-dataset-table tr:hover {
103+
background-color: #e9ecef;
104+
}
105+
</style>
106+
"""
107+
85108
if TYPE_CHECKING:
86109
from qtpy.QtCore import QSize
87110
from qtpy.QtWidgets import QDialog, QWidget
@@ -1846,6 +1869,17 @@ def to_html(self) -> str:
18461869
html += "</table>"
18471870
return html
18481871

1872+
def _repr_html_(self) -> str:
1873+
"""Return HTML representation for Jupyter notebook display.
1874+
1875+
This method is automatically called by Jupyter when displaying the object
1876+
as a cell output, providing a rich HTML rendering of the dataset.
1877+
1878+
Returns:
1879+
HTML representation of the dataset with styling.
1880+
"""
1881+
return DATASET_TABLE_CSS + self.to_html()
1882+
18491883
def serialize(self, writer: HDF5Writer | JSONWriter | INIWriter) -> None:
18501884
"""Serialize the dataset
18511885

0 commit comments

Comments
 (0)