Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whats_new/v1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ Changelog
:pr:`20880` by :user:`Guillaume Lemaitre <glemaitre>`
and :user:`András Simon <simonandras>`.

- |Enhancement| :func:`utils.estimator_html_repr` shows a more helpful error
message when running in a jupyter notebook that is not trusted. :pr:`21316`
by `Thomas Fan`_.

Code and Documentation Contributors
-----------------------------------

Expand Down
24 changes: 22 additions & 2 deletions sklearn/utils/_estimator_html_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,9 @@ def _write_estimator_html(
display: inline-block;
position: relative;
}
#$id div.sk-text-repr-fallback {
display: none;
}
""".replace(
" ", ""
).replace(
Expand All @@ -335,16 +338,33 @@ def estimator_html_repr(estimator):
container_id = "sk-" + str(uuid.uuid4())
style_template = Template(_STYLE)
style_with_id = style_template.substitute(id=container_id)
estimator_str = str(estimator)

# The fallback message is shown by default and loading the CSS sets
# div.sk-text-repr-fallback to display: none to hide the fallback message.
#
# If the notebook is trusted, the CSS is loaded which hides the fallback
# message. If the notebook is not trusted, then the CSS is not loaded and the
# fallback message is shown by default.
#
# The reverse logic applies to HTML repr div.sk-container.
# div.sk-container is hidden by default and the loading the CSS displays it.
fallback_msg = (
"Please rerun this cell to show the HTML repr or trust the notebook."
)
out.write(
f"<style>{style_with_id}</style>"
f'<div id="{container_id}" class"sk-top-container">'
'<div class="sk-container">'
'<div class="sk-text-repr-fallback">'
f"<pre>{html.escape(estimator_str)}</pre><b>{fallback_msg}</b>"
"</div>"
'<div class="sk-container" hidden>'
)
_write_estimator_html(
out,
estimator,
estimator.__class__.__name__,
str(estimator),
estimator_str,
first_call=True,
)
out.write("</div></div>")
Expand Down
12 changes: 12 additions & 0 deletions sklearn/utils/tests/test_estimator_html_repr.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from contextlib import closing
import html
from io import StringIO

import pytest
Expand Down Expand Up @@ -278,3 +279,14 @@ def test_one_estimator_print_change_only(print_changed_only):
pca_repr = str(pca)
html_output = estimator_html_repr(pca)
assert pca_repr in html_output


def test_fallback_exists():
"""Check that repr fallback is in the HTML."""
pca = PCA(n_components=10)
html_output = estimator_html_repr(pca)

assert (
f'<div class="sk-text-repr-fallback"><pre>{html.escape(str(pca))}'
in html_output
)