-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Notebook calls both __repr__ and _repr_html_ #9771
Copy link
Copy link
Closed
Milestone
Description
Observed Behavior:
- Define
_repr_html_on class - In Jupyter/IPython notebook create an instance of the class
- Showing the variable by it being the last line in a cell calls both
__repr__and_repr_html_
Expected Behavior:
Jupyter/IPython notebook should call only _repr_html_ if it is defined.
Rationale:
- The return of
repris never shown to the user so there is no need to call it if_repr_html_is defined already. - For object's whose
reprfunction is expensive to call (eg large collections),_repr_html_can be implemented efficiently by only showing a subset of the collection. This is allowed since althoughreprin general should adhere to its output being runnable byeval,_repr_html_does not have to adhere to this since it produces HTML.
Minimal Example
class Custom:
def __repr__(self):
raise Exception("oops called repr")
def _repr_html_(self):
return "<h1>HTML repr</h1>"In jupyter cell:
a = Custom()
aThis results in: https://gist.github.com/EntilZha/6b81fc9ced2540a5b7acdebc37c66346
Note, the HTML output is still printed despite the exception.
System Information
- Mac OS X 10.11.5
- Safari 9
- IPython 5.0.0
- Jupyter 1.0.0
- Python 3 via brew
Thanks!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels