Skip to content

BUG Adjusts html_repr based on configuration#17093

Merged
adrinjalali merged 6 commits intoscikit-learn:masterfrom
thomasjpfan:use_code_html_sphinx_gallery
May 5, 2020
Merged

BUG Adjusts html_repr based on configuration#17093
adrinjalali merged 6 commits intoscikit-learn:masterfrom
thomasjpfan:use_code_html_sphinx_gallery

Conversation

@thomasjpfan
Copy link
Copy Markdown
Member

_repr_html_ was defined to work to make sphinx-gallery work properly. This PR makes sure the return is just the repr, if the display option is not set.

In a jupyter notebook, the _repr_mimebundle_ will be used so this PR will not have take any effect.

@thomasjpfan
Copy link
Copy Markdown
Member Author

@jnothman @amueller @NicolasHug @ogrisel

This is most likely only used by sphinx gallery. Currently the display configuration option does not do anything in sphinx gallery, because gallery will always should _repr_html_ which is always the graphical HTML representation.

I part of me think this PR is not needed, but I am open to other opinions.

@NicolasHug
Copy link
Copy Markdown
Member

Is there an example were a block ends with an estimator? I don't think we have much of these

I'm fine eitherway. We can always merge this and then decide to use setconfig(display=diagram) in conf.py...

sklearn/base.py Outdated
"""
if get_config()["display"] == 'diagram':
return estimator_html_repr(self)
return f"<code>{html.escape(repr(self))}</code>"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just raise an AttibuteError here, shouldn't it be handled by sphinx gallery etc?

@thomasjpfan
Copy link
Copy Markdown
Member Author

We have some raw output in the gallery, for example on master.

@rth
Copy link
Copy Markdown
Member

rth commented Apr 30, 2020

I see but the <code> </code> is specific to sphinx right? I see it's not.

Can't we detect that we are running inside sphinx via some env variable and otherwise raise AttributeError. I'm just not sure the priority between repr_html and mimetypebundle is correctly implemented in all environements.

Copy link
Copy Markdown
Member

@rth rth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, I think it should be fine. Thanks @thomasjpfan !

@rth
Copy link
Copy Markdown
Member

rth commented Apr 30, 2020

Actually this changes the default repr in jupyter,
Screenshot_2020-04-30 Untitled - Jupyter Notebook

so I think we should raise an attribute error outside of sphinx, instead of trying to reproduce the behaviour without repr_html manually.

@thomasjpfan
Copy link
Copy Markdown
Member Author

I wanted to return an AttributeError but this happens:

Screen Shot 2020-04-30 at 5 19 42 PM

The only way around this is to return None, but it was considered brittle. But in this case we have _repr_mimebundle_, which gets priority, it should be okay.

@thomasjpfan
Copy link
Copy Markdown
Member Author

Updated PR to return None, this should be okay since the _repr_mimebundle_ takes priority in jupyter contexts.

@rth
Copy link
Copy Markdown
Member

rth commented Apr 30, 2020

I wanted to return an AttributeError but this happens:

Cant we do the same hack as in SVC predict_proba of a property that either returns a function or raises AttributeError, that way hasattr(obj, '_repr_html_') would behave consistently.

@thomasjpfan
Copy link
Copy Markdown
Member Author

Cant we do the same hack as in SVC predict_proba of a property that either returns a function or raises AttributeError, that way hasattr(obj, 'repr_html') would behave consistently.

Yes, we can do it here as well. PR updated with the hack.

Copy link
Copy Markdown
Member

@rth rth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks !

@thomasjpfan thomasjpfan added this to the 0.23 milestone May 2, 2020
@thomasjpfan
Copy link
Copy Markdown
Member Author

Looks like this is needed even in jupyter lab, because _repr_mimebundle_ is not taking priority.

Copy link
Copy Markdown
Member

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few suggestions but other than that +1 on my side as well.

sklearn/base.py Outdated
"""HTML representation of estimator.

Used to display the HTML representation in sphinx-gallery.
Jupyer kernels will use `_repr_mimebundle_`.
Copy link
Copy Markdown
Member

@ogrisel ogrisel May 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo:

Suggested change
Jupyer kernels will use `_repr_mimebundle_`.
Jupyter kernels will use `_repr_mimebundle_`.

and apparently this is wrong based on your last comment. I am not sure if _repr_mimebundle_ will ever take the priority. Maybe we can just state:

This is redundant with the logic of _repr_mimebundle_. The latter should be favored in the long term, _repr_html_ is only implemented for consumers who do not interpret _repr_mimebundle_.


return out

@property
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment to explain that @property is necessary to make hasattr(estimator, "_repr_html_") return True or False depending on get_config()["display"] .

sklearn/base.py Outdated
raise AttributeError("_repr_html_ is only defined when the "
"'display' configuration option is set to "
"'diagram'")
return self.__repr_html_
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the use of leading dunder intentional? I find the behavior of private name mangling potentially confusing. Maybe self._repr_html_inner would be more explicit?

Copy link
Copy Markdown
Member

@adrinjalali adrinjalali left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @thomasjpfan

@adrinjalali
Copy link
Copy Markdown
Member

Seems like all other comments are addressed as well, merging.

@adrinjalali adrinjalali merged commit f23b940 into scikit-learn:master May 5, 2020
adrinjalali pushed a commit to adrinjalali/scikit-learn that referenced this pull request May 5, 2020
* ENH Adjusts html_repr based on configuration

* CLN Returns None instead

* CLN Uses property hack

* CLN Address comments
adrinjalali pushed a commit that referenced this pull request May 5, 2020
* ENH Adjusts html_repr based on configuration

* CLN Returns None instead

* CLN Uses property hack

* CLN Address comments
gio8tisu pushed a commit to gio8tisu/scikit-learn that referenced this pull request May 15, 2020
* ENH Adjusts html_repr based on configuration

* CLN Returns None instead

* CLN Uses property hack

* CLN Address comments
viclafargue pushed a commit to viclafargue/scikit-learn that referenced this pull request Jun 26, 2020
* ENH Adjusts html_repr based on configuration

* CLN Returns None instead

* CLN Uses property hack

* CLN Address comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants