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

Privatize Widgets.widget #3122

Merged
merged 2 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ipywidgets/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def _get_recursive_state(widget, store=None, drop_defaults=False):

def add_resolved_links(store, drop_defaults):
"""Adds the state of any link models between two models in store"""
for widget_id, widget in Widget.widgets.items(): # go over all widgets
for widget_id, widget in Widget._active_widgets.items(): # go over all widgets
if isinstance(widget, Link) and widget_id not in store:
if widget.source[0].model_id in store and widget.target[0].model_id in store:
store[widget.model_id] = widget._get_embed_state(drop_defaults=drop_defaults)
Expand Down Expand Up @@ -207,7 +207,7 @@ def embed_data(views, drop_defaults=True, state=None):
view_specs: a list of widget view specs
"""
if views is None:
views = [w for w in Widget.widgets.values() if isinstance(w, DOMWidget)]
views = [w for w in Widget._active_widgets.values() if isinstance(w, DOMWidget)]
else:
try:
views[0]
Expand Down
2 changes: 1 addition & 1 deletion ipywidgets/tests/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CaseWidget(Widget):
class TestEmbed:

def teardown(self):
for w in tuple(Widget.widgets.values()):
for w in tuple(Widget._active_widgets.values()):
w.close()

def test_embed_data_simple(self):
Expand Down
22 changes: 11 additions & 11 deletions ipywidgets/widgets/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def _json_to_widget(x, obj):
return {k: _json_to_widget(v, obj) for k, v in x.items()}
elif isinstance(x, (list, tuple)):
return [_json_to_widget(v, obj) for v in x]
elif isinstance(x, str) and x.startswith('IPY_MODEL_') and x[10:] in Widget.widgets:
return Widget.widgets[x[10:]]
elif isinstance(x, str) and x.startswith('IPY_MODEL_') and x[10:] in Widget._active_widgets:
return Widget._active_widgets[x[10:]]
else:
return x

Expand Down Expand Up @@ -247,7 +247,7 @@ def items(self):
def register(widget):
"""A decorator registering a widget class in the widget registry."""
w = widget.class_traits()
Widget.widget_types.register(w['_model_module'].default_value,
Widget._widget_types.register(w['_model_module'].default_value,
w['_model_module_version'].default_value,
w['_model_name'].default_value,
w['_view_module'].default_value,
Expand All @@ -263,11 +263,11 @@ class Widget(LoggingHasTraits):
#-------------------------------------------------------------------------
_widget_construction_callback = None

# widgets is a dictionary of all active widget objects
widgets = {}
# _active_widgets is a dictionary of all active widget objects
_active_widgets = {}

# widget_types is a registry of widgets by module, version, and name:
widget_types = WidgetRegistry()
# _widget_types is a registry of widgets by module, version, and name:
_widget_types = WidgetRegistry()

@classmethod
def close_all(cls):
Expand Down Expand Up @@ -299,7 +299,7 @@ def handle_comm_opened(comm, msg):
state = data['state']

# Find the widget class to instantiate in the registered widgets
widget_class = Widget.widget_types.get(state['_model_module'],
widget_class = Widget._widget_types.get(state['_model_module'],
state['_model_module_version'],
state['_model_name'],
state['_view_module'],
Expand All @@ -320,7 +320,7 @@ def get_manager_state(drop_defaults=False, widgets=None):
"""
state = {}
if widgets is None:
widgets = Widget.widgets.values()
widgets = Widget._active_widgets.values()
for widget in widgets:
state[widget.model_id] = widget._get_embed_state(drop_defaults=drop_defaults)
return {'version_major': 2, 'version_minor': 0, 'state': state}
Expand Down Expand Up @@ -416,7 +416,7 @@ def _comm_changed(self, change):
self._model_id = self.model_id

self.comm.on_msg(self._handle_msg)
Widget.widgets[self.model_id] = self
Widget._active_widgets[self.model_id] = self

@property
def model_id(self):
Expand All @@ -436,7 +436,7 @@ def close(self):
When the comm is closed, all of the widget views are automatically
removed from the front-end."""
if self.comm is not None:
Widget.widgets.pop(self.model_id, None)
Widget._active_widgets.pop(self.model_id, None)
self.comm.close()
self.comm = None
self._repr_mimebundle_ = None
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/generate-spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def create_markdown(spec):
args = parser.parse_args()
format = args.format

widgets_to_document = sorted(widgets.Widget.widget_types.items())
widgets_to_document = sorted(widgets.Widget._widget_types.items())
spec = create_spec(widgets_to_document)
if format == 'json':
print(json.dumps(spec, sort_keys=True))
Expand Down