Downgraded ipywidget hack

Fixed whitespace for jon's PR #1278
This commit is contained in:
Peter Parente 2016-03-29 15:56:28 -07:00
parent d55e83a1d1
commit 00ef5b1314
3 changed files with 44 additions and 34 deletions

View File

@ -284,19 +284,15 @@ class NotebookWebApplication(web.Application):
# BEGIN HARDCODED WIDGETS HACK
widgets = None
try:
import widgetsnbextension as widgets
except:
try:
import ipywidgets as widgets
except:
app_log.warning('Widgets are unavailable. Please install widgetsnbextension or ipywidgets 4.0')
if widgets is not None:
import ipywidgets as widgets
handlers.append(
(r"/nbextensions/widgets/(.*)", FileFindHandler, {
'path': widgets.find_static_assets(),
'no_cache_paths': ['/'], # don't cache anything in nbextensions
}),
)
except:
app_log.warning('Widgets are unavailable. Please install widgetsnbextension or ipywidgets 4.0')
# END HARDCODED WIDGETS HACK
handlers.append(

View File

@ -13,6 +13,16 @@ define([
// keep track of which extensions have been loaded already
var extensions_loaded = [];
/**
* Whether or not an extension has been loaded
* @param {string} extension - name of the extension
* @return {boolean} true if loaded already
*/
var is_loaded = function(extension) {
var ext_path = "nbextensions/" + extension;
return extensions_loaded.indexOf(ext_path) < 0;
};
/**
* Load a single extension.
* @param {string} extension - extension path.
@ -22,16 +32,14 @@ define([
return new Promise(function(resolve, reject) {
var ext_path = "nbextensions/" + extension;
requirejs([ext_path], function(module) {
try {
if (extensions_loaded.indexOf(ext_path) < 0) {
console.log("Loading extension: " + extension);
module.load_ipython_extension();
extensions_loaded.push(ext_path);
}
else{
console.log("Loaded extension already: " + extension);
}
} finally {
if (is_loaded(extension)) {
console.log("Loading extension: " + extension);
Promise.resolve(module.load_ipython_extension()).then(function() {
resolve(module);
}).catch(reject);
extensions_loaded.push(ext_path);
} else {
console.log("Loaded extension already: " + extension);
resolve(module);
}
}, function(err) {
@ -47,15 +55,17 @@ define([
*/
var load_extensions = function () {
console.log('load_extensions', arguments);
return Promise.all(Array.prototype.map.call(arguments, load_extension)).catch(function(err) {
var args = Array.prototype.splice.apply(arguments);
return Promise.all(args.map(load_extension)).catch(function(err) {
console.error("Failed to load extension" + (err.requireModules.length>1?'s':'') + ":", err.requireModules, err);
});
};
/**
* Return a list of extensions that should be active
* The config for nbextensions comes in as a dict where keys are
* nbextensions paths and the values are a bool indicating if it
* The config for nbextensions comes in as a dict where keys are
* nbextensions paths and the values are a bool indicating if it
* should be active. This returns a list of nbextension paths
* where the value is true
*/
@ -72,10 +82,10 @@ define([
* in a 'load_extensions' key inside it.
*/
function load_extensions_from_config(section) {
section.loaded.then(function() {
return section.loaded.then(function() {
if (section.data.load_extensions) {
var active = filter_extensions(section.data.load_extensions);
load_extensions.apply(this, active);
return load_extensions.apply(this, active);
}
});
}
@ -937,6 +947,7 @@ define([
};
var utils = {
is_loaded: is_loaded,
load_extension: load_extension,
load_extensions: load_extensions,
filter_extensions: filter_extensions,

View File

@ -53,21 +53,12 @@ require([
requirejs(['custom/custom'], function() {});
// BEGIN HARDCODED WIDGETS HACK
// Try to load the new extension
utils.load_extension('widgets/extension').catch(function () {
// Fallback to the ipywidgets extension
utils.load_extension('widgets/notebook/js/extension').catch(function () {
console.warn('Widgets are not available. Please install widgetsnbextension or ipywidgets 4.0');
});
});
// END HARDCODED WIDGETS HACK
// compat with old IPython, remove for IPython > 3.0
window.CodeMirror = CodeMirror;
// Setup all of the config related things
var common_options = {
ws_url : utils.get_body_data("wsUrl"),
base_url : utils.get_body_data("baseUrl"),
@ -188,8 +179,20 @@ require([
});
// Now actually load nbextensions
utils.load_extensions_from_config(config_section);
utils.load_extensions_from_config(common_config);
Promise.all([
utils.load_extensions_from_config(config_section),
utils.load_extensions_from_config(common_config),
]).then(function() {
// BEGIN HARDCODED WIDGETS HACK
if (!utils.is_loaded('widgets/extension')) {
// Fallback to the ipywidgets extension
utils.load_extension('widgets/notebook/js/extension').catch(function () {
console.warn('Widgets are not available. Please install widgetsnbextension or ipywidgets 4.0');
});
}
// END HARDCODED WIDGETS HACK
});
notebook.load_notebook(common_options.notebook_path);
});