Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH add HTML repr in notebooks for Card #372

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <lazarust>`.
- Add HTML representation of the :class:`skops.card.Card` instances in Jupyter
notebooks. :pr:`372` by `Adrin Jalali`_.

v0.7
----
Expand Down
2 changes: 2 additions & 0 deletions skops/_min_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}


Expand Down
30 changes: 30 additions & 0 deletions skops/card/_model_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import joblib
from huggingface_hub import ModelCardData
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
Expand Down Expand Up @@ -1477,6 +1478,35 @@ 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 _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],
Expand Down