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

weakref to enable gc + setting children with iterable + fix various tooltips #3921

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docs/source/dev_install.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ To update the widget model specification with changes, do something like this in
```
python ./packages/schema/generate-spec.py -f json-pretty packages/schema/jupyterwidgetmodels.latest.json
python ./packages/schema/generate-spec.py -f markdown packages/schema/jupyterwidgetmodels.latest.md
jlpm prettier
```

## Releasing new versions
Expand Down
1 change: 0 additions & 1 deletion packages/base-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"chai": "^4.0.0",
"chai-as-promised": "^7.0.0",
"expect.js": "^0.3.1",
"istanbul-instrumenter-loader": "^3.0.1",
"karma": "^6.3.3",
"karma-chrome-launcher": "^3.1.0",
"karma-coverage": "^2.0.3",
Expand Down
1 change: 0 additions & 1 deletion packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"chai": "^4.0.0",
"chai-as-promised": "^7.0.0",
"expect.js": "^0.3.1",
"istanbul-instrumenter-loader": "^3.0.1",
"karma": "^6.3.3",
"karma-chrome-launcher": "^3.1.0",
"karma-coverage": "^2.0.3",
Expand Down
8 changes: 4 additions & 4 deletions packages/base/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1101,11 +1101,11 @@ export class DOMWidgetView extends WidgetView {
}

updateTooltip(): void {
const title = this.model.get('tooltip');
if (!title) {
this.el.removeAttribute('title');
} else if (this.model.get('description').length === 0) {
const title = this.model.get('tooltip') ?? this.model.get('description');
if (title) {
this.el.setAttribute('title', title);
} else {
this.el.removeAttribute('title');
}
}

Expand Down
1 change: 0 additions & 1 deletion packages/controls/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
"chai": "^4.0.0",
"css-loader": "^6.5.1",
"expect.js": "^0.3.1",
"istanbul-instrumenter-loader": "^3.0.1",
"karma": "^6.3.3",
"karma-chrome-launcher": "^3.1.0",
"karma-coverage": "^2.0.3",
Expand Down
13 changes: 1 addition & 12 deletions packages/controls/src/widget_bool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ export class CheckboxView extends DescriptionView {
this.descriptionSpan.textContent = description;
}
this.typeset(this.descriptionSpan);
this.descriptionSpan.title = description;
this.checkbox.title = description;
this.updateTooltip();
}

