mirror of
https://github.com/jupyter/notebook.git
synced 2025-03-07 13:07:22 +08:00
Make Python push initial state.
Also added initial state push callback.
This commit is contained in:
parent
e7ee9c12b3
commit
2b5ebb9e8f
@ -190,7 +190,7 @@ define([
|
|||||||
return this.create_model({model_name: msg.content.data.target_name, comm: comm});
|
return this.create_model({model_name: msg.content.data.target_name, comm: comm});
|
||||||
};
|
};
|
||||||
|
|
||||||
WidgetManager.prototype.create_model = function (model_name, target_name) {
|
WidgetManager.prototype.create_model = function (model_name, target_name, init_state_callback) {
|
||||||
// Create and return a new widget model.
|
// Create and return a new widget model.
|
||||||
//
|
//
|
||||||
// Parameters
|
// Parameters
|
||||||
@ -199,7 +199,13 @@ define([
|
|||||||
// Target name of the widget model to create.
|
// Target name of the widget model to create.
|
||||||
// target_name: string
|
// target_name: string
|
||||||
// Target name of the widget in the back-end.
|
// Target name of the widget in the back-end.
|
||||||
return this._create_model({model_name: model_name, target_name: target_name});
|
// init_state_callback: (optional) callback
|
||||||
|
// Called when the first state push from the back-end is
|
||||||
|
// recieved.
|
||||||
|
return this._create_model({
|
||||||
|
model_name: model_name,
|
||||||
|
target_name: target_name,
|
||||||
|
init_state_callback: init_state_callback});
|
||||||
};
|
};
|
||||||
|
|
||||||
WidgetManager.prototype._create_model = function (options) {
|
WidgetManager.prototype._create_model = function (options) {
|
||||||
@ -214,6 +220,9 @@ define([
|
|||||||
// target_name: (optional) string
|
// target_name: (optional) string
|
||||||
// Target name of the widget in the back-end.
|
// Target name of the widget in the back-end.
|
||||||
// comm: (optional) Comm
|
// comm: (optional) Comm
|
||||||
|
// init_state_callback: (optional) callback
|
||||||
|
// Called when the first state push from the back-end is
|
||||||
|
// recieved.
|
||||||
|
|
||||||
// Create a comm if it wasn't provided.
|
// Create a comm if it wasn't provided.
|
||||||
var comm = options.comm;
|
var comm = options.comm;
|
||||||
@ -226,8 +235,8 @@ define([
|
|||||||
|
|
||||||
var instantiate_model = function(ModelType) {
|
var instantiate_model = function(ModelType) {
|
||||||
var model_id = comm.comm_id;
|
var model_id = comm.comm_id;
|
||||||
var widget_model = new ModelType(that, model_id, comm);
|
var widget_model = new ModelType(that, model_id, comm, options.init_state_callback);
|
||||||
widget_model.on('comm:close', function () {sss
|
widget_model.on('comm:close', function () {
|
||||||
delete that._models[model_id];
|
delete that._models[model_id];
|
||||||
});
|
});
|
||||||
that._models[model_id] = widget_model;
|
that._models[model_id] = widget_model;
|
||||||
|
@ -9,7 +9,7 @@ define(["widgets/js/manager",
|
|||||||
], function(widgetmanager, _, Backbone, $, IPython){
|
], function(widgetmanager, _, Backbone, $, IPython){
|
||||||
|
|
||||||
var WidgetModel = Backbone.Model.extend({
|
var WidgetModel = Backbone.Model.extend({
|
||||||
constructor: function (widget_manager, model_id, comm) {
|
constructor: function (widget_manager, model_id, comm, init_state_callback) {
|
||||||
// Constructor
|
// Constructor
|
||||||
//
|
//
|
||||||
// Creates a WidgetModel instance.
|
// Creates a WidgetModel instance.
|
||||||
@ -20,7 +20,11 @@ define(["widgets/js/manager",
|
|||||||
// model_id : string
|
// model_id : string
|
||||||
// An ID unique to this model.
|
// An ID unique to this model.
|
||||||
// comm : Comm instance (optional)
|
// comm : Comm instance (optional)
|
||||||
|
// init_state_callback : callback (optional)
|
||||||
|
// Called once when the first state message is recieved from
|
||||||
|
// the back-end.
|
||||||
this.widget_manager = widget_manager;
|
this.widget_manager = widget_manager;
|
||||||
|
this.init_state_callback = init_state_callback;
|
||||||
this._buffered_state_diff = {};
|
this._buffered_state_diff = {};
|
||||||
this.pending_msgs = 0;
|
this.pending_msgs = 0;
|
||||||
this.msg_buffer = null;
|
this.msg_buffer = null;
|
||||||
@ -70,6 +74,10 @@ define(["widgets/js/manager",
|
|||||||
switch (method) {
|
switch (method) {
|
||||||
case 'update':
|
case 'update':
|
||||||
this.set_state(msg.content.data.state);
|
this.set_state(msg.content.data.state);
|
||||||
|
if (this.init_state_callback) {
|
||||||
|
this.init_state_callback.apply(this, [this]);
|
||||||
|
this.init_state_callback = null;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'custom':
|
case 'custom':
|
||||||
this.trigger('msg:custom', msg.content.data.content);
|
this.trigger('msg:custom', msg.content.data.content);
|
||||||
|
@ -142,7 +142,7 @@ class Widget(LoggingConfigurable):
|
|||||||
|
|
||||||
Widget._call_widget_constructed(self)
|
Widget._call_widget_constructed(self)
|
||||||
if open_comm:
|
if open_comm:
|
||||||
self.open()
|
self.open()
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
"""Object disposal"""
|
"""Object disposal"""
|
||||||
@ -161,17 +161,18 @@ class Widget(LoggingConfigurable):
|
|||||||
if self._model_id is not None:
|
if self._model_id is not None:
|
||||||
args['comm_id'] = self._model_id
|
args['comm_id'] = self._model_id
|
||||||
self.set_comm(Comm(**args))
|
self.set_comm(Comm(**args))
|
||||||
|
|
||||||
# first update
|
|
||||||
self.send_state()
|
|
||||||
|
|
||||||
def set_comm(self, comm):
|
def set_comm(self, comm):
|
||||||
"""Set's the comm of the widget."""
|
"""Set's the comm of the widget."""
|
||||||
self.comm = comm
|
self.comm = comm
|
||||||
self._model_id = self.model_id
|
self._model_id = self.model_id
|
||||||
|
|
||||||
self.comm.on_msg(self._handle_msg)
|
self.comm.on_msg(self._handle_msg)
|
||||||
Widget.widgets[self.model_id] = self
|
Widget.widgets[self.model_id] = self
|
||||||
|
|
||||||
|
# first update
|
||||||
|
self.send_state()
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def model_id(self):
|
def model_id(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user