Skip to content

added _view_count trait to Widget, to count the number of views #1232

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

Merged
merged 3 commits into from
Apr 4, 2017
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
2 changes: 2 additions & 0 deletions ipywidgets/widgets/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ def get_view_spec(self):
help="Name of the view object.").tag(sync=True)
_view_module_version = Unicode('*',
help="A semver requirement for the view module.").tag(sync=True)
_view_count = Int(read_only=True,
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)
comm = Instance('ipykernel.comm.Comm', allow_none=True)

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)
Expand Down
13 changes: 12 additions & 1 deletion jupyter-js-widgets/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class WidgetModel extends Backbone.Model {
_view_module: "jupyter-js-widgets",
_view_name: null,
_view_module_version: "*",
_view_count: 0,
msg_throttle: 1,
};
}
Expand Down Expand Up @@ -100,7 +101,6 @@ class WidgetModel extends Backbone.Model {
this.state_lock = null;
this._buffered_state_diff = {};


this.views = {};

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

this.options = parameters.options;

this.once('remove', () => {
this.model.set('_view_count', this.model.get('_view_count') - 1);
this.model.save_changes()
});

this.once('displayed', () => {
this.model.set('_view_count', this.model.get('_view_count') + 1);
this.model.save_changes()
});

this.displayed = new Promise((resolve, reject) => {
this.once('displayed', resolve);
});
Expand Down