##// END OF EJS Templates
Create REST API for kernel specs
Thomas Kluyver -
Show More
1 NO CONTENT: new file 100644
NO CONTENT: new file 100644
@@ -0,0 +1,75 b''
1 """Tornado handlers for kernel specifications."""
2
3 # Copyright (c) IPython Development Team.
4 # Distributed under the terms of the Modified BSD License.
5
6 import logging
7 from tornado import web
8
9 from zmq.utils import jsonapi
10
11 from ...base.handlers import IPythonHandler, json_errors, path_regex
12
13
14 class MainKernelSpecHandler(IPythonHandler):
15 SUPPORTED_METHODS = ('GET',)
16
17 @web.authenticated
18 @json_errors
19 def get(self):
20 ksm = self.kernel_spec_manager
21 results = []
22 for kernel_name in ksm.find_kernel_specs():
23 results.append(dict(name=kernel_name,
24 display_name=ksm.get_kernel_spec(kernel_name).display_name))
25
26 self.set_header("Content-Type", 'application/json')
27 self.finish(jsonapi.dumps(results))
28
29
30 class KernelSpecHandler(IPythonHandler):
31 SUPPORTED_METHODS = ('GET',)
32
33 @web.authenticated
34 @json_errors
35 def get(self, kernel_name):
36 ksm = self.kernel_spec_manager
37 kernelspec = ksm.get_kernel_spec(kernel_name)
38 self.set_header("Content-Type", 'application/json')
39 self.finish(kernelspec.to_json())
40
41
42 class KernelSpecResourceHandler(web.StaticFileHandler, IPythonHandler):
43 SUPPORTED_METHODS = ('GET', 'HEAD')
44
45 def initialize(self):
46 web.StaticFileHandler.initialize(self, path='')
47
48 def get(self, kernel_name, path, include_body=True):
49 ksm = self.kernel_spec_manager
50 self.root = ksm.get_kernel_spec(kernel_name).resource_dir
51 self.log.warn("Set root: %s", self.root)
52 return web.StaticFileHandler.get(self, path, include_body=include_body)
53
54 # @classmethod
55 # def get_absolute_path(cls, root, path):
56 # res = web.StaticFileHandler.get_absolute_path(cls, root, path)
57 # self.log.warn("Full path: %s", res)
58 # return res
59
60 def head(self, kernel_name, path):
61 self.get(kernel_name, path, include_body=False)
62
63
64 #-----------------------------------------------------------------------------
65 # URL to handler mappings
66 #-----------------------------------------------------------------------------
67
68
69 _kernel_name_regex = r"(?P<kernel_name>\w+)"
70
71 default_handlers = [
72 (r"/api/kernelspecs", MainKernelSpecHandler),
73 (r"/api/kernelspecs/%s" % _kernel_name_regex, KernelSpecHandler),
74 (r"/api/kernelspecs/%s/(?P<path>.*)" % _kernel_name_regex, KernelSpecResourceHandler),
75 ]
@@ -159,6 +159,10 b' class IPythonHandler(AuthenticatedHandler):'
159 return self.settings['session_manager']
159 return self.settings['session_manager']
160
160
161 @property
161 @property
162 def kernel_spec_manager(self):
163 return self.settings['kernel_spec_manager']
164
165 @property
162 def project_dir(self):
166 def project_dir(self):
163 return self.notebook_manager.notebook_dir
167 return self.notebook_manager.notebook_dir
164
168
@@ -67,12 +67,13 b' from IPython.core.application import ('
67 )
67 )
68 from IPython.core.profiledir import ProfileDir
68 from IPython.core.profiledir import ProfileDir
69 from IPython.kernel import KernelManager
69 from IPython.kernel import KernelManager
70 from IPython.kernel.kernelspec import KernelSpecManager
70 from IPython.kernel.zmq.session import default_secure, Session
71 from IPython.kernel.zmq.session import default_secure, Session
71 from IPython.nbformat.sign import NotebookNotary
72 from IPython.nbformat.sign import NotebookNotary
72 from IPython.utils.importstring import import_item
73 from IPython.utils.importstring import import_item
73 from IPython.utils import submodule
74 from IPython.utils import submodule
74 from IPython.utils.traitlets import (
75 from IPython.utils.traitlets import (
75 Dict, Unicode, Integer, List, Bool, Bytes,
76 Dict, Unicode, Integer, List, Bool, Bytes, Instance,
76 DottedObjectName, TraitError,
77 DottedObjectName, TraitError,
77 )
78 )
78 from IPython.utils import py3compat
79 from IPython.utils import py3compat
@@ -118,19 +119,21 b' def load_handlers(name):'
118 class NotebookWebApplication(web.Application):
119 class NotebookWebApplication(web.Application):
119
120
120 def __init__(self, ipython_app, kernel_manager, notebook_manager,
121 def __init__(self, ipython_app, kernel_manager, notebook_manager,
121 cluster_manager, session_manager, log, base_url,
122 cluster_manager, session_manager, kernel_spec_manager, log,
122 settings_overrides, jinja_env_options):
123 base_url, settings_overrides, jinja_env_options):
123
124
124 settings = self.init_settings(
125 settings = self.init_settings(
125 ipython_app, kernel_manager, notebook_manager, cluster_manager,
126 ipython_app, kernel_manager, notebook_manager, cluster_manager,
126 session_manager, log, base_url, settings_overrides, jinja_env_options)
127 session_manager, kernel_spec_manager, log, base_url,
128 settings_overrides, jinja_env_options)
127 handlers = self.init_handlers(settings)
129 handlers = self.init_handlers(settings)
128
130
129 super(NotebookWebApplication, self).__init__(handlers, **settings)
131 super(NotebookWebApplication, self).__init__(handlers, **settings)
130
132
131 def init_settings(self, ipython_app, kernel_manager, notebook_manager,
133 def init_settings(self, ipython_app, kernel_manager, notebook_manager,
132 cluster_manager, session_manager, log, base_url,
134 cluster_manager, session_manager, kernel_spec_manager,
133 settings_overrides, jinja_env_options=None):
135 log, base_url, settings_overrides,
136 jinja_env_options=None):
134 # Python < 2.6.5 doesn't accept unicode keys in f(**kwargs), and
137 # Python < 2.6.5 doesn't accept unicode keys in f(**kwargs), and
135 # base_url will always be unicode, which will in turn
138 # base_url will always be unicode, which will in turn
136 # make the patterns unicode, and ultimately result in unicode
139 # make the patterns unicode, and ultimately result in unicode
@@ -162,6 +165,7 b' class NotebookWebApplication(web.Application):'
162 notebook_manager=notebook_manager,
165 notebook_manager=notebook_manager,
163 cluster_manager=cluster_manager,
166 cluster_manager=cluster_manager,
164 session_manager=session_manager,
167 session_manager=session_manager,
168 kernel_spec_manager=kernel_spec_manager,
165
169
166 # IPython stuff
170 # IPython stuff
167 nbextensions_path = ipython_app.nbextensions_path,
171 nbextensions_path = ipython_app.nbextensions_path,
@@ -188,6 +192,7 b' class NotebookWebApplication(web.Application):'
188 handlers.extend(load_handlers('services.clusters.handlers'))
192 handlers.extend(load_handlers('services.clusters.handlers'))
189 handlers.extend(load_handlers('services.sessions.handlers'))
193 handlers.extend(load_handlers('services.sessions.handlers'))
190 handlers.extend(load_handlers('services.nbconvert.handlers'))
194 handlers.extend(load_handlers('services.nbconvert.handlers'))
195 handlers.extend(load_handlers('services.kernelspecs.handlers'))
191 # FIXME: /files/ should be handled by the Contents service when it exists
196 # FIXME: /files/ should be handled by the Contents service when it exists
192 nbm = settings['notebook_manager']
197 nbm = settings['notebook_manager']
193 if hasattr(nbm, 'notebook_dir'):
198 if hasattr(nbm, 'notebook_dir'):
@@ -510,6 +515,11 b' class NotebookApp(BaseIPythonApplication):'
510 help='The cluster manager class to use.'
515 help='The cluster manager class to use.'
511 )
516 )
512
517
518 kernel_spec_manager = Instance(KernelSpecManager)
519
520 def _kernel_spec_manager_default(self):
521 return KernelSpecManager(ipython_dir=self.ipython_dir)
522
513 trust_xheaders = Bool(False, config=True,
523 trust_xheaders = Bool(False, config=True,
514 help=("Whether to trust or not X-Scheme/X-Forwarded-Proto and X-Real-Ip/X-Forwarded-For headers"
524 help=("Whether to trust or not X-Scheme/X-Forwarded-Proto and X-Real-Ip/X-Forwarded-For headers"
515 "sent by the upstream reverse proxy. Necessary if the proxy handles SSL")
525 "sent by the upstream reverse proxy. Necessary if the proxy handles SSL")
@@ -616,7 +626,7 b' class NotebookApp(BaseIPythonApplication):'
616 """initialize tornado webapp and httpserver"""
626 """initialize tornado webapp and httpserver"""
617 self.web_app = NotebookWebApplication(
627 self.web_app = NotebookWebApplication(
618 self, self.kernel_manager, self.notebook_manager,
628 self, self.kernel_manager, self.notebook_manager,
619 self.cluster_manager, self.session_manager,
629 self.cluster_manager, self.session_manager, self.kernel_spec_manager,
620 self.log, self.base_url, self.webapp_settings,
630 self.log, self.base_url, self.webapp_settings,
621 self.jinja_environment_options
631 self.jinja_environment_options
622 )
632 )
@@ -48,6 +48,14 b' class KernelSpec(HasTraits):'
48 kernel_dict = json.load(f)
48 kernel_dict = json.load(f)
49 return cls(resource_dir=resource_dir, **kernel_dict)
49 return cls(resource_dir=resource_dir, **kernel_dict)
50
50
51 def to_json(self):
52 return json.dumps(dict(argv=self.argv,
53 env=self.env,
54 display_name=self.display_name,
55 language=self.language,
56 codemirror_mode=self.codemirror_mode,
57 ))
58
51 def _is_kernel_dir(path):
59 def _is_kernel_dir(path):
52 """Is ``path`` a kernel directory?"""
60 """Is ``path`` a kernel directory?"""
53 return os.path.isdir(path) and os.path.isfile(pjoin(path, 'kernel.json'))
61 return os.path.isdir(path) and os.path.isfile(pjoin(path, 'kernel.json'))
General Comments 0
You need to be logged in to leave comments. Login now