mirror of
https://github.com/jupyter/notebook.git
synced 2025-02-05 12:19:58 +08:00
Enable widget instanciation from front-end.
This commit is contained in:
parent
b26706ba69
commit
e7ee9c12b3
@ -187,12 +187,47 @@ define([
|
|||||||
|
|
||||||
WidgetManager.prototype._handle_comm_open = function (comm, msg) {
|
WidgetManager.prototype._handle_comm_open = function (comm, msg) {
|
||||||
// Handle when a comm is opened.
|
// Handle when a comm is opened.
|
||||||
|
return this.create_model({model_name: msg.content.data.target_name, comm: comm});
|
||||||
|
};
|
||||||
|
|
||||||
|
WidgetManager.prototype.create_model = function (model_name, target_name) {
|
||||||
|
// Create and return a new widget model.
|
||||||
|
//
|
||||||
|
// Parameters
|
||||||
|
// ----------
|
||||||
|
// model_name: string
|
||||||
|
// Target name of the widget model to create.
|
||||||
|
// target_name: string
|
||||||
|
// Target name of the widget in the back-end.
|
||||||
|
return this._create_model({model_name: model_name, target_name: target_name});
|
||||||
|
};
|
||||||
|
|
||||||
|
WidgetManager.prototype._create_model = function (options) {
|
||||||
|
// Create and return a new widget model.
|
||||||
|
//
|
||||||
|
// Parameters
|
||||||
|
// ----------
|
||||||
|
// options: dictionary
|
||||||
|
// Dictionary of options with the following contents:
|
||||||
|
// model_name: string
|
||||||
|
// Target name of the widget model to create.
|
||||||
|
// target_name: (optional) string
|
||||||
|
// Target name of the widget in the back-end.
|
||||||
|
// comm: (optional) Comm
|
||||||
|
|
||||||
|
// Create a comm if it wasn't provided.
|
||||||
|
var comm = options.comm;
|
||||||
|
if (!comm) {
|
||||||
|
comm = this.comm_manager.new_comm('ipython.widget', {'target_name': options.target_name});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create and return a new model that is connected to the comm.
|
||||||
var that = this;
|
var that = this;
|
||||||
|
|
||||||
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);
|
||||||
widget_model.on('comm:close', function () {
|
widget_model.on('comm:close', function () {sss
|
||||||
delete that._models[model_id];
|
delete that._models[model_id];
|
||||||
});
|
});
|
||||||
that._models[model_id] = widget_model;
|
that._models[model_id] = widget_model;
|
||||||
|
@ -18,6 +18,7 @@ import collections
|
|||||||
from IPython.core.getipython import get_ipython
|
from IPython.core.getipython import get_ipython
|
||||||
from IPython.kernel.comm import Comm
|
from IPython.kernel.comm import Comm
|
||||||
from IPython.config import LoggingConfigurable
|
from IPython.config import LoggingConfigurable
|
||||||
|
from IPython.utils.importstring import import_item
|
||||||
from IPython.utils.traitlets import Unicode, Dict, Instance, Bool, List, \
|
from IPython.utils.traitlets import Unicode, Dict, Instance, Bool, List, \
|
||||||
CaselessStrEnum, Tuple, CUnicode, Int, Set
|
CaselessStrEnum, Tuple, CUnicode, Int, Set
|
||||||
from IPython.utils.py3compat import string_types
|
from IPython.utils.py3compat import string_types
|
||||||
@ -95,6 +96,15 @@ class Widget(LoggingConfigurable):
|
|||||||
if Widget._widget_construction_callback is not None and callable(Widget._widget_construction_callback):
|
if Widget._widget_construction_callback is not None and callable(Widget._widget_construction_callback):
|
||||||
Widget._widget_construction_callback(widget)
|
Widget._widget_construction_callback(widget)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def handle_comm_opened(comm, msg):
|
||||||
|
"""Static method, called when a widget is constructed."""
|
||||||
|
target_name = msg['content']['data']['target_name']
|
||||||
|
widget_class = import_item(target_name)
|
||||||
|
widget = widget_class(open_comm=False)
|
||||||
|
widget.set_comm(comm)
|
||||||
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
# Traits
|
# Traits
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
@ -125,13 +135,14 @@ class Widget(LoggingConfigurable):
|
|||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
# (Con/de)structor
|
# (Con/de)structor
|
||||||
#-------------------------------------------------------------------------
|
#-------------------------------------------------------------------------
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, open_comm=True, **kwargs):
|
||||||
"""Public constructor"""
|
"""Public constructor"""
|
||||||
self._model_id = kwargs.pop('model_id', None)
|
self._model_id = kwargs.pop('model_id', None)
|
||||||
super(Widget, self).__init__(**kwargs)
|
super(Widget, self).__init__(**kwargs)
|
||||||
|
|
||||||
Widget._call_widget_constructed(self)
|
Widget._call_widget_constructed(self)
|
||||||
self.open()
|
if open_comm:
|
||||||
|
self.open()
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
"""Object disposal"""
|
"""Object disposal"""
|
||||||
@ -149,15 +160,19 @@ class Widget(LoggingConfigurable):
|
|||||||
'model_module': self._model_module})
|
'model_module': self._model_module})
|
||||||
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.comm = Comm(**args)
|
self.set_comm(Comm(**args))
|
||||||
self._model_id = self.model_id
|
|
||||||
|
|
||||||
self.comm.on_msg(self._handle_msg)
|
|
||||||
Widget.widgets[self.model_id] = self
|
|
||||||
|
|
||||||
# first update
|
# first update
|
||||||
self.send_state()
|
self.send_state()
|
||||||
|
|
||||||
|
def set_comm(self, comm):
|
||||||
|
"""Set's the comm of the widget."""
|
||||||
|
self.comm = comm
|
||||||
|
self._model_id = self.model_id
|
||||||
|
|
||||||
|
self.comm.on_msg(self._handle_msg)
|
||||||
|
Widget.widgets[self.model_id] = self
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def model_id(self):
|
def model_id(self):
|
||||||
"""Gets the model id of this widget.
|
"""Gets the model id of this widget.
|
||||||
@ -330,7 +345,7 @@ class Widget(LoggingConfigurable):
|
|||||||
def _handle_custom_msg(self, content):
|
def _handle_custom_msg(self, content):
|
||||||
"""Called when a custom msg is received."""
|
"""Called when a custom msg is received."""
|
||||||
self._msg_callbacks(self, content)
|
self._msg_callbacks(self, content)
|
||||||
|
|
||||||
def _notify_trait(self, name, old_value, new_value):
|
def _notify_trait(self, name, old_value, new_value):
|
||||||
"""Called when a property has been changed."""
|
"""Called when a property has been changed."""
|
||||||
# Trigger default traitlet callback machinery. This allows any user
|
# Trigger default traitlet callback machinery. This allows any user
|
||||||
@ -341,10 +356,10 @@ class Widget(LoggingConfigurable):
|
|||||||
# Send the state after the user registered callbacks for trait changes
|
# Send the state after the user registered callbacks for trait changes
|
||||||
# have all fired (allows for user to validate values).
|
# have all fired (allows for user to validate values).
|
||||||
if self.comm is not None and name in self.keys:
|
if self.comm is not None and name in self.keys:
|
||||||
# Make sure this isn't information that the front-end just sent us.
|
# Make sure this isn't information that the front-end just sent us.
|
||||||
if self._should_send_property(name, new_value):
|
if self._should_send_property(name, new_value):
|
||||||
# Send new state to front-end
|
# Send new state to front-end
|
||||||
self.send_state(key=name)
|
self.send_state(key=name)
|
||||||
|
|
||||||
def _handle_displayed(self, **kwargs):
|
def _handle_displayed(self, **kwargs):
|
||||||
"""Called when a view has been displayed for this widget instance"""
|
"""Called when a view has been displayed for this widget instance"""
|
||||||
|
Loading…
Reference in New Issue
Block a user