Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Add option to open an IPython console connected to the current notebook #79

Merged
merged 9 commits into from
Jul 12, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion spyder_notebook/notebookplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,18 @@ def get_plugin_actions(self):
_("Open..."),
icon=ima.icon('fileopen'),
triggered=self.open_notebook)
open_console_action = create_action(self,
_("Open console"),
icon=ima.icon('ipython_console'),
triggered=self.open_console)
self.clear_recent_notebooks_action =\
create_action(self, _("Clear this list"),
triggered=self.clear_recent_notebooks)
# Plugin actions
self.menu_actions = [create_nb_action, open_action,
self.recent_notebook_menu, MENU_SEPARATOR,
save_as_action]
save_as_action, MENU_SEPARATOR,
open_console_action]
self.setup_menu_actions()

return self.menu_actions
Expand All @@ -172,6 +177,7 @@ def register_plugin(self):
"""Register plugin in Spyder's main window."""
self.focus_changed.connect(self.main.plugin_focus_changed)
self.main.add_dockwidget(self)
self.ipyconsole = self.main.ipyconsole
self.create_new_client(give_focus=False)
icon_path = os.path.join(PACKAGE_PATH, 'images', 'icon.svg')
self.main.add_to_fileswitcher(self, self.tabwidget, self.clients,
Expand Down Expand Up @@ -375,6 +381,15 @@ def open_notebook(self, filenames=None):
for filename in filenames:
self.create_new_client(filename=filename)

def open_console(self, client=None):
"""Open an IPython console for the given client or the current one."""
if not client:
client = self.get_current_client()
if self.ipyconsole is not None:
kernel_id = client.get_kernel_id()
self.ipyconsole._create_client_for_kernel(kernel_id, None, None,
None)

# ------ Public API (for tabs) --------------------------------------------
def add_tab(self, widget):
"""Add tab."""
Expand Down
27 changes: 4 additions & 23 deletions spyder_notebook/tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
from qtpy.QtCore import Qt, QTimer
from qtpy.QtWidgets import QFileDialog, QApplication, QLineEdit

# Notebook imports
from notebook.utils import url_path_join

# Local imports
from spyder_notebook.notebookplugin import NotebookPlugin

Expand Down Expand Up @@ -85,24 +82,6 @@ def manage_save_dialog(qtbot, fname, directory=LOCATION):
qtbot.keyClick(w, Qt.Key_Enter)


def get_kernel_id(client):
"""Get the kernel id for a client and the sessions url."""
sessions_url = client.add_token(url_path_join(client.server_url,
'api/sessions'))
sessions_req = requests.get(sessions_url).content.decode()
sessions = json.loads(sessions_req)

if os.name == 'nt':
path = client.path.replace('\\', '/')
else:
path = client.path

for session in sessions:
if session['notebook']['path'] == path:
kernel_id = session['kernel']['id']
return (kernel_id, sessions_url)


def is_kernel_up(kernel_id, sessions_url):
"""Determine if the kernel with the id is up."""
sessions_req = requests.get(sessions_url).content.decode()
Expand Down Expand Up @@ -163,8 +142,10 @@ def test_shutdown_notebook_kernel(qtbot):
qtbot.waitUntil(lambda: prompt_present(nbwidget), timeout=NOTEBOOK_UP)

# Get kernel id for the client
qtbot.waitUntil(lambda: get_kernel_id(notebook.get_current_client()) is not None, timeout=NOTEBOOK_UP)
kernel_id, sessions_url = get_kernel_id(notebook.get_current_client())
client = notebook.get_current_client()
qtbot.waitUntil(lambda: client.get_kernel_id() is not None, timeout=NOTEBOOK_UP)
kernel_id = client.get_kernel_id()
sessions_url = client.get_session_url()

# Close the current client
notebook.close_client()
Expand Down
20 changes: 20 additions & 0 deletions spyder_notebook/widgets/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,26 @@ def save(self):
"""Save current notebook."""
self.notebookwidget.click("#save-notbook button")

def get_session_url(self):
"""Get the kernel sessions url of the client."""
return self.add_token(url_path_join(self.server_url, 'api/sessions'))

def get_kernel_id(self):
"""Get the kernel id of the client."""
sessions_url = self.get_session_url()
sessions_req = requests.get(sessions_url).content.decode()
sessions = json.loads(sessions_req)

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']
return kernel_id

def shutdown_kernel(self):
"""Shutdown the kernel of the client."""
sessions_url = self.add_token(url_path_join(self.server_url,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use self.get_session_url here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, we can simplify this whole function by simply calling self.get_kernel_id().

Please do it so we can remove this chunk of repeated code here.

Expand Down