Skip to content

Commit fac424e

Browse files
committed
Use requirejs to perform redirect.
1 parent 04eadff commit fac424e

File tree

1 file changed

+14
-43
lines changed

1 file changed

+14
-43
lines changed

jupyter_notebook/notebookapp.py

Lines changed: 14 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import base64
1010
import datetime
1111
import errno
12-
import imp
1312
import importlib
1413
import io
1514
import json
@@ -99,12 +98,6 @@
9998
ipython notebook --profile=sympy # use the sympy profile
10099
ipython notebook --certfile=mycert.pem # use SSL/TLS certificate
101100
"""
102-
DEPRECATED_WIDGET_WARNING = """
103-
console.warn("/static/widgets/js/ is deprecated, import from nbextensions/widgets/widgets/js/ instead.");
104-
"""
105-
MISSING_WIDGET_ERROR = """
106-
throw new Error('Python package `ipython_widgets` not installed. Please install it on the host.');
107-
"""
108101

109102
#-----------------------------------------------------------------------------
110103
# Helper functions
@@ -221,7 +214,7 @@ def init_handlers(self, settings):
221214

222215
# Order matters. The first handler to match the URL will handle the request.
223216
handlers = []
224-
handlers.extend(self._create_widget_shim_handlers())
217+
handlers.extend(self._create_widget_shim_handlers(settings))
225218
handlers.extend(load_handlers('tree.handlers'))
226219
handlers.extend([(r"/login", settings['login_handler_class'])])
227220
handlers.extend([(r"/logout", settings['logout_handler_class'])])
@@ -270,43 +263,21 @@ def init_handlers(self, settings):
270263
new_handlers.append((r'(.*)', Template404))
271264
return new_handlers
272265

273-
def _create_widget_shim_handlers(self):
266+
def _create_widget_shim_handlers(self, settings):
274267
"""Creates a handler that shims the loading of widgets
275268
JS files for the old /static/widgets/js/*.js path."""
276-
# Add RESTful shim for the ipython_widgets
277-
try:
278-
ipythonwidgets = imp.find_module('ipython_widgets')[1]
279-
widget_js = os.path.join(ipythonwidgets, 'static', 'widgets')
280-
281-
class DeprecationHandler(IPythonHandler):
282-
def get(self, url_path):
283-
self.set_header("Content-Type", 'text/javascript')
284-
285-
# Verify that the file requested exists.
286-
local_file = os.path.join(widget_js, *url_path.split('/'))
287-
if not os.path.isfile(local_file):
288-
self.set_status(404)
289-
self.finish("File doesn't exits")
290-
291-
# Read the file and append the warning.
292-
with open(local_file, 'r') as f:
293-
contents = f.read()
294-
self.finish(DEPRECATED_WIDGET_WARNING + contents)
295-
self.log.warn('Deprecated widget Javascript path /static/widgets/js/*.js was used')
296-
return [(
297-
r'/deprecatedwidgets/(.*)',
298-
DeprecationHandler
299-
)]
300-
except: # ipython_widgets is not installed
301-
class ErrorHandler(IPythonHandler):
302-
def get(self, *args, **kwargs):
303-
self.set_header("Content-Type", 'text/javascript')
304-
self.finish(MISSING_WIDGET_ERROR)
305-
self.log.warn("Attempt to import widget javascript failed, `ipython_widgets` is not installed")
306-
return [(
307-
r'/deprecatedwidgets/(.*)',
308-
ErrorHandler
309-
)]
269+
class DeprecationHandler(IPythonHandler):
270+
def get(self, url_path):
271+
self.set_header("Content-Type", 'text/javascript')
272+
self.finish("""
273+
console.warn('`/static/widgets/js` is deprecated. Use `/nbextensions/widgets/widgets/js` instead.');
274+
define(['%s'], function(x) { console.log('...loaded...'); return x; });
275+
""" % url_path_join(settings['base_url'], 'nbextensions', 'widgets', 'widgets', url_path))
276+
self.log.warn('Deprecated widget Javascript path /static/widgets/js/*.js was used')
277+
return [(
278+
r'/deprecatedwidgets/(.*)',
279+
DeprecationHandler
280+
)]
310281

311282

312283
class NbserverListApp(JupyterApp):

0 commit comments

Comments
 (0)