Skip to content

Commit 174f525

Browse files
authored
Merge pull request #1232 from maartenbreddels/_view_count
added _view_count trait to Widget, to count the number of views
2 parents adce7cb + 079071d commit 174f525

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

ipywidgets/widgets/widget.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ def get_view_spec(self):
288288
help="Name of the view object.").tag(sync=True)
289289
_view_module_version = Unicode('*',
290290
help="A semver requirement for the view module.").tag(sync=True)
291+
_view_count = Int(read_only=True,
292+
help="EXPERIMENTAL: The number of views of the model displayed in the frontend. This attribute is experimental and may change or be removed in the future.").tag(sync=True)
291293
comm = Instance('ipykernel.comm.Comm', allow_none=True)
292294

293295
msg_throttle = Int(1, help="""Maximum number of msgs the front-end can send before receiving an idle msg from the back-end.""").tag(sync=True)

jupyter-js-widgets/src/widget.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class WidgetModel extends Backbone.Model {
6060
_view_module: "jupyter-js-widgets",
6161
_view_name: null,
6262
_view_module_version: "*",
63+
_view_count: 0,
6364
msg_throttle: 1,
6465
};
6566
}
@@ -100,7 +101,6 @@ class WidgetModel extends Backbone.Model {
100101
this.state_lock = null;
101102
this._buffered_state_diff = {};
102103

103-
104104
this.views = {};
105105

106106
if (comm) {
@@ -623,6 +623,17 @@ abstract class WidgetView extends NativeView<WidgetModel> {
623623
this.listenTo(this.model, 'change', this.update);
624624

625625
this.options = parameters.options;
626+
627+
this.once('remove', () => {
628+
this.model.set('_view_count', this.model.get('_view_count') - 1);
629+
this.model.save_changes()
630+
});
631+
632+
this.once('displayed', () => {
633+
this.model.set('_view_count', this.model.get('_view_count') + 1);
634+
this.model.save_changes()
635+
});
636+
626637
this.displayed = new Promise((resolve, reject) => {
627638
this.once('displayed', resolve);
628639
});

0 commit comments

Comments
 (0)