From 444c6d92239205233138a215791cb2de1f39d6bc Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Tue, 20 Jun 2023 17:52:35 +0200 Subject: [PATCH 1/4] ENH add HTML repr in notebooks for Card --- docs/changes.rst | 2 ++ skops/card/_model_card.py | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/docs/changes.rst b/docs/changes.rst index ad0e7afd..9a5e4e39 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -13,6 +13,8 @@ v0.8 ---- - Adds the abillity to set the :attr:`.Section.folded` property when using :meth:`.Card.add`. :pr:`361` by :user:`Thomas Lazarus `. +- Add HTML representation of the :class:`skops.card.Card` instances in Jupyter + notebooks. v0.7 ---- diff --git a/skops/card/_model_card.py b/skops/card/_model_card.py index 9cb1c742..8404c2ee 100644 --- a/skops/card/_model_card.py +++ b/skops/card/_model_card.py @@ -1477,6 +1477,14 @@ def render(self) -> str: """ return "\n".join(self._generate_card()) + def _repr_markdown_(self) -> str: + """Render the model card as markdown in a Jupyter Notebook. + + Jupyter understands this method and if it exists, uses it to render the + markdown. + """ + return self.render() + def _iterate_key_section_content( self, data: dict[str, Section], From 795600e75496b6331d5df518efacffec0d3f935c Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Tue, 20 Jun 2023 17:53:59 +0200 Subject: [PATCH 2/4] add pr number to changelog --- docs/changes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/changes.rst b/docs/changes.rst index 9a5e4e39..0f31eb3e 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -14,7 +14,7 @@ v0.8 - Adds the abillity to set the :attr:`.Section.folded` property when using :meth:`.Card.add`. :pr:`361` by :user:`Thomas Lazarus `. - Add HTML representation of the :class:`skops.card.Card` instances in Jupyter - notebooks. + notebooks. :pr:`372` by `Adrin Jalali`_. v0.7 ---- From 5a3f203640245f9f478cde2b14e966c85269e88b Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Thu, 22 Jun 2023 15:55:10 +0200 Subject: [PATCH 3/4] add _repr_html_ --- skops/_min_dependencies.py | 2 ++ skops/card/_model_card.py | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/skops/_min_dependencies.py b/skops/_min_dependencies.py index d66b554f..caaa33b7 100644 --- a/skops/_min_dependencies.py +++ b/skops/_min_dependencies.py @@ -36,6 +36,8 @@ "catboost": ("1.0", "tests", "python_version < '3.11'"), "fairlearn": ("0.7.0", "docs, tests", None), "rich": ("12", "tests, rich", None), + # Required for the documentation build with sphinx-gallery + "markdown": ("3.4", "docs", None), } diff --git a/skops/card/_model_card.py b/skops/card/_model_card.py index 8404c2ee..59225dbd 100644 --- a/skops/card/_model_card.py +++ b/skops/card/_model_card.py @@ -16,7 +16,7 @@ import joblib from huggingface_hub import ModelCardData -from sklearn.utils import estimator_html_repr +from sklearn.utils import available_if, estimator_html_repr from tabulate import tabulate # type: ignore from skops.card._templates import CONTENT_PLACEHOLDER, SKOPS_TEMPLATE, Templates @@ -1485,6 +1485,27 @@ def _repr_markdown_(self) -> str: """ return self.render() + def _markdown_installed(self): + try: + import markdown # type: ignore[import] # noqa: F401 + + return True + except ImportError: + return False + + @available_if(_markdown_installed) + def _repr_html_(self) -> str: + """Render the model card as HTML in a Jupyter Notebook. + + This is only available if the user has `markdown` package installed. + This is only used in our documentation build, since sphinx-gallery + doesn't support `_repr_markdown_`. In a Jupyter notebook, + `_repr_markdown_` is used. + """ + import markdown + + return markdown.markdown(self.render()) + def _iterate_key_section_content( self, data: dict[str, Section], From 621a47588d1988a439c49699cb0699123b7e2c0e Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Thu, 22 Jun 2023 16:00:54 +0200 Subject: [PATCH 4/4] fix import --- skops/card/_model_card.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/skops/card/_model_card.py b/skops/card/_model_card.py index 59225dbd..1327868e 100644 --- a/skops/card/_model_card.py +++ b/skops/card/_model_card.py @@ -16,7 +16,8 @@ import joblib from huggingface_hub import ModelCardData -from sklearn.utils import available_if, estimator_html_repr +from sklearn.utils import estimator_html_repr +from sklearn.utils.metaestimators import available_if from tabulate import tabulate # type: ignore from skops.card._templates import CONTENT_PLACEHOLDER, SKOPS_TEMPLATE, Templates