Search code examples
pandasgetattrrepr

What pandas DataFrame method tells ipython notebook to display as HTML


I have created a class in which the main deliverable piece of data is stored in an attribute as a pandas DataFrame. I'd like the default display behavior of instances of this class to be the same as that of this DataFrame. Particularly when in iPython Notebook.

for example:

from pandas import DataFrame


class TestDFDisplay():
    def __init__(self):
        self.dataframe = DataFrame([[1, 2], [3, 4]])


tdf = TestDFDisplay()

when I:

tdf.dataframe

I get an HTML version of:

   0  1
0  1  2
1  3  4

when I:

tdf

I get:

<__main__.TestDFDisplay instance at 0x000000001A836788>

I'd rather get the same HTML of:

   0  1
0  1  2
1  3  4

Instead, I could:

from pandas import DataFrame


class TestDFDisplay():
    def __init__(self):
        self.dataframe = DataFrame([[1, 2], [3, 4]])

    def __getattr__(self, item):
        try:
            return object.__getattribute__(self, item)
        except AttributeError:
            try:
                return getattr(self.dataframe, item)
            except:
                raise AttributeError


tdf = TestDFDisplay()

but this is a very heavy handed way of diverting any and every attempt to get an attribute from the class instance to attempting to get it from the DataFrame. This works, but I'd rather be more precise and do some like the following:

from pandas import DataFrame


class TestDFDisplay():
    def __init__(self):
        self.dataframe = DataFrame([[1, 2], [3, 4]])

    def __repr__(self):
        return self.dataframe.__repr__()


tdf = TestDFDisplay()

So when I:

tdf

I get the text version of (which is the same as displayed here) and not the HTML version which I want:

   0  1
0  1  2
1  3  4

That's ok. That just means that the 'repr' method wasn't the correct method that's being called on the DataFrame in order to display the HTML in iPython Notebook.

My question is: What is the correct method I should be redirecting at the DataFrame?


Solution

  • You want the _repr_html_ It's what IPython/Jupyter looks for when using the rich (HTML) display system.

    So in your class

    class TestDFDisplay():
        def __init__(self):
            self.dataframe = DataFrame([[1, 2], [3, 4]])
    
        def _repr_html_(self):
            return self.dataframe._repr_html_()
    

    should work.