Skip to content

Commit 807c37a

Browse files
committed
Added command to shutdown the kernel
1 parent 4f61859 commit 807c37a

4 files changed

Lines changed: 26 additions & 5 deletions

File tree

Default.sublime-commands

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
[
2-
{ "caption": "Open IPython Notebook", "command": "inb_prompt_list_notebooks" },
3-
{ "caption": "Save IPython Notebook", "command": "inb_save_notebook" },
2+
{ "caption": "Open IPython Notebook", "command": "inb_prompt_list_notebooks" },
3+
{ "caption": "Save IPython Notebook", "command": "inb_save_notebook" },
44
{ "caption": "Restart IPython Notebook Kernel", "command": "inb_restart_kernel" },
55
{ "caption": "Interrupt IPython Notebook Kernel", "command": "inb_interrupt_kernel" },
6+
{ "caption": "Shutdown IPython Notebook Kernel", "command": "inb_shutdown_kernel" },
67
{ "caption": "Open Current Notebook As Ipynb File", "command": "inb_open_as_ipynb" },
78
{ "caption": "Rename IPython Notebook", "command": "inb_rename_notebook" }
89
]

ipy_connection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,15 @@ def interrupt_kernel(self):
286286
req = urlopen(url, data=bytearray(b""))
287287
req.read()
288288

289+
def shutdown_kernel(self):
290+
url = self.baseurl + "/kernels/" + self.kernel_id
291+
req = Request(url)
292+
req.add_header("Content-Type", "application/json")
293+
req.get_method = lambda: "DELETE"
294+
data = urlopen(req)
295+
data.read()
296+
self.status_callback("closed")
297+
289298
def get_notebook(self):
290299
req = urlopen(self.notebook_url)
291300
try:
@@ -303,6 +312,7 @@ def save_notebook(self, notebook):
303312
request.get_method = lambda: "PUT"
304313
data = urlopen(request)
305314
data.read()
315+
self.status_callback("idle")
306316

307317
def on_iopub_msg(self, msg):
308318
m = json.loads(msg)

ipy_view.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ def is_R_cell(self):
189189
return (len(code) >= 3) and (code[:3] == '%%R')
190190

191191
def check_R(self):
192-
if self.old_is_R != self.is_R_cell():
193-
self.update_prompt_number()
192+
if self.old_is_R != self.is_R_cell():
193+
self.update_prompt_number()
194194

195195
def rewrite_prompt_number(self, edit):
196196
if (self.prompt == self.old_prompt_number) and (self.old_is_R == self.is_R_cell()):
@@ -477,6 +477,11 @@ def restart_kernel(self):
477477
cell.running = False
478478
self.kernel.restart_kernel()
479479

480+
def shutdown_kernel(self):
481+
for cell in self.cells:
482+
if isinstance(cell, CodeCellView):
483+
cell.running = False
484+
self.kernel.shutdown_kernel()
480485

481486
def on_status(self, execution_state):
482487
def set_status():
@@ -671,6 +676,6 @@ def get_nb_view(self, view):
671676
def on_close(self, view):
672677
id = view.id()
673678
if id in self.views:
674-
del self.views[id]
679+
del self.views[id]
675680

676681
manager = NotebookViewManager()

subl_ipy_notebook.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ def run(self, edit):
141141
def description(self):
142142
return "Save IPython notebook"
143143

144+
class InbShutdownKernelCommand(sublime_plugin.TextCommand):
145+
def run(self, edit):
146+
nbview = manager.get_nb_view(self.view)
147+
if nbview and nbview.kernel:
148+
nbview.kernel.shutdown_kernel()
144149

145150
class InbBackspaceCommand(sublime_plugin.TextCommand):
146151
def run(self, edit):

0 commit comments

Comments
 (0)