Merge pull request #3136 from takluyver/i3135

Find available kernelspecs more efficiently
This commit is contained in:
Steven Silvester 2017-12-19 14:52:03 -06:00 committed by GitHub
commit 782e9cedb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 13 deletions

View File

@ -16,17 +16,19 @@ from tornado import web
from ...base.handlers import APIHandler
from ...utils import url_path_join, url_unescape
def kernelspec_model(handler, name):
def kernelspec_model(handler, name, spec_dict, resource_dir):
"""Load a KernelSpec by name and return the REST API model"""
ksm = handler.kernel_spec_manager
spec = ksm.get_kernel_spec(name)
d = {'name': name}
d['spec'] = spec.to_dict()
d['resources'] = resources = {}
resource_dir = spec.resource_dir
d = {
'name': name,
'spec': spec_dict,
'resources': {}
}
# Add resource files if they exist
resource_dir = resource_dir
for resource in ['kernel.js', 'kernel.css']:
if os.path.exists(pjoin(resource_dir, resource)):
resources[resource] = url_path_join(
d['resources'][resource] = url_path_join(
handler.base_url,
'kernelspecs',
name,
@ -35,7 +37,7 @@ def kernelspec_model(handler, name):
for logo_file in glob.glob(pjoin(resource_dir, 'logo-*')):
fname = os.path.basename(logo_file)
no_ext, _ = os.path.splitext(fname)
resources[no_ext] = url_path_join(
d['resources'][no_ext] = url_path_join(
handler.base_url,
'kernelspecs',
name,
@ -52,9 +54,10 @@ class MainKernelSpecHandler(APIHandler):
model = {}
model['default'] = km.default_kernel_name
model['kernelspecs'] = specs = {}
for kernel_name in ksm.find_kernel_specs():
for kernel_name, kernel_info in ksm.get_all_specs().items():
try:
d = kernelspec_model(self, kernel_name)
d = kernelspec_model(self, kernel_name, kernel_info['spec'],
kernel_info['resource_dir'])
except Exception:
self.log.error("Failed to load kernel spec: '%s'", kernel_name, exc_info=True)
continue
@ -67,10 +70,13 @@ class KernelSpecHandler(APIHandler):
@web.authenticated
def get(self, kernel_name):
ksm = self.kernel_spec_manager
kernel_name = url_unescape(kernel_name)
try:
model = kernelspec_model(self, url_unescape(kernel_name))
spec = ksm.get_kernel_spec(kernel_name)
except KeyError:
raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name)
model = kernelspec_model(self, kernel_name, spec.to_dict(), spec.resource_dir)
self.set_header("Content-Type", 'application/json')
self.finish(json.dumps(model))

View File

@ -148,7 +148,7 @@ install_requires = [
'ipython_genutils',
'traitlets>=4.2.1',
'jupyter_core>=4.4.0',
'jupyter_client',
'jupyter_client>=5.2.0',
'nbformat',
'nbconvert',
'ipykernel', # bless IPython kernel for now