Skip to content

Commit

Permalink
Merge pull request #26 from dalthviz/fixes_issue_5
Browse files Browse the repository at this point in the history
PR: Shutdown kernel of the notebook before closing it
  • Loading branch information
ccordoba12 authored Jan 16, 2017
2 parents a3a1423 + 817e54c commit fcd366b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions spyder_notebook/notebookplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import subprocess
import sys
import tempfile

# Qt imports
from qtpy.QtWidgets import QApplication, QMessageBox, QVBoxLayout, QMenu
from qtpy.QtCore import Qt, Signal
Expand Down Expand Up @@ -224,6 +225,7 @@ def close_client(self, index=None, client=None):
client = self.tabwidget.widget(index)

# TODO: Eliminate the notebook from disk if it's an Untitled one
client.shutdown_kernel()
client.close()

# Note: notebook index may have changed after closing related widgets
Expand Down
34 changes: 33 additions & 1 deletion spyder_notebook/widgets/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@

import os
import os.path as osp
import json
from string import Template
import sys

# Qt imports
from qtpy.QtCore import QUrl
from qtpy.QtWebEngineWidgets import (QWebEnginePage, QWebEngineSettings,
WEBENGINE)
from qtpy.QtWidgets import QMenu, QVBoxLayout, QWidget
from qtpy.QtWidgets import QMenu, QVBoxLayout, QWidget, QMessageBox

# Notebook imports
from notebook.utils import url_path_join, url_escape

# Third-party imports
import requests

# Spyder imports
from spyder.config.base import _, get_image_path, get_module_source_path
from spyder.py3compat import is_text_string
Expand Down Expand Up @@ -168,6 +172,34 @@ def save(self):
"""Save current notebook."""
self.notebookwidget.click('#save-notbook button')

def shutdown_kernel(self):
"""Shutdown the kernel of the client."""
sessions_url = url_path_join(self.server_url, 'api/sessions')
sessions_req = requests.get(sessions_url).content.decode()
sessions = json.loads(sessions_req)
kernel_id = None
if os.name == 'nt':
path = self.path.replace('\\', '/')
else:
path = self.path
for session in sessions:
if session['notebook']['path'] == path:
kernel_id = session['kernel']['id']
break
if kernel_id:
delete_url = url_path_join(self.server_url,
'api/kernels/',
kernel_id)
delete_req = requests.delete(delete_url)
if delete_req.status_code != 204:
QMessageBox.warning(
self,
_("Server error"),
_("The Jupyter Notebook server "
"failed to shutdown the kernel "
"associated with this notebook. "
"If you want to shut it down, "
"you'll have to close Spyder."))

#-----------------------------------------------------------------------------
# Tests
Expand Down

0 comments on commit fcd366b

Please sign in to comment.