mirror of
https://github.com/jupyter/notebook.git
synced 2025-04-06 13:50:29 +08:00
Merge pull request #3136 from takluyver/i3135
Find available kernelspecs more efficiently
This commit is contained in:
commit
782e9cedb3
@ -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))
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user