Skip to content

Commit

Permalink
Added command to shutdown the kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
paulofilip3 committed Feb 2, 2014
1 parent 4f61859 commit 807c37a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
5 changes: 3 additions & 2 deletions Default.sublime-commands
@@ -1,8 +1,9 @@
[
{ "caption": "Open IPython Notebook", "command": "inb_prompt_list_notebooks" },
{ "caption": "Save IPython Notebook", "command": "inb_save_notebook" },
{ "caption": "Open IPython Notebook", "command": "inb_prompt_list_notebooks" },
{ "caption": "Save IPython Notebook", "command": "inb_save_notebook" },
{ "caption": "Restart IPython Notebook Kernel", "command": "inb_restart_kernel" },
{ "caption": "Interrupt IPython Notebook Kernel", "command": "inb_interrupt_kernel" },
{ "caption": "Shutdown IPython Notebook Kernel", "command": "inb_shutdown_kernel" },
{ "caption": "Open Current Notebook As Ipynb File", "command": "inb_open_as_ipynb" },
{ "caption": "Rename IPython Notebook", "command": "inb_rename_notebook" }
]
10 changes: 10 additions & 0 deletions ipy_connection.py
Expand Up @@ -286,6 +286,15 @@ def interrupt_kernel(self):
req = urlopen(url, data=bytearray(b""))
req.read()

def shutdown_kernel(self):
url = self.baseurl + "/kernels/" + self.kernel_id
req = Request(url)
req.add_header("Content-Type", "application/json")
req.get_method = lambda: "DELETE"
data = urlopen(req)
data.read()
self.status_callback("closed")

def get_notebook(self):
req = urlopen(self.notebook_url)
try:
Expand All @@ -303,6 +312,7 @@ def save_notebook(self, notebook):
request.get_method = lambda: "PUT"
data = urlopen(request)
data.read()
self.status_callback("idle")

def on_iopub_msg(self, msg):
m = json.loads(msg)
Expand Down
11 changes: 8 additions & 3 deletions ipy_view.py
Expand Up @@ -189,8 +189,8 @@ def is_R_cell(self):
return (len(code) >= 3) and (code[:3] == '%%R')

def check_R(self):
if self.old_is_R != self.is_R_cell():
self.update_prompt_number()
if self.old_is_R != self.is_R_cell():
self.update_prompt_number()

def rewrite_prompt_number(self, edit):
if (self.prompt == self.old_prompt_number) and (self.old_is_R == self.is_R_cell()):
Expand Down Expand Up @@ -477,6 +477,11 @@ def restart_kernel(self):
cell.running = False
self.kernel.restart_kernel()

def shutdown_kernel(self):
for cell in self.cells:
if isinstance(cell, CodeCellView):
cell.running = False
self.kernel.shutdown_kernel()

def on_status(self, execution_state):
def set_status():
Expand Down Expand Up @@ -671,6 +676,6 @@ def get_nb_view(self, view):
def on_close(self, view):
id = view.id()
if id in self.views:
del self.views[id]
del self.views[id]

manager = NotebookViewManager()
5 changes: 5 additions & 0 deletions subl_ipy_notebook.py
Expand Up @@ -141,6 +141,11 @@ def run(self, edit):
def description(self):
return "Save IPython notebook"

class InbShutdownKernelCommand(sublime_plugin.TextCommand):
def run(self, edit):
nbview = manager.get_nb_view(self.view)
if nbview and nbview.kernel:
nbview.kernel.shutdown_kernel()

class InbBackspaceCommand(sublime_plugin.TextCommand):
def run(self, edit):
Expand Down

0 comments on commit 807c37a

Please sign in to comment.