简体   繁体   中英

How do you nest jupyter widgets?

I am writing a custom Jupyter widget (made from the cookiecutter ) that would ideally have some custom javascript but then also make use of existing widgets as well.

I can't find documentation on nesting widgets. How would I make a custom widget that has parts that are custom javascript, as well as using existing widgets?

Asked too soon. You can use the Box widget (or VBox/HBox). See the example code in the doc string.

Things like,

import ipywidgets as widgets
title_widget = widgets.HTML('<em>Box Example</em>')
slider = widgets.IntSlider()
box1 = widgets.Box([title_widget, slider])
widgets.VBox([box1, box1])

work fine. And if you look at the code in the class,

def __init__(self, children=(), **kwargs):
    kwargs['children'] = children
    super(Box, self).__init__(**kwargs)
    self.on_displayed(Box._fire_children_displayed)

def _fire_children_displayed(self):
    for child in self.children:
        child._handle_displayed()

You can see logic for managing children.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM