MRocklin
MRocklin

Reputation: 57311

Display encapsulated widget in IPython notebook

I have an object that encapsulates a widget.

class Foo(object):
    def __init__(self, widget):
        self.widget = widget

I want this object to display itself as this widget in the notebook.

>>> my_widget
<fancy widget>

>>> Foo(my_widget)
<fancy widget>

Is there a method that I should implement on my Foo class to accomplish this?

Upvotes: 0

Views: 163

Answers (1)

Jonathan
Jonathan

Reputation: 157

ipywidgets piggy backs onto IPython's rich display system. _ipython_display_ is a hook in the system that allows you to intercept display calls. Try the following:

    class Foo(object):
        def __init__(self, widget):
            self.widget = widget

        def _ipython_display_(self, **kwargs):
            self.widget._ipython_display_(**kwargs)

Cheers, Jon

Edit: Here's the relevant example file included with ipython. Scroll down to "More complex display"...

Upvotes: 3

Related Questions