/**
Expand All @@ -180,16 +179,6 @@ export class CheckboxView extends DescriptionView {
}
}

updateTooltip(): void {
if (!this.checkbox) return; // we might be constructing the parent
const title = this.model.get('tooltip');
if (!title) {
this.checkbox.removeAttribute('title');
} else if (this.model.get('description').length === 0) {
this.checkbox.setAttribute('title', title);
}
}

events(): { [e: string]: string } {
return {
'click input[type="checkbox"]': '_handle_click',
Expand Down
5 changes: 0 additions & 5 deletions packages/controls/src/widget_description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ export class DescriptionView extends DOMWidgetView {
}
}

updateTooltip(): void {
if (!this.label) return;
this.label.title = this.model.get('tooltip');
}

label: HTMLLabelElement;
}

Expand Down
3 changes: 3 additions & 0 deletions packages/controls/src/widget_link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,12 @@ export class DirectionalLinkModel extends CoreWidgetModel {
undefined
);
this.stopListening(this.sourceModel, 'destroy', undefined);
this.set('source', [null, '']);
}
if (this.targetModel) {
this.stopListening(this.targetModel, 'destroy', undefined);
this.set('target', [null, '']);
this.save_changes();
}
}

Expand Down
10 changes: 0 additions & 10 deletions packages/controls/src/widget_selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,6 @@ export class SelectionView extends DescriptionView {
}
}

updateTooltip(): void {
if (!this.listbox) return; // we might be constructing the parent
const title = this.model.get('tooltip');
if (!title) {
this.listbox.removeAttribute('title');
} else if (this.model.get('description').length === 0) {
this.listbox.setAttribute('title', title);
}
}

listbox: HTMLSelectElement;
}

Expand Down
20 changes: 0 additions & 20 deletions packages/controls/src/widget_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,16 +378,6 @@ export class TextareaView extends StringView {
}
}

updateTooltip(): void {
if (!this.textbox) return; // we might be constructing the parent
const title = this.model.get('tooltip');
if (!title) {
this.textbox.removeAttribute('title');
} else if (this.model.get('description').length === 0) {
this.textbox.setAttribute('title', title);
}
}

events(): { [e: string]: string } {
return {
'keydown input': 'handleKeyDown',
Expand Down Expand Up @@ -504,16 +494,6 @@ export class TextView extends StringView {
}
}

updateTooltip(): void {
if (!this.textbox) return; // we might be constructing the parent
const title = this.model.get('tooltip');
if (!title) {
this.textbox.removeAttribute('title');
} else if (this.model.get('description').length === 0) {
this.textbox.setAttribute('title', title);
}
}

update(options?: any): void {
/**
* Update the contents of this view
Expand Down
42 changes: 7 additions & 35 deletions packages/schema/jupyterwidgetmodels.latest.json
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,8 @@
{
"default": [],
"help": "List of widget children",
"items": {
"type": "reference",
"widget": "Widget"
},
"name": "children",
"type": "array"
"type": "Children"
},
{
"default": "reference to new instance",
Expand Down Expand Up @@ -935,12 +931,8 @@
{
"default": [],
"help": "List of widget children",
"items": {
"type": "reference",
"widget": "Widget"
},
"name": "children",
"type": "array"
"type": "Children"
},
{
"default": "reference to new instance",
Expand Down Expand Up @@ -3753,12 +3745,8 @@
{
"default": [],
"help": "List of widget children",
"items": {
"type": "reference",
"widget": "Widget"
},
"name": "children",
"type": "array"
"type": "Children"
},
{
"default": "reference to new instance",
Expand Down Expand Up @@ -3850,12 +3838,8 @@
{
"default": [],
"help": "List of widget children",
"items": {
"type": "reference",
"widget": "Widget"
},
"name": "children",
"type": "array"
"type": "Children"
},
{
"default": "reference to new instance",
Expand Down Expand Up @@ -6643,12 +6627,8 @@
{
"default": [],
"help": "List of widget children",
"items": {
"type": "reference",
"widget": "Widget"
},
"name": "children",
"type": "array"
"type": "Children"
},
{
"default": "reference to new instance",
Expand Down Expand Up @@ -6756,12 +6736,8 @@
{
"default": [],
"help": "List of widget children",
"items": {
"type": "reference",
"widget": "Widget"
},
"name": "children",
"type": "array"
"type": "Children"
},
{
"default": "reference to new instance",
Expand Down Expand Up @@ -7914,12 +7890,8 @@
{
"default": [],
"help": "List of widget children",
"items": {
"type": "reference",
"widget": "Widget"
},
"name": "children",
"type": "array"
"type": "Children"
},
{
"default": "reference to new instance",
Expand Down
14 changes: 7 additions & 7 deletions packages/schema/jupyterwidgetmodels.latest.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ that the widget is registered with.
| `_view_module_version` | string | `'2.0.0'` |
| `_view_name` | string | `'AccordionView'` |
| `box_style` | string (one of `'success'`, `'info'`, `'warning'`, `'danger'`, `''`) | `''` | Use a predefined styling for the box. |
| `children` | array of reference to Widget widget | `[]` | List of widget children |
| `children` | Children | `[]` | List of widget children |
| `layout` | reference to Layout widget | reference to new instance |
| `selected_index` | `null` or number (integer) | `null` | The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected. |
| `tabbable` | `null` or boolean | `null` | Is widget tabbable? |
Expand Down Expand Up @@ -163,7 +163,7 @@ that the widget is registered with.
| `_view_module_version` | string | `'2.0.0'` |
| `_view_name` | string | `'BoxView'` |
| `box_style` | string (one of `'success'`, `'info'`, `'warning'`, `'danger'`, `''`) | `''` | Use a predefined styling for the box. |
| `children` | array of reference to Widget widget | `[]` | List of widget children |
| `children` | Children | `[]` | List of widget children |
| `layout` | reference to Layout widget | reference to new instance |
| `tabbable` | `null` or boolean | `null` | Is widget tabbable? |
| `tooltip` | `null` or string | `null` | A tooltip caption. |
Expand Down Expand Up @@ -663,7 +663,7 @@ that the widget is registered with.
| `_view_module_version` | string | `'2.0.0'` |
| `_view_name` | string | `'GridBoxView'` |
| `box_style` | string (one of `'success'`, `'info'`, `'warning'`, `'danger'`, `''`) | `''` | Use a predefined styling for the box. |
| `children` | array of reference to Widget widget | `[]` | List of widget children |
| `children` | Children | `[]` | List of widget children |
| `layout` | reference to Layout widget | reference to new instance |
| `tabbable` | `null` or boolean | `null` | Is widget tabbable? |
| `tooltip` | `null` or string | `null` | A tooltip caption. |
Expand All @@ -680,7 +680,7 @@ that the widget is registered with.
| `_view_module_version` | string | `'2.0.0'` |
| `_view_name` | string | `'HBoxView'` |
| `box_style` | string (one of `'success'`, `'info'`, `'warning'`, `'danger'`, `''`) | `''` | Use a predefined styling for the box. |
| `children` | array of reference to Widget widget | `[]` | List of widget children |
| `children` | Children | `[]` | List of widget children |
| `layout` | reference to Layout widget | reference to new instance |
| `tabbable` | `null` or boolean | `null` | Is widget tabbable? |
| `tooltip` | `null` or string | `null` | A tooltip caption. |
Expand Down Expand Up @@ -1179,7 +1179,7 @@ that the widget is registered with.
| `_view_module_version` | string | `'2.0.0'` |
| `_view_name` | string | `'StackView'` |
| `box_style` | string (one of `'success'`, `'info'`, `'warning'`, `'danger'`, `''`) | `''` | Use a predefined styling for the box. |
| `children` | array of reference to Widget widget | `[]` | List of widget children |
| `children` | Children | `[]` | List of widget children |
| `layout` | reference to Layout widget | reference to new instance |
| `selected_index` | `null` or number (integer) | `null` | The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected. |
| `tabbable` | `null` or boolean | `null` | Is widget tabbable? |
Expand All @@ -1198,7 +1198,7 @@ that the widget is registered with.
| `_view_module_version` | string | `'2.0.0'` |
| `_view_name` | string | `'TabView'` |
| `box_style` | string (one of `'success'`, `'info'`, `'warning'`, `'danger'`, `''`) | `''` | Use a predefined styling for the box. |
| `children` | array of reference to Widget widget | `[]` | List of widget children |
| `children` | Children | `[]` | List of widget children |
| `layout` | reference to Layout widget | reference to new instance |
| `selected_index` | `null` or number (integer) | `null` | The index of the selected page. This is either an integer selecting a particular sub-widget, or None to have no widgets selected. |
| `tabbable` | `null` or boolean | `null` | Is widget tabbable? |
Expand Down Expand Up @@ -1402,7 +1402,7 @@ that the widget is registered with.
| `_view_module_version` | string | `'2.0.0'` |
| `_view_name` | string | `'VBoxView'` |
| `box_style` | string (one of `'success'`, `'info'`, `'warning'`, `'danger'`, `''`) | `''` | Use a predefined styling for the box. |
| `children` | array of reference to Widget widget | `[]` | List of widget children |
| `children` | Children | `[]` | List of widget children |
| `layout` | reference to Layout widget | reference to new instance |
| `tabbable` | `null` or boolean | `null` | Is widget tabbable? |
| `tooltip` | `null` or string | `null` | A tooltip caption. |
Expand Down
6 changes: 3 additions & 3 deletions python/ipywidgets/ipywidgets/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import json
import re
from .widgets import Widget, DOMWidget, widget as widget_module
from .widgets import Widget, DOMWidget
from .widgets.widget_link import Link
from .widgets.docutils import doc_subst
from ._version import __html_manager_version__
Expand Down 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_module._instances.items(): # go over all widgets
for widget_id, widget in Widget._instances.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_module._instances.values() if isinstance(w, DOMWidget)]
views = [w for w in Widget._instances.values() if isinstance(w, DOMWidget)]
else:
try:
views[0]
Expand Down
4 changes: 2 additions & 2 deletions python/ipywidgets/ipywidgets/tests/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import traitlets

from ..widgets import IntSlider, IntText, Text, Widget, jslink, HBox, widget_serialization, widget as widget_module
from ..widgets import IntSlider, IntText, Text, Widget, jslink, HBox, widget_serialization
from ..embed import embed_data, embed_snippet, embed_minimal_html, dependency_state


Expand All @@ -29,7 +29,7 @@ class CaseWidget(Widget):
class TestEmbed:

def teardown(self):
for w in tuple(widget_module._instances.values()):
for w in tuple(Widget._instances.values()):
w.close()

def test_embed_data_simple(self):
Expand Down
2 changes: 1 addition & 1 deletion python/ipywidgets/ipywidgets/widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.

from .widget import Widget, CallbackDispatcher, register, widget_serialization
from .widget import Widget, CallbackDispatcher, register, widget_serialization, enable_weakreference, disable_weakreference
from .domwidget import DOMWidget
from .valuewidget import ValueWidget

Expand Down
5 changes: 2 additions & 3 deletions python/ipywidgets/ipywidgets/widgets/tests/test_send_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

from ..._version import __control_protocol_version__


# A widget with simple traits
class SimpleWidget(Widget):
a = Bool().tag(sync=True)
b = Tuple(Bool(), Bool(), Bool(), default_value=(False, False, False)).tag(
sync=True
)
b = Tuple(Bool(), Bool(), Bool(), default_value=(False, False, False)).tag(sync=True)
c = List(Bool()).tag(sync=True)


Expand Down
Loading