diff --git a/IPython/html/base/handlers.py b/IPython/html/base/handlers.py
index 03070c0..56772f9 100644
--- a/IPython/html/base/handlers.py
+++ b/IPython/html/base/handlers.py
@@ -159,6 +159,10 @@ class IPythonHandler(AuthenticatedHandler):
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
diff --git a/IPython/html/kernelspecs/__init__.py b/IPython/html/kernelspecs/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/IPython/html/kernelspecs/__init__.py
diff --git a/IPython/html/kernelspecs/handlers.py b/IPython/html/kernelspecs/handlers.py
new file mode 100644
index 0000000..26eecf1
--- /dev/null
+++ b/IPython/html/kernelspecs/handlers.py
@@ -0,0 +1,27 @@
+from tornado import web
+from ..base.handlers import IPythonHandler
+from ..services.kernelspecs.handlers import kernel_name_regex
+
+class KernelSpecResourceHandler(web.StaticFileHandler, IPythonHandler):
+ SUPPORTED_METHODS = ('GET', 'HEAD')
+
+ def initialize(self):
+ web.StaticFileHandler.initialize(self, path='')
+
+ @web.authenticated
+ def get(self, kernel_name, path, include_body=True):
+ ksm = self.kernel_spec_manager
+ try:
+ self.root = ksm.get_kernel_spec(kernel_name).resource_dir
+ except KeyError:
+ raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name)
+ self.log.debug("Serving kernel resource from: %s", self.root)
+ return web.StaticFileHandler.get(self, path, include_body=include_body)
+
+ @web.authenticated
+ def head(self, kernel_name, path):
+ self.get(kernel_name, path, include_body=False)
+
+default_handlers = [
+ (r"/kernelspecs/%s/(?P.*)" % kernel_name_regex, KernelSpecResourceHandler),
+]
\ No newline at end of file
diff --git a/IPython/html/notebookapp.py b/IPython/html/notebookapp.py
index fc08f6b..9c555d6 100644
--- a/IPython/html/notebookapp.py
+++ b/IPython/html/notebookapp.py
@@ -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,
@@ -183,11 +187,13 @@ class NotebookWebApplication(web.Application):
handlers.extend(load_handlers('auth.logout'))
handlers.extend(load_handlers('notebook.handlers'))
handlers.extend(load_handlers('nbconvert.handlers'))
+ handlers.extend(load_handlers('kernelspecs.handlers'))
handlers.extend(load_handlers('services.kernels.handlers'))
handlers.extend(load_handlers('services.notebooks.handlers'))
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 +516,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 +627,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
)
diff --git a/IPython/html/services/kernelspecs/__init__.py b/IPython/html/services/kernelspecs/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/IPython/html/services/kernelspecs/__init__.py
diff --git a/IPython/html/services/kernelspecs/handlers.py b/IPython/html/services/kernelspecs/handlers.py
new file mode 100644
index 0000000..dbe8382
--- /dev/null
+++ b/IPython/html/services/kernelspecs/handlers.py
@@ -0,0 +1,50 @@
+"""Tornado handlers for kernel specifications."""
+
+# Copyright (c) IPython Development Team.
+# Distributed under the terms of the Modified BSD License.
+import json
+from tornado import web
+
+from ...base.handlers import IPythonHandler, json_errors
+
+
+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():
+ d = ksm.get_kernel_spec(kernel_name).to_dict()
+ d['name'] = kernel_name
+ results.append(d)
+
+ self.set_header("Content-Type", 'application/json')
+ self.finish(json.dumps(results))
+
+
+class KernelSpecHandler(IPythonHandler):
+ SUPPORTED_METHODS = ('GET',)
+
+ @web.authenticated
+ @json_errors
+ def get(self, kernel_name):
+ ksm = self.kernel_spec_manager
+ try:
+ kernelspec = ksm.get_kernel_spec(kernel_name)
+ except KeyError:
+ raise web.HTTPError(404, u'Kernel spec %s not found' % kernel_name)
+ self.set_header("Content-Type", 'application/json')
+ self.finish(kernelspec.to_json())
+
+
+# URL to handler mappings
+
+kernel_name_regex = r"(?P\w+)"
+
+default_handlers = [
+ (r"/api/kernelspecs", MainKernelSpecHandler),
+ (r"/api/kernelspecs/%s" % kernel_name_regex, KernelSpecHandler),
+]
diff --git a/IPython/html/services/kernelspecs/tests/test_kernelspecs_api.py b/IPython/html/services/kernelspecs/tests/test_kernelspecs_api.py
new file mode 100644
index 0000000..ad2a5fb
--- /dev/null
+++ b/IPython/html/services/kernelspecs/tests/test_kernelspecs_api.py
@@ -0,0 +1,97 @@
+# coding: utf-8
+"""Test the kernel specs webservice API."""
+
+import errno
+import io
+import json
+import os
+
+pjoin = os.path.join
+
+import requests
+
+from IPython.html.utils import url_path_join
+from IPython.html.tests.launchnotebook import NotebookTestBase, assert_http_error
+
+# Copied from IPython.kernel.tests.test_kernelspec so updating that doesn't
+# break these tests
+sample_kernel_json = {'argv':['cat', '{connection_file}'],
+ 'display_name':'Test kernel',
+ 'language':'bash',
+ }
+
+some_resource = u"The very model of a modern major general"
+
+
+class KernelSpecAPI(object):
+ """Wrapper for notebook API calls."""
+ def __init__(self, base_url):
+ self.base_url = base_url
+
+ def _req(self, verb, path, body=None):
+ response = requests.request(verb,
+ url_path_join(self.base_url, path),
+ data=body,
+ )
+ response.raise_for_status()
+ return response
+
+ def list(self):
+ return self._req('GET', 'api/kernelspecs')
+
+ def kernel_spec_info(self, name):
+ return self._req('GET', url_path_join('api/kernelspecs', name))
+
+ def kernel_resource(self, name, path):
+ return self._req('GET', url_path_join('kernelspecs', name, path))
+
+class APITest(NotebookTestBase):
+ """Test the kernelspec web service API"""
+ def setUp(self):
+ ipydir = self.ipython_dir.name
+ sample_kernel_dir = pjoin(ipydir, 'kernels', 'sample')
+ try:
+ os.makedirs(sample_kernel_dir)
+ except OSError as e:
+ if e.errno != errno.EEXIST:
+ raise
+
+ with open(pjoin(sample_kernel_dir, 'kernel.json'), 'w') as f:
+ json.dump(sample_kernel_json, f)
+
+ with io.open(pjoin(sample_kernel_dir, 'resource.txt'), 'w',
+ encoding='utf-8') as f:
+ f.write(some_resource)
+
+ self.ks_api = KernelSpecAPI(self.base_url())
+
+ def test_list_kernelspecs(self):
+ specs = self.ks_api.list().json()
+ assert isinstance(specs, list)
+
+ # 2: the sample kernelspec created in setUp, and the native Python kernel
+ self.assertEqual(len(specs), 2)
+
+ def is_sample_kernelspec(s):
+ return s['name'] == 'sample' and s['display_name'] == 'Test kernel'
+
+ assert any(is_sample_kernelspec(s) for s in specs), specs
+
+ def test_get_kernelspec(self):
+ spec = self.ks_api.kernel_spec_info('Sample').json() # Case insensitive
+ self.assertEqual(spec['language'], 'bash')
+
+ def test_get_nonexistant_kernelspec(self):
+ with assert_http_error(404):
+ self.ks_api.kernel_spec_info('nonexistant')
+
+ def test_get_kernel_resource_file(self):
+ res = self.ks_api.kernel_resource('sAmple', 'resource.txt')
+ self.assertEqual(res.text, some_resource)
+
+ def test_get_nonexistant_resource(self):
+ with assert_http_error(404):
+ self.ks_api.kernel_resource('nonexistant', 'resource.txt')
+
+ with assert_http_error(404):
+ self.ks_api.kernel_resource('sample', 'nonexistant.txt')
diff --git a/IPython/kernel/kernelspec.py b/IPython/kernel/kernelspec.py
index a8931e0..34e536f 100644
--- a/IPython/kernel/kernelspec.py
+++ b/IPython/kernel/kernelspec.py
@@ -50,6 +50,17 @@ class KernelSpec(HasTraits):
with io.open(kernel_file, 'r', encoding='utf-8') as f:
kernel_dict = json.load(f)
return cls(resource_dir=resource_dir, **kernel_dict)
+
+ def to_dict(self):
+ return dict(argv=self.argv,
+ env=self.env,
+ display_name=self.display_name,
+ language=self.language,
+ codemirror_mode=self.codemirror_mode,
+ )
+
+ def to_json(self):
+ return json.dumps(self.to_dict())
def _is_kernel_dir(path):
"""Is ``path`` a kernel directory?"""