In IPython you can have alternative representations of the an object, as seen here. However I was unable to find a bare bones example of an implementation of the alternative display logic. Obviously you can do more with it than what I show here but I thought a simple demonstration would be useful.
For some background, a $\text{__repr__}$ method in Python will return a text representation of an object. Since everything is an object in Python this is how Python returns output when displaying a string object like 'Hello world' e.g. "Hello world" is returned or for an int object like 5, "5" is returned. IPython extends this idea to more than mere text representations to include SVG, JPG, PNG, LaTex, HTML, and JSON representations. This is how, for instance, a pandas dataframe can be displayed as an HTML table. IPython's display methods are used to display all rich representations in a Notebook.
There is a hierarchy of display order, which means if IPython can return a richer display format it will pick the highest in the hierarchy. The lowest is plain text, followed by JSON, PNG, JPEG, SVG, LaTeX, and finally HTML. However, this display order can be overridden by using a specific display method, display_PNG for when an object has both a PNG and a LaTex repr methods.
The following is a simple example showing some repr methods followed by an example using an override call.
class test(object):
def __init__(self):
pass
def __repr__(self):
"Lowest in the display order"
txt = "2 + 2 = 4"
return txt
# def _repr_png_(self):
# "Higher than text in the display order"
# with open("PNG_doc.resized.png",'rb') as f:
# txt = f.read()
# return txt
# def _repr_svg_(self):
# "Higher then text in the display order"
# txt = """<svg width="100" height="100">
# <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
# </svg>"""
# return txt
# def _repr_latex_(self):
# "Second highest in the display order"
# txt = "$$x^2 = 4$$"
# return txt
# def _repr_html_(self):
# "Highest in the display order"
# txt = "<strong>What?</strong>"
# return txt
T = test()
T
2 + 2 = 4
# In order to make this executable on any other computer, I need to grab a
# remote png file.
from urllib.request import urlretrieve
urlretrieve("http://i.imgur.com/aTxezXX.png", "PNG_doc.resized.png")
('PNG_doc.resized.png', <http.client.HTTPMessage at 0x7f555cc62320>)
class test(object):
def __init__(self):
pass
def __repr__(self):
"Lowest in the display order"
txt = "2 + 2 = 4"
return txt
def _repr_png_(self):
"Higher than text in the display order"
with open("PNG_doc.resized.png",'rb') as f:
txt = f.read()
return txt
# def _repr_svg_(self):
# "Higher then text in the display order"
# txt = """<svg width="100" height="100">
# <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
# </svg>"""
# return txt
# def _repr_latex_(self):
# "Second highest in the display order"
# txt = "$$x^2 = 4$$"
# return txt
# def _repr_html_(self):
# "Highest in the display order"
# txt = "<strong>What?</strong>"
# return txt
T = test()
T
class test(object):
def __init__(self):
pass
def __repr__(self):
"Lowest in the display order"
txt = "2 + 2 = 4"
return txt
def _repr_png_(self):
"Higher than text in the display order"
with open("PNG_doc.resized.png",'rb') as f:
txt = f.read()
return txt
def _repr_svg_(self):
"Higher then text in the display order"
txt = """<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>"""
return txt
# def _repr_latex_(self):
# "Second highest in the display order"
# txt = "$$x^2 = 4$$"
# return txt
# def _repr_html_(self):
# "Highest in the display order"
# txt = "<strong>What?</strong>"
# return txt
T = test()
T
class test(object):
def __init__(self):
pass
def __repr__(self):
"Lowest in the display order"
txt = "2 + 2 = 4"
return txt
def _repr_png_(self):
"Higher than text in the display order"
with open("PNG_doc.resized.png",'rb') as f:
txt = f.read()
return txt
def _repr_svg_(self):
"Higher then text in the display order"
txt = """<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>"""
return txt
def _repr_latex_(self):
"Second highest in the display order"
txt = r"$$x^2 = \frac{16}{4}$$"
return txt
# def _repr_html_(self):
# "Highest in the display order"
# txt = "<strong>What?</strong>"
# return txt
T = test()
T
Wait... that the SVG repr again! What's up?
This is a small bug that affects nbviewer that is going to be fixed in the next release. In the Notebook this appears as,
$$x^2 = \frac{16}{4}$$Be patient; January is almost here.
class test(object):
def __init__(self):
pass
def __repr__(self):
"Lowest in the display order"
txt = "2 + 2 = 4"
return txt
def _repr_png_(self):
"Higher than text in the display order"
with open("PNG_doc.resized.png",'rb') as f:
txt = f.read()
return txt
def _repr_svg_(self):
"Higher then text in the display order"
txt = """<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>"""
return txt
def _repr_latex_(self):
"Second highest in the display order"
txt = "$$x^2 = 4$$"
return txt
def _repr_html_(self):
"Highest in the display order"
txt = ('<strong style="color:red">What?</strong><strong> .... </strong>'
'<strong style="color:green">HTML is really the tops!</strong>')
return txt
T = test()
T
If you have a particular representation that you need to be displayed instead of the default highest repr method available, you can override the display order with IPython's display display_... method.
from IPython.display import display_svg
display_svg(T)
If you look at the raw JSON code of the Notebook file you can see all the representations of an object stored with the object. This has an effect on the Notebook's file size and separate image representations of a frequently used object can make a substantial impact on the portability of a Notebook.