1
# -*- coding: utf-8 -*-
3
# Copyright © 2011 Pierre Raybaut
4
# Licensed under the terms of the MIT License
5
# (see spyderlib/__init__.py for details)
7
"""IPython v0.12+ Plugin"""
9
from spyderlib.qt.QtGui import QHBoxLayout, QMessageBox
10
from spyderlib.qt.QtCore import SIGNAL
13
from spyderlib.baseconfig import _
14
from spyderlib.widgets.ipython import IPythonApp
15
from spyderlib.plugins import SpyderPluginWidget
18
class IPythonPlugin(SpyderPluginWidget):
19
"""Find in files DockWidget"""
20
CONF_SECTION = 'ipython'
21
def __init__(self, parent, connection_file, kernel_widget_id, kernel_name):
22
super(IPythonPlugin, self).__init__(parent)
24
self.already_closed = False
26
self.connection_file = connection_file
27
self.kernel_widget_id = kernel_widget_id
28
self.kernel_name = kernel_name
30
self.ipython_widget = None
33
self.initialize_plugin()
35
def toggle(self, state):
36
"""Toggle widget visibility"""
38
self.dockwidget.setVisible(state)
40
#------ SpyderPluginWidget API ---------------------------------------------
41
def get_plugin_title(self):
42
"""Return widget title"""
43
return _("IPython client (%s)") % self.kernel_name
45
def get_focus_widget(self):
47
Return the widget to give focus to when
48
this plugin's dockwidget is raised on top-level
50
return self.ipython_widget._control
52
def get_plugin_actions(self):
53
"""Return a list of actions related to plugin"""
56
def register_plugin(self):
57
"""Register plugin in Spyder's main window"""
58
argv = ['--existing']+[self.connection_file]
60
iapp = self.main.ipython_app
62
self.main.ipython_app = iapp = IPythonApp()
63
iapp.initialize_all_except_qt(argv=argv)
65
iapp.parse_command_line(argv=argv)
66
exit_callback = self.close_client
67
self.ipython_widget = iapp.new_frontend_from_existing(exit_callback)
69
layout = QHBoxLayout()
70
layout.addWidget(self.ipython_widget)
71
self.setLayout(layout)
73
self.main.add_ipython_frontend(self)
75
self.connect(self.dockwidget, SIGNAL('visibilityChanged(bool)'),
76
lambda state: self.main.plugin_focus_changed())
78
def refresh_plugin(self):
82
def closing_plugin(self, cancelable=False):
83
"""Perform actions before parent main window is closed"""
86
#------ Public API --------------------------------------------------------
87
def close_client(self):
88
"""Closing IPython client and eventually the associated kernel"""
89
if self.already_closed:
91
console = self.main.extconsole
92
index = console.get_shell_index_from_id(self.kernel_widget_id)
94
answer = QMessageBox.question(self, self.get_plugin_title(),
95
_("This IPython frontend will be closed.\n"
96
"Do you want to kill the associated kernel and "
97
"the all of its clients?"),
98
QMessageBox.Yes|QMessageBox.No|QMessageBox.Cancel)
99
if answer == QMessageBox.Yes:
100
console.close_console(index=index)
101
self.main.close_related_ipython_frontends(self)
102
elif answer == QMessageBox.Cancel:
104
self.already_closed = True
106
self.main.remove_ipython_frontend(self)