1

If I just construct a pandas DataFrame in a Jupyter notebook, then the output looks nice:

frame = pd.DataFrame({'a': [1,2,3]})
frame

enter image description here

However, if I make my own class whose repr is a pandas.DataFrame, then that formatting is lost:

class Foo:
    def __init__(self, frame, bar):
        self.frame = frame
        self.bar = bar
    
    def __repr__(self):
        return repr(self.frame)

enter image description here

How can I define the repr of my class Foo, such that when I display it in a Jupyter Notebook, it'll look just like it would if it was a pandas.DataFrame?

ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65

1 Answers1

1

The key resource you seek for guidance in making your class integrate well with Jupyter is covered under _repr_mimebundle_ in the 'Integrating your objects with IPython¶' section in the documentation.

I'm trying to look at where Pandas encodes it so that you can match but not finding quickly right now. (Some of it seems here but it's more complex than the latex example I point to below because of css and more.)

See another example of use of _repr_mimebundle_ here.

Wayne
  • 6,607
  • 8
  • 36
  • 93