-
Notifications
You must be signed in to change notification settings - Fork 947
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
Widgets are not unique when stored in a dict in sub-class #2944
Comments
…ubwidgets were pointed to the same memory location in different instants. For more information see: jupyter-widgets/ipywidgets#2944 Removed unused imports.
I think this is an issue of class instance variables vs class variables. I just read https://stackoverflow.com/questions/45284838/are-the-attributes-in-a-python-class-shared-or-not which I found to be helpful for understanding this. I think this issue is in how you are (or are not) initializing self.widgets. If I change your example to this: import ipywidgets as widgets
class myBox(widgets.HBox):
def __init__(self):
self.widgets = {}
self.widgets['innerbox']=widgets.Text()
super().__init__(children=[self.widgets['innerbox']])
b1 = myBox()
b2 = myBox()
print(id(b1.widgets['innerbox']) == id(b2.widgets['innerbox']))
print(b1.widgets is b2.widgets) then I get the result I think you are expecting.
|
And I think the issue was coming up in this case because ipywidgets/ipywidgets/widgets/widget.py Lines 266 to 267 in 6be18d9
So the solution is either to redefine it for the instance as I did, or use a different variable name. |
I think we should not have used such a common name, or figured out some other way of having that global list. That's a good thing to look at changing for ipywidgets 8. |
Thanks for the quick feedback and fix. My initial motivation for using a As for identifying widgets, I could (didn't) use a convention of prepending In the end I just went with storing them all as unique class attributes/variables and it works fine. This was mostly to draw attention to what was quite a perplexing issue for me, in case someone else had the same (not)bright idea. |
* Added data explore GUI * Changed to local imports * Fixed typo causing recursion * Added explore to automatic imports * Added some limited tests for explore. * Added ipywidgets dependency to setup * Cleaned up imports. Removed redundant return_value_or_empty functions. Fixed bug with ExperimentExplorer __init__ when session not passed. * Fixed some code comments and user facing comments. Changed to using a dict comprehension for formatting info output. * Added minimal tests for ExperimentExplorer. Modified test files for explore to ensure differences betweem two experiments to make tests meaninful. * Forgot to save last change when adding last tests * Removed unused hvplot import * Changed to storing widgets directly in attribute and not in dict as subwidgets were pointed to the same memory location in different instants. For more information see: jupyter-widgets/ipywidgets#2944 Removed unused imports. * White space cleanup. Enforced consistent style across classes.
Should this also be turned into a private attribute? My intuition is yes, but I can't find a firm reason either way. Perhaps yes because otherwise if we rename it something like: |
This may be a python feature rather than an ipywidgets bug, but thought it was worth pointing out in case someone else has a similar issue and can't figure it out.
I am subclassing
HBox
andVBox
to create self-contained "combo-widgets". However, I noticed that when I made independent instances of my combo-widgets, the widgets they contained seemed to be connected. It turns out that because I was storing the widgets in adict
the widgets were pointing to the same address.Here is an MCVE:
Result is
It doesn't happen if I store the inner widget like so
if I give the inner widgets different arguments every time they are instantiated they still end up pointing to the same place
The text was updated successfully, but these errors were encountered: