Skip to content

Commit

Permalink
Merge pull request jupyter#1279 from parente/widg
Browse files Browse the repository at this point in the history
Downgraded ipywidget hack
  • Loading branch information
minrk committed Mar 31, 2016
2 parents b5fe738 + 9f9bc69 commit a412557
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 48 deletions.
27 changes: 8 additions & 19 deletions notebook/notebookapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,6 @@ def load_handlers(name):
mod = __import__(name, fromlist=['default_handlers'])
return mod.default_handlers


class DeprecationHandler(IPythonHandler):
def get(self, url_path):
self.set_header("Content-Type", 'text/javascript')
self.finish("""
console.warn('`/static/widgets/js` is deprecated. Use `nbextensions/widgets/widgets/js` instead.');
define(['%s'], function(x) { return x; });
""" % url_path_join('nbextensions', 'widgets', 'widgets', url_path.rstrip('.js')))
self.log.warning('Deprecated widget Javascript path /static/widgets/js/*.js was used')

#-----------------------------------------------------------------------------
# The Tornado web application
#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -262,7 +252,6 @@ def init_handlers(self, settings):

# Order matters. The first handler to match the URL will handle the request.
handlers = []
handlers.append((r'/deprecatedwidgets/(.*)', DeprecationHandler))
handlers.extend(load_handlers('tree.handlers'))
handlers.extend([(r"/login", settings['login_handler_class'])])
handlers.extend([(r"/logout", settings['logout_handler_class'])])
Expand All @@ -282,21 +271,21 @@ def init_handlers(self, settings):
handlers.extend(load_handlers('lab.handlers'))

# BEGIN HARDCODED WIDGETS HACK
# TODO: Remove on notebook 5.0
widgets = None
try:
import widgetsnbextension as widgets
import widgetsnbextension
except:
try:
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')
if widgets is not None:
handlers.append(
(r"/nbextensions/widgets/(.*)", FileFindHandler, {
'path': widgets.find_static_assets(),
'no_cache_paths': ['/'], # don't cache anything in nbextensions
}),
)
# END HARDCODED WIDGETS HACK

handlers.append(
Expand Down
39 changes: 25 additions & 14 deletions notebook/static/base/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -22,16 +32,16 @@ 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);
if (!is_loaded(extension)) {
console.log("Loading extension: " + extension);
if (module.load_ipython_extension) {
Promise.resolve(module.load_ipython_extension()).then(function() {
resolve(module);
}).catch(reject);
}
else{
console.log("Loaded extension already: " + extension);
}
} finally {
extensions_loaded.push(ext_path);
} else {
console.log("Loaded extension already: " + extension);
resolve(module);
}
}, function(err) {
Expand All @@ -54,8 +64,8 @@ define([

/**
* 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
*/
Expand All @@ -72,12 +82,12 @@ 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);
}
});
}).catch(utils.reject('Could not load nbextensions from ' + section.section_name + ' config file'));
}

//============================================================================
Expand Down Expand Up @@ -937,6 +947,7 @@ define([
};

var utils = {
is_loaded: is_loaded,
load_extension: load_extension,
load_extensions: load_extensions,
filter_extensions: filter_extensions,
Expand Down
36 changes: 23 additions & 13 deletions notebook/static/notebook/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -187,9 +178,28 @@ require([
configurable: false
});

// Now actually load nbextensions
utils.load_extensions_from_config(config_section);
utils.load_extensions_from_config(common_config);
// Now actually load nbextensionsload_extensions_from_config
Promise.all([
utils.load_extensions_from_config(config_section),
utils.load_extensions_from_config(common_config),
])
.catch(function(error) {
console.error('Could not load nbextensions from user config files', error);
})
// BEGIN HARDCODED WIDGETS HACK
.then(function() {
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');
});
}
})
.catch(function(error) {
console.error('Could not load ipywidgets', error);
});
// END HARDCODED WIDGETS HACK

notebook.load_notebook(common_options.notebook_path);

});
Expand Down
1 change: 0 additions & 1 deletion notebook/templates/lab.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
{% endif %}
custom : '{{ base_url }}custom',
nbextensions : '{{ base_url }}nbextensions',
widgets : '{{ base_url }}deprecatedwidgets',
kernelspecs : '{{ base_url }}kernelspecs',
underscore : 'components/underscore/underscore-min',
backbone : 'components/backbone/backbone-min',
Expand Down
1 change: 0 additions & 1 deletion notebook/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
'auth/js/main': 'auth/js/built/main.min',
custom : '{{ base_url }}custom',
nbextensions : '{{ base_url }}nbextensions',
widgets : '{{ base_url }}deprecatedwidgets',
kernelspecs : '{{ base_url }}kernelspecs',
underscore : 'components/underscore/underscore-min',
backbone : 'components/backbone/backbone-min',
Expand Down

0 comments on commit a412557

Please sign in to comment.