Skip to content

Commit

Permalink
Update DeclWidgets init API
Browse files Browse the repository at this point in the history
Move main Notebook shim function to dashboards.js

Ref jupyter#133

(c) Copyright IBM Corp. 2016
  • Loading branch information
jhpedemonte committed Mar 10, 2016
1 parent db1dd65 commit d12d9c2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
Binary file modified etc/notebooks/bundled-dashboard.zip
Binary file not shown.
Binary file modified etc/notebooks/taxi-demo.zip
Binary file not shown.
32 changes: 28 additions & 4 deletions public/js/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ requirejs([
// initialize Gridstack
_initGrid();

_initDeclWidgets();

// start a kernel
Kernel.start().then(function(kernel) {
// initialize an ipywidgets manager
var widgetManager = new WidgetManager(kernel, _consumeMessage);
_registerKernelErrorHandler(kernel);

_shimNotebook(kernel, widgetManager);
_initDeclWidgets();

_getCodeCells().each(function() {
var $cell = $(this);

Expand Down Expand Up @@ -133,6 +134,25 @@ requirejs([
$container.removeClass('invisible');
}

// shim Jupyter Notebook objects for backwards compatibility
function _shimNotebook(kernel, widgetManager) {
var ipy = window.IPython = window.IPython || {};
var nb = ipy.notebook = ipy.notebook || {};
nb.base_url = document.baseURI;
nb.events = nb.events || $({});

nb.kernel = kernel;
var KernelStatus = Services.KernelStatus;
nb.kernel.is_connected = function() {
return kernel.status === KernelStatus.Busy || kernel.status === KernelStatus.Idle;
};

nb.kernel.widget_manager = widgetManager;

// kernel has already started
nb.events.trigger('kernel_ready.Kernel');
}

function _initDeclWidgets() {
if (Config.supportsDeclWidgets) {
// construct path relative to notebook, in order to properly configure require.js
Expand All @@ -148,8 +168,12 @@ requirejs([

require(['urth_widgets/js/init/init'], function(DeclWidgets) {
// initialize Declarative Widgets
// NOTE: DeclWidgets adds 'urth_components/...' to this path
DeclWidgets(document.baseURI);
DeclWidgets({
IPython: window.IPython,
events: window.IPython.notebook.events,
WidgetManager: WidgetManager,
WidgetModel: Widgets.WidgetModel
});
});
} else {
console.log('Declarative Widgets not supported ("urth_components" directory not found)');
Expand Down
46 changes: 19 additions & 27 deletions public/js/widget-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ define([
Services
) {

var KernelStatus = Services.KernelStatus;

var WidgetManager = function(kernel, msgHandler) {
var WidgetManager = function(kernel, msgHandler) {
// Call the base class.
Widgets.ManagerBase.call(this);

Expand All @@ -43,12 +41,29 @@ define([
}).bind(this);
validate();

this._shimDeclWidgets(kernel);
this._shimMatplotlib(kernel);
this._shimBokeh(kernel);
};

WidgetManager.prototype = Object.create(Widgets.ManagerBase.prototype);

//--------------------------------------------------------------------
// Class level
//--------------------------------------------------------------------

WidgetManager.register_widget_model = function(model_name, model_type) {
return Widgets.ManagerBase.register_widget_model.apply(this, arguments);
};

WidgetManager.register_widget_view = function(view_name, view_type) {
return Widgets.ManagerBase.register_widget_view.apply(this, arguments);
};


//--------------------------------------------------------------------
// Instance level
//--------------------------------------------------------------------

/*
* Called when a jupyter widget is added to the DOM.
*
Expand Down Expand Up @@ -176,28 +191,6 @@ define([
* DECLARATIVE WIDGETS SHIMS
**/

function notebookShim() {
var ipy = window.IPython = window.IPython || {};
var nb = ipy.notebook = ipy.notebook || {};
return nb;
}

WidgetManager.prototype._shimDeclWidgets = function(kernel) {
var nb = notebookShim();
nb.events = nb.events || $({});

nb.kernel = kernel;
nb.kernel.is_connected = function() {
return kernel.status === KernelStatus.Busy || kernel.status === KernelStatus.Idle;
};
nb.kernel.widget_manager = this;

// IPython.notebook.base_url ?????

// WidgetManager is instantiated after creation of a kernel, so assume it is ready
nb.events.trigger('kernel_ready.Kernel');
};

WidgetManager.prototype._hookupDeclWidgetsCallbacks = function(kernelFuture, widgetNode, outputAreaModel) {
var that = this;

Expand All @@ -219,7 +212,6 @@ define([
*/

WidgetManager.prototype._shimMatplotlib = function(kernel) {
var nb = notebookShim();
var cells = this._pendingExecutions;
nb.get_cells = function() {
return Object.keys(cells).map(function(id) {
Expand Down

0 comments on commit d12d9c2

Please sign in to comment.