-
Notifications
You must be signed in to change notification settings - Fork 104
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
FEA add link for HTML representation #1051
base: main
Are you sure you want to change the base?
Changes from all commits
37cdcb5
c473d92
ce62f69
4887823
b2284b8
5309352
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import itertools | ||
|
||
import sklearn | ||
from sklearn.utils.fixes import parse_version | ||
|
||
sklearn_version = parse_version(sklearn.__version__) | ||
|
||
# TODO: remove when scikit-learn 1.6 is the minimum supported version and only import | ||
# We have this fix due to the following bug: | ||
# https://github.com/scikit-learn/scikit-learn/pull/29774 | ||
if sklearn_version > parse_version("1.6"): | ||
from sklearn.utils._estimator_html_repr import _HTMLDocumentationLinkMixin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIUC we will now have 2 of those in every skrub estimator's parents, one directly and one through the BaseEstimator? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. The one on the left in the inheritance will be the one used. |
||
else: | ||
|
||
class _HTMLDocumentationLinkMixin: | ||
"""Mixin class allowing to generate a link to the API documentation.""" | ||
|
||
_doc_link_module = "sklearn" | ||
_doc_link_url_param_generator = None | ||
|
||
@property | ||
def _doc_link_template(self): | ||
sklearn_version = parse_version(sklearn.__version__) | ||
if sklearn_version.dev is None: | ||
version_url = f"{sklearn_version.major}.{sklearn_version.minor}" | ||
else: | ||
version_url = "dev" | ||
return getattr( | ||
self, | ||
"__doc_link_template", | ||
( | ||
f"https://scikit-learn.org/{version_url}/modules/generated/" | ||
"{estimator_module}.{estimator_name}.html" | ||
), | ||
) | ||
|
||
@_doc_link_template.setter | ||
def _doc_link_template(self, value): | ||
setattr(self, "__doc_link_template", value) | ||
|
||
def _get_doc_link(self): | ||
"""Generates a link to the API documentation for a given estimator. | ||
|
||
This method generates the link to the estimator's documentation page | ||
by using the template defined by the attribute `_doc_link_template`. | ||
|
||
Returns | ||
------- | ||
url : str | ||
The URL to the API documentation for this estimator. If the estimator | ||
does not belong to module `_doc_link_module`, the empty string (i.e. | ||
`""`) is returned. | ||
""" | ||
if self.__class__.__module__.split(".")[0] != self._doc_link_module: | ||
return "" | ||
|
||
if self._doc_link_url_param_generator is None: | ||
estimator_name = self.__class__.__name__ | ||
# Construct the estimator's module name, up to the first private | ||
# submodule. This works because in scikit-learn all public estimators | ||
# are exposed at that level, even if they actually live in a private | ||
# sub-module. | ||
estimator_module = ".".join( | ||
itertools.takewhile( | ||
lambda part: not part.startswith("_"), | ||
self.__class__.__module__.split("."), | ||
) | ||
) | ||
return self._doc_link_template.format( | ||
estimator_module=estimator_module, estimator_name=estimator_name | ||
) | ||
return self._doc_link_template.format( | ||
**self._doc_link_url_param_generator() | ||
) | ||
|
||
|
||
doc_link_template = ( | ||
"https://skrub-data.org/{version}/reference/generated/" | ||
"{estimator_module}.{estimator_name}.html" | ||
) | ||
doc_link_module = "skrub" | ||
|
||
|
||
def doc_link_url_param_generator(estimator): | ||
from skrub import __version__ | ||
|
||
skrub_version = parse_version(__version__) | ||
if skrub_version.dev is None: | ||
version_url = f"{skrub_version.major}.{skrub_version.minor}" | ||
else: | ||
version_url = "dev" | ||
estimator_name = estimator.__class__.__name__ | ||
estimator_module = ".".join( | ||
itertools.takewhile( | ||
lambda part: not part.startswith("_"), | ||
estimator.__class__.__module__.split("."), | ||
) | ||
) | ||
return { | ||
"version": version_url, | ||
"estimator_module": estimator_module, | ||
"estimator_name": estimator_name, | ||
} | ||
|
||
|
||
class _SkrubHTMLDocumentationLinkMixin(_HTMLDocumentationLinkMixin): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. even when the scikit-learn fix is released, we will most likely want this mixin to avoid repeating these 3 attributes everywhere. so in the end all we will have to remove is the so after all I am +1 for this PR |
||
_doc_link_template = doc_link_template | ||
_doc_link_module = doc_link_module | ||
_doc_link_url_param_generator = doc_link_url_param_generator |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we name this module something more explicit like "_sklearn_html_repr_utils" or similar?