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