##// END OF EJS Templates
add tooltips
add tooltips

File last commit:

r19681:2344bb6d
r20398:c180176e
Show More
handlers.py
86 lines | 2.5 KiB | text/x-python | PythonLexer
Thomas Kluyver
Create REST API for kernel specs
r16684 """Tornado handlers for kernel specifications."""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Min RK
add resource URLs to kernelspec model...
r19681
import glob
Thomas Kluyver
Use stdlib json, not zmq jsonapi
r17020 import json
Min RK
add resource URLs to kernelspec model...
r19681 import os
pjoin = os.path.join
Thomas Kluyver
Create REST API for kernel specs
r16684 from tornado import web
Thomas Kluyver
Test kernel specs REST API...
r16689 from ...base.handlers import IPythonHandler, json_errors
Min RK
add resource URLs to kernelspec model...
r19681 from ...utils import url_path_join
Thomas Kluyver
Create REST API for kernel specs
r16684
Min RK
add resource URLs to kernelspec model...
r19681 def kernelspec_model(handler, name):
"""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
for resource in ['kernel.js', 'kernel.css']:
if os.path.exists(pjoin(resource_dir, resource)):
resources[resource] = url_path_join(
handler.base_url,
'kernelspecs',
name,
resource
)
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(
handler.base_url,
'kernelspecs',
name,
fname
)
return d
Thomas Kluyver
Create REST API for kernel specs
r16684
class MainKernelSpecHandler(IPythonHandler):
SUPPORTED_METHODS = ('GET',)
@web.authenticated
@json_errors
def get(self):
ksm = self.kernel_spec_manager
Min RK
kernelspecs is a dict...
r19263 km = self.kernel_manager
model = {}
model['default'] = km.default_kernel_name
model['kernelspecs'] = specs = {}
for kernel_name in ksm.find_kernel_specs():
Min RK
log kernel specs that fail to load
r18552 try:
Min RK
add resource URLs to kernelspec model...
r19681 d = kernelspec_model(self, kernel_name)
Min RK
log kernel specs that fail to load
r18552 except Exception:
self.log.error("Failed to load kernel spec: '%s'", kernel_name, exc_info=True)
continue
Min RK
kernelspecs is a dict...
r19263 specs[kernel_name] = d
Thomas Kluyver
Create REST API for kernel specs
r16684 self.set_header("Content-Type", 'application/json')
Min RK
kernelspecs is a dict...
r19263 self.finish(json.dumps(model))
Thomas Kluyver
Create REST API for kernel specs
r16684
class KernelSpecHandler(IPythonHandler):
SUPPORTED_METHODS = ('GET',)
@web.authenticated
@json_errors
def get(self, kernel_name):
Thomas Kluyver
Test kernel specs REST API...
r16689 try:
Min RK
add resource URLs to kernelspec model...
r19681 model = kernelspec_model(self, kernel_name)
Thomas Kluyver
Test kernel specs REST API...
r16689 except KeyError:
raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name)
Thomas Kluyver
Create REST API for kernel specs
r16684 self.set_header("Content-Type", 'application/json')
Min RK
add resource URLs to kernelspec model...
r19681 self.finish(json.dumps(model))
Thomas Kluyver
Create REST API for kernel specs
r16684
# URL to handler mappings
Thomas Kluyver
Refactor kernelspec resource handler to separate URL prefix
r16706 kernel_name_regex = r"(?P<kernel_name>\w+)"
Thomas Kluyver
Create REST API for kernel specs
r16684
default_handlers = [
(r"/api/kernelspecs", MainKernelSpecHandler),
Thomas Kluyver
Refactor kernelspec resource handler to separate URL prefix
r16706 (r"/api/kernelspecs/%s" % kernel_name_regex, KernelSpecHandler),
Thomas Kluyver
Create REST API for kernel specs
r16684 ]