Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
Use relative path based on notebook for html import loads.
Browse files Browse the repository at this point in the history
(c) Copyright IBM Corp. 2015
  • Loading branch information
Drew Walters committed Oct 7, 2015
1 parent dfc40d2 commit 45444eb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
15 changes: 14 additions & 1 deletion nb-extension/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ define([
'./init/init'
], function(init) {
'use strict';
init(IPython.notebook.base_url + 'urth_components');
// Default the root directory for bower_components to be
// the notebook base url, but use the base of the current
// url instead as long as it is valid. This is done so that
// on init, the HTML imports are loaded from the same
// url that will be used for HTML imports in cells of the
// notebook. Doing this allows the browser to recognize the
// resource has already been retrieved and prevents double
// initialization of polymer and elements.
var bower_root = IPython.notebook.base_url;
var slashIndex = window.location.href.lastIndexOf('/');
if (slashIndex > 0) {
bower_root = window.location.href.substr(0, slashIndex + 1);
}
init(bower_root + 'urth_components');
console.debug('Custom JS loaded');
});
13 changes: 7 additions & 6 deletions nb-extension/python/urth/widgets/urth_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def post(self):
msg = 'Failed to open bower.json for {0}.'.format(package_name)
raise tornado.web.HTTPError(404, msg, reason=msg)

class ComponentsRedirectHandler(RequestHandler):
class ComponentsRedirectHandler(RequestHandler):
def get(self, path):
'''
Redirect relative requests for components to the global store. Makes
Expand All @@ -100,12 +100,13 @@ def load_jupyter_server_extension(nb_app):
if ipython_dir in path:
nbext = path
import_route_pattern = url_path_join(web_app.settings['base_url'], '/urth_import')
components_route_pattern = url_path_join(web_app.settings['base_url'], '/urth_components/(.*)')
components_redirect_route_pattern = url_path_join(web_app.settings['base_url'], '/notebooks/.*/urth_components/(.*)')

# Any requests containing /urth_components/ will get served from the bower_components
# directory.
components_route_pattern = url_path_join(web_app.settings['base_url'], '.*/urth_components/(.*)')
path = os.path.join(nbext, 'urth_widgets/bower_components/')

web_app.add_handlers(host_pattern, [
(import_route_pattern, UrthImportHandler),
(components_route_pattern, FileFindHandler, {'path': [path]}),
(components_redirect_route_pattern, ComponentsRedirectHandler)
(components_route_pattern, FileFindHandler, {'path': [path]})
])

2 changes: 1 addition & 1 deletion notebooks/examples/urth-core-bind.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@
"outputs": [],
"source": [
"%%html\n",
"<link rel='import' href='/urth_components/paper-slider/paper-slider.html'\n",
"<link rel='import' href='urth_components/paper-slider/paper-slider.html'\n",
" is='urth-core-import' package='PolymerElements/paper-slider'>"
]
},
Expand Down

0 comments on commit 45444eb

Please sign in to comment.