8000 ENH Improves message for HTML repr when notebook is not trusted (#21316) · scikit-learn/scikit-learn@d5db197 · GitHub
[go: up one dir, main page]

Skip to content

Commit d5db197

Browse files
authored
ENH Improves message for HTML repr when notebook is not trusted (#21316)
1 parent 8f621ad commit d5db197

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

doc/whats_new/v1.1.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ Changelog
120120
:pr:`20880` by :user:`Guillaume Lemaitre <glemaitre>`
121121
and :user:`András Simon <simonandras>`.
122122

123+
- |Enhancement| :func:`utils.estimator_html_repr` shows a more helpful error
124+
message when running in a jupyter notebook that is not trusted. :pr:`21316`
125+
by `Thomas Fan`_.
126+
123127
Code and Documentation Contributors
124128
-----------------------------------
125129

sklearn/utils/_estimator_html_repr.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ def _write_estimator_html(
309309
display: inline-block;
310310
position: relative;
311311
}
312+
#$id div.sk-text-repr-fallback {
313+
display: none;
314+
}
312315
""".replace(
313316
" ", ""
314317
).replace(
@@ -335,16 +338,33 @@ def estimator_html_repr(estimator):
335338
container_id = "sk-" + str(uuid.uuid4())
336339
style_template = Template(_STYLE)
337340
style_with_id = style_template.substitute(id=container_id)
341+
estimator_str = str(estimator)
342+
343+
# The fallback message is shown by default and loading the CSS sets
344+
# div.sk-text-repr-fallback to display: none to hide the fallback message.
345+
#
346+
# If the notebook is trusted, the CSS is loaded which hides the fallback
347+
# message. If the notebook is not trusted, then the CSS is not loaded and the
348+
# fallback message is shown by default.
349+
#
350+
# The reverse logic applies to HTML repr div.sk-container.
351+
# div.sk-container is hidden by default and the loading the CSS displays it.
352+
fallback_msg = (
353+
"Please rerun this cell to show the HTML repr or trust the notebook."
354+
)
338355
out.write(
339356
f"<style>{style_with_id}</style>"
340357
f'<div id="{container_id}" class"sk-top-container">'
341-
'<div class="sk-container">'
358+
'<div class="sk-text-repr-fallback">'
359+
f"<pre>{html.escape(estimator_str)}</pre><b>{fallback_msg}</b>"
360+
"</div>"
361+
'<div class="sk-container" hidden>'
342362
)
343363
_write_estimator_html(
344364
out,
345365
estimator,
346366
estimator.__class__.__name__,
347-
str(estimator),
367+
estimator_str,
348368
first_call=True,
349369
)
350370
out.write("</div></div>")

sklearn/utils/tests/test_estimator_html_repr.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from contextlib import closing
2+
import html
23
from io import StringIO
34

45
import pytest
@@ -278,3 +279,14 @@ def test_one_estimator_print_change_only(print_changed_only):
278279
pca_repr = str(pca)
279280
html_output = estimator_html_repr(pca)
280281
assert pca_repr in html_output
282+
283+
284+
def test_fallback_exists():
285+
"""Check that repr fallback is in the HTML."""
286+
pca = PCA(n_components=10)
287+
html_output = estimator_html_repr(pca)
288+
289+
assert (
290+
f'<div class="sk-text-repr-fallback"><pre>{html.escape(str(pca))}'
291+
in html_output
292+
)

0 commit comments

Comments
 (0)
0