Show More
@@ -36,22 +36,27 b' class MainKernelHandler(IPythonHandler):' | |||||
36 | @web.authenticated |
|
36 | @web.authenticated | |
37 | def get(self): |
|
37 | def get(self): | |
38 | km = self.kernel_manager |
|
38 | km = self.kernel_manager | |
39 |
self.finish(jsonapi.dumps(km.list_kernel |
|
39 | self.finish(jsonapi.dumps(km.list_kernels())) | |
40 |
|
40 | |||
41 | @web.authenticated |
|
41 | @web.authenticated | |
42 | def post(self): |
|
42 | def post(self): | |
43 | km = self.kernel_manager |
|
43 | km = self.kernel_manager | |
44 | nbm = self.notebook_manager |
|
44 | nbm = self.notebook_manager | |
45 | notebook_id = self.get_argument('notebook', default=None) |
|
45 | kernel_id = km.start_kernel(cwd=nbm.notebook_dir) | |
46 | kernel_id = km.start_kernel(notebook_id, cwd=nbm.notebook_dir) |
|
46 | model = km.kernel_model(kernel_id, self.ws_url) | |
47 | data = {'ws_url':self.ws_url,'kernel_id':kernel_id} |
|
|||
48 | self.set_header('Location', '{0}kernels/{1}'.format(self.base_kernel_url, kernel_id)) |
|
47 | self.set_header('Location', '{0}kernels/{1}'.format(self.base_kernel_url, kernel_id)) | |
49 |
self.finish(jsonapi.dumps( |
|
48 | self.finish(jsonapi.dumps(model)) | |
50 |
|
49 | |||
51 |
|
50 | |||
52 | class KernelHandler(IPythonHandler): |
|
51 | class KernelHandler(IPythonHandler): | |
53 |
|
52 | |||
54 | SUPPORTED_METHODS = ('DELETE') |
|
53 | SUPPORTED_METHODS = ('DELETE', 'GET') | |
|
54 | ||||
|
55 | @web.authenticated | |||
|
56 | def get(self, kernel_id): | |||
|
57 | km = self.kernel_manager | |||
|
58 | model = km.kernel_model(kernel_id,self.ws_url) | |||
|
59 | self.finish(jsonapi.dumps(model)) | |||
55 |
|
60 | |||
56 | @web.authenticated |
|
61 | @web.authenticated | |
57 | def delete(self, kernel_id): |
|
62 | def delete(self, kernel_id): | |
@@ -71,9 +76,9 b' class KernelActionHandler(IPythonHandler):' | |||||
71 | self.set_status(204) |
|
76 | self.set_status(204) | |
72 | if action == 'restart': |
|
77 | if action == 'restart': | |
73 | km.restart_kernel(kernel_id) |
|
78 | km.restart_kernel(kernel_id) | |
74 | data = {'ws_url':self.ws_url, 'kernel_id':kernel_id} |
|
79 | model = km.kernel_model(kernel_id,self.ws_url) | |
75 | self.set_header('Location', '{0}kernels/{1}'.format(self.base_kernel_url, kernel_id)) |
|
80 | self.set_header('Location', '{0}kernels/{1}'.format(self.base_kernel_url, kernel_id)) | |
76 |
self.write(jsonapi.dumps( |
|
81 | self.write(jsonapi.dumps(model)) | |
77 | self.finish() |
|
82 | self.finish() | |
78 |
|
83 | |||
79 |
|
84 | |||
@@ -173,10 +178,10 b' _kernel_id_regex = r"(?P<kernel_id>\\w+-\\w+-\\w+-\\w+-\\w+)"' | |||||
173 | _kernel_action_regex = r"(?P<action>restart|interrupt)" |
|
178 | _kernel_action_regex = r"(?P<action>restart|interrupt)" | |
174 |
|
179 | |||
175 | default_handlers = [ |
|
180 | default_handlers = [ | |
176 | (r"/kernels", MainKernelHandler), |
|
181 | (r"/api/kernels", MainKernelHandler), | |
177 | (r"/kernels/%s" % _kernel_id_regex, KernelHandler), |
|
182 | (r"/api/kernels/%s" % _kernel_id_regex, KernelHandler), | |
178 | (r"/kernels/%s/%s" % (_kernel_id_regex, _kernel_action_regex), KernelActionHandler), |
|
183 | (r"/api/kernels/%s/%s" % (_kernel_id_regex, _kernel_action_regex), KernelActionHandler), | |
179 | (r"/kernels/%s/iopub" % _kernel_id_regex, IOPubHandler), |
|
184 | (r"/api/kernels/%s/iopub" % _kernel_id_regex, IOPubHandler), | |
180 | (r"/kernels/%s/shell" % _kernel_id_regex, ShellHandler), |
|
185 | (r"/api/kernels/%s/shell" % _kernel_id_regex, ShellHandler), | |
181 | (r"/kernels/%s/stdin" % _kernel_id_regex, StdinHandler) |
|
186 | (r"/api/kernels/%s/stdin" % _kernel_id_regex, StdinHandler) | |
182 | ] |
|
187 | ] |
@@ -35,56 +35,31 b' class MappingKernelManager(MultiKernelManager):' | |||||
35 | return "IPython.kernel.ioloop.IOLoopKernelManager" |
|
35 | return "IPython.kernel.ioloop.IOLoopKernelManager" | |
36 |
|
36 | |||
37 | kernel_argv = List(Unicode) |
|
37 | kernel_argv = List(Unicode) | |
38 |
|
38 | kernels = [] | ||
39 | _notebook_mapping = Dict() |
|
|||
40 |
|
39 | |||
41 | #------------------------------------------------------------------------- |
|
40 | #------------------------------------------------------------------------- | |
42 | # Methods for managing kernels and sessions |
|
41 | # Methods for managing kernels and sessions | |
43 | #------------------------------------------------------------------------- |
|
42 | #------------------------------------------------------------------------- | |
44 |
|
43 | |||
45 | def kernel_for_notebook(self, notebook_id): |
|
|||
46 | """Return the kernel_id for a notebook_id or None.""" |
|
|||
47 | return self._notebook_mapping.get(notebook_id) |
|
|||
48 |
|
||||
49 | def set_kernel_for_notebook(self, notebook_id, kernel_id): |
|
|||
50 | """Associate a notebook with a kernel.""" |
|
|||
51 | if notebook_id is not None: |
|
|||
52 | self._notebook_mapping[notebook_id] = kernel_id |
|
|||
53 |
|
||||
54 | def notebook_for_kernel(self, kernel_id): |
|
|||
55 | """Return the notebook_id for a kernel_id or None.""" |
|
|||
56 | for notebook_id, kid in self._notebook_mapping.iteritems(): |
|
|||
57 | if kernel_id == kid: |
|
|||
58 | return notebook_id |
|
|||
59 | return None |
|
|||
60 |
|
||||
61 | def delete_mapping_for_kernel(self, kernel_id): |
|
|||
62 | """Remove the kernel/notebook mapping for kernel_id.""" |
|
|||
63 | notebook_id = self.notebook_for_kernel(kernel_id) |
|
|||
64 | if notebook_id is not None: |
|
|||
65 | del self._notebook_mapping[notebook_id] |
|
|||
66 |
|
||||
67 | def _handle_kernel_died(self, kernel_id): |
|
44 | def _handle_kernel_died(self, kernel_id): | |
68 | """notice that a kernel died""" |
|
45 | """notice that a kernel died""" | |
69 | self.log.warn("Kernel %s died, removing from map.", kernel_id) |
|
46 | self.log.warn("Kernel %s died, removing from map.", kernel_id) | |
70 | self.delete_mapping_for_kernel(kernel_id) |
|
|||
71 | self.remove_kernel(kernel_id) |
|
47 | self.remove_kernel(kernel_id) | |
72 |
|
48 | |||
73 |
def start_kernel(self, |
|
49 | def start_kernel(self, **kwargs): | |
74 |
"""Start a kernel for a |
|
50 | """Start a kernel for a session an return its kernel_id. | |
75 |
|
51 | |||
76 | Parameters |
|
52 | Parameters | |
77 | ---------- |
|
53 | ---------- | |
78 |
|
|
54 | session_id : uuid | |
79 |
The uuid of the |
|
55 | The uuid of the session to associate the new kernel with. If this | |
80 |
is not None, this kernel will be persistent whenever the |
|
56 | is not None, this kernel will be persistent whenever the session | |
81 | requests a kernel. |
|
57 | requests a kernel. | |
82 | """ |
|
58 | """ | |
83 | kernel_id = self.kernel_for_notebook(notebook_id) |
|
59 | kernel_id = None | |
84 | if kernel_id is None: |
|
60 | if kernel_id is None: | |
85 | kwargs['extra_arguments'] = self.kernel_argv |
|
61 | kwargs['extra_arguments'] = self.kernel_argv | |
86 | kernel_id = super(MappingKernelManager, self).start_kernel(**kwargs) |
|
62 | kernel_id = super(MappingKernelManager, self).start_kernel(**kwargs) | |
87 | self.set_kernel_for_notebook(notebook_id, kernel_id) |
|
|||
88 | self.log.info("Kernel started: %s" % kernel_id) |
|
63 | self.log.info("Kernel started: %s" % kernel_id) | |
89 | self.log.debug("Kernel args: %r" % kwargs) |
|
64 | self.log.debug("Kernel args: %r" % kwargs) | |
90 | # register callback for failed auto-restart |
|
65 | # register callback for failed auto-restart | |
@@ -99,12 +74,23 b' class MappingKernelManager(MultiKernelManager):' | |||||
99 |
|
74 | |||
100 | def shutdown_kernel(self, kernel_id, now=False): |
|
75 | def shutdown_kernel(self, kernel_id, now=False): | |
101 | """Shutdown a kernel by kernel_id""" |
|
76 | """Shutdown a kernel by kernel_id""" | |
|
77 | i = 0 | |||
|
78 | for kernel in self.kernels: | |||
|
79 | if kernel['kernel_id'] == kernel_id: | |||
|
80 | del self.kernels[i] | |||
|
81 | i = i+1 | |||
102 | super(MappingKernelManager, self).shutdown_kernel(kernel_id, now=now) |
|
82 | super(MappingKernelManager, self).shutdown_kernel(kernel_id, now=now) | |
103 | self.delete_mapping_for_kernel(kernel_id) |
|
83 | ||
|
84 | def kernel_model(self, kernel_id, ws_url): | |||
|
85 | model = {"kernel_id":kernel_id, "ws_url": ws_url} | |||
|
86 | self.kernels.append(model) | |||
|
87 | return model | |||
|
88 | ||||
|
89 | def list_kernels(self): | |||
|
90 | return self.kernels | |||
104 |
|
91 | |||
105 | # override _check_kernel_id to raise 404 instead of KeyError |
|
92 | # override _check_kernel_id to raise 404 instead of KeyError | |
106 | def _check_kernel_id(self, kernel_id): |
|
93 | def _check_kernel_id(self, kernel_id): | |
107 | """Check a that a kernel_id exists and raise 404 if not.""" |
|
94 | """Check a that a kernel_id exists and raise 404 if not.""" | |
108 | if kernel_id not in self: |
|
95 | if kernel_id not in self: | |
109 | raise web.HTTPError(404, u'Kernel does not exist: %s' % kernel_id) |
|
96 | raise web.HTTPError(404, u'Kernel does not exist: %s' % kernel_id) | |
110 |
|
General Comments 0
You need to be logged in to leave comments.
Login now