0

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?

1 Answer 1

3

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"...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.