handlers.py
50 lines
| 1.4 KiB
| text/x-python
|
PythonLexer
|
r16684 | """Tornado handlers for kernel specifications.""" | ||
# Copyright (c) IPython Development Team. | ||||
# Distributed under the terms of the Modified BSD License. | ||||
|
r17020 | import json | ||
|
r16684 | from tornado import web | ||
|
r16689 | from ...base.handlers import IPythonHandler, json_errors | ||
|
r16684 | |||
class MainKernelSpecHandler(IPythonHandler): | ||||
SUPPORTED_METHODS = ('GET',) | ||||
@web.authenticated | ||||
@json_errors | ||||
def get(self): | ||||
ksm = self.kernel_spec_manager | ||||
results = [] | ||||
for kernel_name in ksm.find_kernel_specs(): | ||||
|
r16714 | d = ksm.get_kernel_spec(kernel_name).to_dict() | ||
d['name'] = kernel_name | ||||
results.append(d) | ||||
|
r16684 | |||
self.set_header("Content-Type", 'application/json') | ||||
|
r17020 | self.finish(json.dumps(results)) | ||
|
r16684 | |||
class KernelSpecHandler(IPythonHandler): | ||||
SUPPORTED_METHODS = ('GET',) | ||||
@web.authenticated | ||||
@json_errors | ||||
def get(self, kernel_name): | ||||
ksm = self.kernel_spec_manager | ||||
|
r16689 | try: | ||
kernelspec = ksm.get_kernel_spec(kernel_name) | ||||
except KeyError: | ||||
raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name) | ||||
|
r16684 | self.set_header("Content-Type", 'application/json') | ||
self.finish(kernelspec.to_json()) | ||||
# URL to handler mappings | ||||
|
r16706 | kernel_name_regex = r"(?P<kernel_name>\w+)" | ||
|
r16684 | |||
default_handlers = [ | ||||
(r"/api/kernelspecs", MainKernelSpecHandler), | ||||
|
r16706 | (r"/api/kernelspecs/%s" % kernel_name_regex, KernelSpecHandler), | ||
|
r16684 | ] | ||