Skip to content

Commit

Permalink
Update to use Spyder's latest API
Browse files Browse the repository at this point in the history
  • Loading branch information
ccordoba12 committed Jun 13, 2022
1 parent ed5c188 commit 2269e98
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 53 deletions.
16 changes: 13 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,25 @@ def run(self):
sys.exit(1)


REQUIREMENTS = ['spyder>=5.0.4,<6', 'jinja2', 'jupyter_core', 'nbformat',
'notebook>=4.3', 'qtpy', 'qdarkstyle', 'requests', 'traitlets']
REQUIREMENTS = [
'spyder>=5.3.1,<6',
'jinja2',
'jupyter_core',
'nbformat',
'notebook>=4.3',
'qtpy',
'qdarkstyle',
'requests',
'traitlets'
]


setup(
name='spyder-notebook',
version=get_version(),
cmdclass={'sdist': my_sdist},
keywords='spyder jupyter notebook',
python_requires='>=3.6',
python_requires='>=3.7',
url='/~https://github.com/spyder-ide/spyder-notebook',
license='MIT',
author='Spyder Development Team',
Expand Down
43 changes: 28 additions & 15 deletions spyder_notebook/notebookplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

# Spyder imports
from spyder.api.plugins import Plugins, SpyderDockablePlugin
from spyder.api.plugin_registration.decorators import (
on_plugin_available, on_plugin_teardown)
from spyder.utils.switcher import shorten_paths

# Local imports
Expand Down Expand Up @@ -48,7 +50,8 @@ class NotebookPlugin(SpyderDockablePlugin):

# ---- SpyderDockablePlugin API
# ------------------------------------------------------------------------
def get_name(self):
@staticmethod
def get_name():
"""Return plugin name."""
title = _('Notebook')
return title
Expand All @@ -61,38 +64,48 @@ def get_icon(self):
"""Return plugin icon."""
return self.create_icon('notebook')

def on_close(self, cancelable=False):
return self.get_widget().on_close(cancelable=cancelable)

def register(self):
def on_initialize(self):
"""Register plugin in Spyder's main window."""
self.focus_changed.connect(self.main.plugin_focus_changed)
ipyconsole = self.get_plugin(Plugins.IPythonConsole)
preferences = self.get_plugin(Plugins.Preferences)

preferences.register_plugin_preferences(self)

if ipyconsole:
self.get_widget().sig_open_console_requested.connect(
self.open_console)

# Connect to switcher
self.switcher = self.main.switcher
self.switcher.sig_mode_selected.connect(self.handle_switcher_modes)
self.switcher.sig_item_selected.connect(
self.handle_switcher_selection)

@on_plugin_available(plugin=Plugins.Preferences)
def on_preferences_available(self):
preferences = self.get_plugin(Plugins.Preferences)
preferences.register_plugin_preferences(self)

@on_plugin_available(plugin=Plugins.IPythonConsole)
def on_ipyconsole_available(self):
self.get_widget().sig_open_console_requested.connect(
self.open_console)

@on_plugin_teardown(plugin=Plugins.Preferences)
def on_preferences_teardown(self):
preferences = self.get_plugin(Plugins.Preferences)
preferences.deregister_plugin_preferences(self)

@on_plugin_teardown(plugin=Plugins.IPythonConsole)
def on_ipyconsole_teardown(self):
self.get_widget().sig_open_console_requested.disconnect(
self.open_console)

def on_mainwindow_visible(self):
self.get_widget().open_previous_session()

# ------ Public API -------------------------------------------------------
def open_console(self, kernel_id, tab_name):
"""Open an IPython console as requested."""
ipyconsole = self.get_plugin(Plugins.IPythonConsole)
ipyconsole._create_client_for_kernel(kernel_id, None, None, None)
ipyconsole.get_widget()._create_client_for_kernel(
kernel_id, None, None, None)
ipyclient = ipyconsole.get_current_client()
ipyclient.allow_rename = False
ipyconsole.rename_client_tab(ipyclient, tab_name)
ipyconsole.get_widget().rename_client_tab(ipyclient, tab_name)

# ------ Public API (for FileSwitcher) ------------------------------------
def handle_switcher_modes(self, mode):
Expand Down
2 changes: 1 addition & 1 deletion spyder_notebook/utils/servermanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ServerState(enum.Enum):
TIMED_OUT = 5


class ServerProcess():
class ServerProcess:
"""
Process executing a notebook server.
Expand Down
48 changes: 20 additions & 28 deletions spyder_notebook/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,35 +116,30 @@ def setup(self):
icon=self.create_icon('filenew'),
triggered=self.create_new_client
)

open_notebook_action = self.create_action(
NotebookMainWidgetActions.Open,
text=_("Open..."),
icon=self.create_icon('fileopen'),
triggered=self.open_notebook
)

self.save_as_action = self.create_action(
NotebookMainWidgetActions.SaveAs,
text=_("Save as..."),
icon=self.create_icon('filesaveas'),
triggered=self.save_as
)

self.open_console_action = self.create_action(
NotebookMainWidgetActions.OpenConsole,
text=_("Open console"),
icon=self.create_icon('ipython_console'),
triggered=self.open_console
)

self.server_info_action = self.create_action(
NotebookMainWidgetActions.ServerInfo,
text=_('Server info...'),
icon=self.create_icon('log'),
triggered=self.view_servers
)

self.clear_recent_notebooks_action = self.create_action(
NotebookMainWidgetActions.ClearRecentNotebooks,
text=_("Clear this list"),
Expand Down Expand Up @@ -172,13 +167,11 @@ def setup(self):
menu=options_menu,
section=NotebookMainWidgetOptionsMenuSections.SaveAs,
)

self.add_item_to_menu(
self.open_console_action,
menu=options_menu,
section=NotebookMainWidgetOptionsMenuSections.Console,
)

self.add_item_to_menu(
self.server_info_action,
menu=options_menu,
Expand All @@ -199,6 +192,26 @@ def update_actions(self):
self.save_as_action.setEnabled(False)
self.open_console_action.setEnabled(False)

def on_close(self):
"""
Perform actions before parent main window is closed.
This function closes all tabs, shuts down all notebook server and
stores the file names of all opened notebooks that are not temporary
and all notebooks in the 'Open recent' menu in Spyder's config.
"""
opened_notebooks = []
for client_index in range(self.tabwidget.count()):
client = self.tabwidget.widget(client_index)
if (not self.tabwidget.is_welcome_client(client)
and not self.tabwidget.is_newly_created(client)):
opened_notebooks.append(client.filename)
client.close()

self.set_conf('recent_notebooks', self.recent_notebooks)
self.set_conf('opened_notebooks', opened_notebooks)
self.server_manager.shutdown_all_servers()

# ---- Public API
# ------------------------------------------------------------------------
@property
Expand Down Expand Up @@ -338,24 +351,3 @@ def clear_recent_notebooks(self):
"""Clear the list of recent notebooks."""
self.recent_notebooks = []
self.update_recent_notebooks_menu()

def on_close(self, cancelable=False):
"""
Perform actions before parent main window is closed.
This function closes all tabs. It stores the file names of all opened
notebooks that are not temporary and all notebooks in the 'Open recent'
menu in the config.
"""
opened_notebooks = []
for client_index in range(self.tabwidget.count()):
client = self.tabwidget.widget(client_index)
if (not self.tabwidget.is_welcome_client(client)
and not self.tabwidget.is_newly_created(client)):
opened_notebooks.append(client.filename)
client.close()

self.set_conf('recent_notebooks', self.recent_notebooks)
self.set_conf('opened_notebooks', opened_notebooks)
self.server_manager.shutdown_all_servers()
return True
7 changes: 1 addition & 6 deletions spyder_notebook/widgets/notebooktabwidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,7 @@ def get_interpreter(self):
-------
The file name of the interpreter
"""
if self.get_conf(option='default', section='main_interpreter'):
pyexec = get_python_executable()
else:
pyexec = self.get_conf(option='executable', section='main_interpreter')
if not is_python_interpreter(pyexec):
pyexec = get_python_executable()
pyexec = self.get_conf(option='executable', section='main_interpreter')
return pyexec

def maybe_create_welcome_client(self):
Expand Down

0 comments on commit 2269e98

Please sign in to comment.