Appending another widget to an accordion container?

I am new to Jupyter notebooks (obviously). I have been reading docs and burning a lot of time, I just can’t seem to find an ipywidgets example or documentation of how to add to an accordion container.

A couple of docs I have found, a video from 2014, and a website:
https://panel.holoviz.org/reference/layouts/Accordion.html

Firstly, I gather there are three common widget libraries? They ipywidgets, an IPython specific library, and the brokeh library, that are all different? Is this a historical evolution and ipywidgets is now the way to go? Perhaps ipywidgets and the brokey widgets are two separate routes? Does the brokey library use a separate server than the jupyter server?

Given that I start with this:

import ipywidgets as widgets
accordion = widgets.Accordion(children=[widgets.IntSlider(), widgets.Text()], titles=('Slider', 'Text'))
display(accordion)

The read the docs doc,
https://ipywidgets.readthedocs.io/en/7.x/examples/Widget%20List.html?highlight=accordion
has no example of adding a widget.

An IPython widgets doc, and the brokeh docs show examples of using an append method, but accordion (from the code just above) and accodion.children have no append method.

Any pointers appreciated.

children is a immutable tuple: you might be able to extend it with, e.g.

accordion.children = [*accordion.children, IntSlider()] 
2 Likes

@bollwyvl Ok, I see, so there is no method on the container for adding another widget, nor does the container have a list that we may append to.

Hence what we must do is to assign to children a completely new list of contained widgets.

… and here you provide an elegant syntax for creating that new list by spreading the old list along with a new element, then assigning that.

Ok, great, works … I thought that I must have been doing it wrong! LOL

Oh gosh, now I am having problems with the titles.

The example in the docs:

Did not add titles as advertised, but instead resulted in: <Sorry, new users can only put one embedded media item in a post. --JupyterBot>
Screenshot from 2021-11-16 13-08-07|690x198

Am I missing something, or are titles only available through an index given to set/get methods?

Alas, that’s the ipywidgets 8 API, which isn’t quite out… try the stable docs!

2 Likes