Create REST API for kernel specs

This commit is contained in:
Thomas Kluyver 2014-05-08 16:18:23 -07:00
parent 79b8ecd9c9
commit 02fa348c69
3 changed files with 96 additions and 7 deletions

View File

@ -158,6 +158,10 @@ class IPythonHandler(AuthenticatedHandler):
def session_manager(self):
return self.settings['session_manager']
@property
def kernel_spec_manager(self):
return self.settings['kernel_spec_manager']
@property
def project_dir(self):
return self.notebook_manager.notebook_dir

View File

@ -67,12 +67,13 @@ from IPython.core.application import (
)
from IPython.core.profiledir import ProfileDir
from IPython.kernel import KernelManager
from IPython.kernel.kernelspec import KernelSpecManager
from IPython.kernel.zmq.session import default_secure, Session
from IPython.nbformat.sign import NotebookNotary
from IPython.utils.importstring import import_item
from IPython.utils import submodule
from IPython.utils.traitlets import (
Dict, Unicode, Integer, List, Bool, Bytes,
Dict, Unicode, Integer, List, Bool, Bytes, Instance,
DottedObjectName, TraitError,
)
from IPython.utils import py3compat
@ -118,19 +119,21 @@ def load_handlers(name):
class NotebookWebApplication(web.Application):
def __init__(self, ipython_app, kernel_manager, notebook_manager,
cluster_manager, session_manager, log, base_url,
settings_overrides, jinja_env_options):
cluster_manager, session_manager, kernel_spec_manager, log,
base_url, settings_overrides, jinja_env_options):
settings = self.init_settings(
ipython_app, kernel_manager, notebook_manager, cluster_manager,
session_manager, log, base_url, settings_overrides, jinja_env_options)
session_manager, kernel_spec_manager, log, base_url,
settings_overrides, jinja_env_options)
handlers = self.init_handlers(settings)
super(NotebookWebApplication, self).__init__(handlers, **settings)
def init_settings(self, ipython_app, kernel_manager, notebook_manager,
cluster_manager, session_manager, log, base_url,
settings_overrides, jinja_env_options=None):
cluster_manager, session_manager, kernel_spec_manager,
log, base_url, settings_overrides,
jinja_env_options=None):
# Python < 2.6.5 doesn't accept unicode keys in f(**kwargs), and
# base_url will always be unicode, which will in turn
# make the patterns unicode, and ultimately result in unicode
@ -162,6 +165,7 @@ class NotebookWebApplication(web.Application):
notebook_manager=notebook_manager,
cluster_manager=cluster_manager,
session_manager=session_manager,
kernel_spec_manager=kernel_spec_manager,
# IPython stuff
nbextensions_path = ipython_app.nbextensions_path,
@ -188,6 +192,7 @@ class NotebookWebApplication(web.Application):
handlers.extend(load_handlers('services.clusters.handlers'))
handlers.extend(load_handlers('services.sessions.handlers'))
handlers.extend(load_handlers('services.nbconvert.handlers'))
handlers.extend(load_handlers('services.kernelspecs.handlers'))
# FIXME: /files/ should be handled by the Contents service when it exists
nbm = settings['notebook_manager']
if hasattr(nbm, 'notebook_dir'):
@ -510,6 +515,11 @@ class NotebookApp(BaseIPythonApplication):
help='The cluster manager class to use.'
)
kernel_spec_manager = Instance(KernelSpecManager)
def _kernel_spec_manager_default(self):
return KernelSpecManager(ipython_dir=self.ipython_dir)
trust_xheaders = Bool(False, config=True,
help=("Whether to trust or not X-Scheme/X-Forwarded-Proto and X-Real-Ip/X-Forwarded-For headers"
"sent by the upstream reverse proxy. Necessary if the proxy handles SSL")
@ -616,7 +626,7 @@ class NotebookApp(BaseIPythonApplication):
"""initialize tornado webapp and httpserver"""
self.web_app = NotebookWebApplication(
self, self.kernel_manager, self.notebook_manager,
self.cluster_manager, self.session_manager,
self.cluster_manager, self.session_manager, self.kernel_spec_manager,
self.log, self.base_url, self.webapp_settings,
self.jinja_environment_options
)

View File

@ -0,0 +1,75 @@
"""Tornado handlers for kernel specifications."""
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
import logging
from tornado import web
from zmq.utils import jsonapi
from ...base.handlers import IPythonHandler, json_errors, path_regex
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():
results.append(dict(name=kernel_name,
display_name=ksm.get_kernel_spec(kernel_name).display_name))
self.set_header("Content-Type", 'application/json')
self.finish(jsonapi.dumps(results))
class KernelSpecHandler(IPythonHandler):
SUPPORTED_METHODS = ('GET',)
@web.authenticated
@json_errors
def get(self, kernel_name):
ksm = self.kernel_spec_manager
kernelspec = ksm.get_kernel_spec(kernel_name)
self.set_header("Content-Type", 'application/json')
self.finish(kernelspec.to_json())
class KernelSpecResourceHandler(web.StaticFileHandler, IPythonHandler):
SUPPORTED_METHODS = ('GET', 'HEAD')
def initialize(self):
web.StaticFileHandler.initialize(self, path='')
def get(self, kernel_name, path, include_body=True):
ksm = self.kernel_spec_manager
self.root = ksm.get_kernel_spec(kernel_name).resource_dir
self.log.warn("Set root: %s", self.root)
return web.StaticFileHandler.get(self, path, include_body=include_body)
# @classmethod
# def get_absolute_path(cls, root, path):
# res = web.StaticFileHandler.get_absolute_path(cls, root, path)
# self.log.warn("Full path: %s", res)
# return res
def head(self, kernel_name, path):
self.get(kernel_name, path, include_body=False)
#-----------------------------------------------------------------------------
# URL to handler mappings
#-----------------------------------------------------------------------------
_kernel_name_regex = r"(?P<kernel_name>\w+)"
default_handlers = [
(r"/api/kernelspecs", MainKernelSpecHandler),
(r"/api/kernelspecs/%s" % _kernel_name_regex, KernelSpecHandler),
(r"/api/kernelspecs/%s/(?P<path>.*)" % _kernel_name_regex, KernelSpecResourceHandler),
]