From e3b78bbf2eeb26f065848e930ca27f57d1ca4837 Mon Sep 17 00:00:00 2001 From: "Thomas J. Fan" Date: Tue, 12 Oct 2021 15:16:31 -0400 Subject: [PATCH 1/6] ENH Adds nicer message when notebook is not trusted --- sklearn/utils/_estimator_html_repr.py | 14 ++++++++++++-- sklearn/utils/tests/test_estimator_html_repr.py | 12 ++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/sklearn/utils/_estimator_html_repr.py b/sklearn/utils/_estimator_html_repr.py index b2d38b9e97ab3..c01bb3eea9a88 100644 --- a/sklearn/utils/_estimator_html_repr.py +++ b/sklearn/utils/_estimator_html_repr.py @@ -309,6 +309,9 @@ def _write_estimator_html( display: inline-block; position: relative; } +#$id div.sk-text-repr-fallback { + display: none; +} """.replace( " ", "" ).replace( @@ -335,16 +338,23 @@ 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) + rerun_msg = ( + "Please rerun this cell to show the HTML repr or trust the notebook." + ) + fallback_html = f"
{html.escape(estimator_str)}
{rerun_msg}" out.write( f"" f'
' - '
' + f'
{fallback_html}
' + '
") diff --git a/sklearn/utils/tests/test_estimator_html_repr.py b/sklearn/utils/tests/test_estimator_html_repr.py index f22c03f20bdd7..9d474ad10fe10 100644 --- a/sklearn/utils/tests/test_estimator_html_repr.py +++ b/sklearn/utils/tests/test_estimator_html_repr.py @@ -1,4 +1,5 @@ from contextlib import closing +import html from io import StringIO import pytest @@ -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'
{html.escape(str(pca))}'
+        in html_output
+    )

From 6fa3fb3cdb37871ec6412ffe12de0992a4139714 Mon Sep 17 00:00:00 2001
From: "Thomas J. Fan" 
Date: Tue, 12 Oct 2021 15:48:20 -0400
Subject: [PATCH 2/6] DOC Adds whats new

---
 doc/whats_new/v1.1.rst | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/doc/whats_new/v1.1.rst b/doc/whats_new/v1.1.rst
index 1a9f773ce08df..4a9f511ab9c1b 100644
--- a/doc/whats_new/v1.1.rst
+++ b/doc/whats_new/v1.1.rst
@@ -98,6 +98,10 @@ Changelog
   :pr:`20880` by :user:`Guillaume Lemaitre `
   and :user:`András Simon `.
 
+- |Enhancement| :func:`utils.estimator_html_repr` shows a more helpful error
+  message when running in a jupyter notebook that is not trusted. :pr:`xxxxx`
+  by `Thomas Fan`_.
+
 Code and Documentation Contributors
 -----------------------------------
 

From 418836f14d969baf9a61c9a95c1dc99b8c9efbb0 Mon Sep 17 00:00:00 2001
From: "Thomas J. Fan" 
Date: Tue, 12 Oct 2021 16:01:04 -0400
Subject: [PATCH 3/6] DOC Adds PR number

---
 doc/whats_new/v1.1.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/doc/whats_new/v1.1.rst b/doc/whats_new/v1.1.rst
index 4a9f511ab9c1b..2575c53fb705d 100644
--- a/doc/whats_new/v1.1.rst
+++ b/doc/whats_new/v1.1.rst
@@ -99,7 +99,7 @@ Changelog
   and :user:`András Simon `.
 
 - |Enhancement| :func:`utils.estimator_html_repr` shows a more helpful error
-  message when running in a jupyter notebook that is not trusted. :pr:`xxxxx`
+  message when running in a jupyter notebook that is not trusted. :pr:`21316`
   by `Thomas Fan`_.
 
 Code and Documentation Contributors

From 32f3103f7fdf3c9263553dbe34383643e3f534e0 Mon Sep 17 00:00:00 2001
From: "Thomas J. Fan" 
Date: Mon, 18 Oct 2021 09:55:26 -0400
Subject: [PATCH 4/6] DOC Adds comment on how fallback works

---
 sklearn/utils/_estimator_html_repr.py | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/sklearn/utils/_estimator_html_repr.py b/sklearn/utils/_estimator_html_repr.py
index c01bb3eea9a88..49391d0183918 100644
--- a/sklearn/utils/_estimator_html_repr.py
+++ b/sklearn/utils/_estimator_html_repr.py
@@ -338,16 +338,21 @@ 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)
-        rerun_msg = (
+
+        # When the notebook is trusted, the CSS is loaded and
+        # div.sk-text-repr-fallback is set to display: none, hiding the fallback message
+        # If the notebook is not trusted, the CSS is not loaded, then fallback message
+        # is shown by default.
+        fallback_msg = (
             "Please rerun this cell to show the HTML repr or trust the notebook."
         )
-        fallback_html = f"
{html.escape(estimator_str)}
{rerun_msg}" out.write( f"" f'
' - f'
{fallback_html}
' + '
' + f"
{html.escape(estimator_str)}
{fallback_msg}" + "
" '