Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add new items to a HBox after being created #1764

Closed
BenjaPH opened this issue Oct 20, 2017 · 5 comments
Closed

How to add new items to a HBox after being created #1764

BenjaPH opened this issue Oct 20, 2017 · 5 comments
Labels
resolved-locked Closed issues are locked after 30 days inactivity. Please open a new issue for related discussion.
Milestone

Comments

@BenjaPH
Copy link

BenjaPH commented Oct 20, 2017

What would be the best way to add new items to an HBox once again being instantiated and displayed.

import ipywidgets as widgets
from IPython.display import display

HeaderLabels = ['Column-1', 'Column-2', 'Column-3']    
HeaderWidgets= [widgets.Label(Text) for Text in HeaderLabels]
HorizBox = widgets.HBox(HeaderWidgets)

display(HorizBox)

After some selection in another widget it is needed to add a new label to the HBox.
The only way I have found to do this is (in the change value event called function) to close it and recreate a new one to be displayed. This is,

NewHeaderWidgets=list(HorizBox.children)
HorizBox.close()
NewLabelWidget=widgets.Label('New Column')
NewHeaderWidgets.append(NewLabelWidget)
NewHorizBox = widgets.HBox(NewHeaderWidgets)
display(NewHorizBox, , display_id="1")

This works, but there is another straightforward method to do that?
I hope to find a method like

HorizBox.children.append(NewLabelWidget) # but it is an inmutable tuple an raise an error

also i have tried to update the display, but the output header is duplicated (the old one is not erased)

HorizBox.children=tuple(list(HorizBox.children).append(widgets.Label('New Column')))
update_display(NewHorizBox , display_id="1")

It is possible to redisplay the HBox representation with the update in the same cell?

@jasongrout
Copy link
Member

jasongrout commented Oct 20, 2017

NewLabelWidget = widgets.Label('New Column')
HorizBox.children += (NewLabelWidget,)

@jasongrout
Copy link
Member

You don't need to redisplay, you just need to set the children to a new tuple containing the original items and the new one. You can add two tuples together to create a new tuple: (1,2,3)+(4,) creates (1,2,3,4). The += is just a shortcut for HorizBox.children = HorizBox.children + (NewLabelWidget,).

@jasongrout jasongrout added this to the Reference milestone Oct 20, 2017
@jasongrout
Copy link
Member

If you preferred, in python 3 you can also use a splatting syntax to create the new tuple: HorizBox.children = (*HorizBox.children, NewLabelWidget)

@BenjaPH
Copy link
Author

BenjaPH commented Oct 23, 2017

Thanks a lot @jasongrout; not only for the correct solution but also for the different ways to do that and its explanations.
I am a newbie in python and all the information is welcome.

@jasongrout
Copy link
Member

Great! I'm closing this issue as answered, but feel free to open other issues for other questions, or ask on our gitter channel, etc.

@github-actions github-actions bot added the resolved-locked Closed issues are locked after 30 days inactivity. Please open a new issue for related discussion. label Feb 9, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
resolved-locked Closed issues are locked after 30 days inactivity. Please open a new issue for related discussion.
Projects
None yet
Development

No branches or pull requests

2 participants