Skip to content

Commit f23b940

Browse files
authored
FIX Adjusts html_repr based on configuration (scikit-learn#17093)
* ENH Adjusts html_repr based on configuration * CLN Returns None instead * CLN Uses property hack * CLN Address comments
1 parent b2b88d3 commit f23b940

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

sklearn/base.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,25 @@ def _validate_data(self, X, y=None, reset=True,
437437

438438
return out
439439

440+
@property
440441
def _repr_html_(self):
441-
"""HTML representation of estimator"""
442+
"""HTML representation of estimator.
443+
444+
This is redundant with the logic of `_repr_mimebundle_`. The latter
445+
should be favorted in the long term, `_repr_html_` is only
446+
implemented for consumers who do not interpret `_repr_mimbundle_`.
447+
"""
448+
if get_config()["display"] != 'diagram':
449+
raise AttributeError("_repr_html_ is only defined when the "
450+
"'display' configuration option is set to "
451+
"'diagram'")
452+
return self._repr_html_inner
453+
454+
def _repr_html_inner(self):
455+
"""This function is returned by the @property `_repr_html_` to make
456+
`hasattr(estimator, "_repr_html_") return `True` or `False` depending
457+
on `get_config()["display"]`.
458+
"""
442459
return estimator_html_repr(self)
443460

444461
def _repr_mimebundle_(self, **kwargs):

sklearn/tests/test_base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,3 +525,15 @@ def test_repr_mimebundle_():
525525
output = tree._repr_mimebundle_()
526526
assert "text/plain" in output
527527
assert "text/html" in output
528+
529+
530+
def test_repr_html_wraps():
531+
# Checks the display configuration flag controls the html output
532+
tree = DecisionTreeClassifier()
533+
msg = "_repr_html_ is only defined when"
534+
with pytest.raises(AttributeError, match=msg):
535+
output = tree._repr_html_()
536+
537+
with config_context(display='diagram'):
538+
output = tree._repr_html_()
539+
assert "<style>" in output

0 commit comments

Comments
 (0)