| 1 |
#!/usr/bin/env python |
| 2 |
"""Example integrating an IPython kernel into a GUI App. |
| 3 |
|
| 4 |
This trivial GUI application internally starts an IPython kernel, to which Qt |
| 5 |
consoles can be connected either by the user at the command line or started |
| 6 |
from the GUI itself, via a button. The GUI can also manipulate one variable in |
| 7 |
the kernel's namespace, and print the namespace to the console. |
| 8 |
|
| 9 |
Play with it by running the script and then opening one or more consoles, and |
| 10 |
pushing the 'Counter++' and 'Namespace' buttons. |
| 11 |
|
| 12 |
Upon exit, it should automatically close all consoles opened from the GUI. |
| 13 |
|
| 14 |
Consoles attached separately from a terminal will not be terminated, though |
| 15 |
they will notice that their kernel died. |
| 16 |
""" |
| 17 |
# ----------------------------------------------------------------------------- |
| 18 |
# Imports |
| 19 |
# ----------------------------------------------------------------------------- |
| 20 |
|
| 21 |
from internal_ipkernel import InternalIPKernel |
| 22 |
from PyQt4 import Qt |
| 23 |
|
| 24 |
|
| 25 |
# ----------------------------------------------------------------------------- |
| 26 |
# Functions and classes |
| 27 |
# ----------------------------------------------------------------------------- |
| 28 |
class SimpleWindow(Qt.QWidget, InternalIPKernel): |
| 29 |
"""A custom Qt widget for IPykernel.""" |
| 30 |
|
| 31 |
def __init__(self, app): |
| 32 |
"""Initialize the widget.""" |
| 33 |
Qt.QWidget.__init__(self) |
| 34 |
self.app = app |
| 35 |
self.add_widgets() |
| 36 |
self.init_ipkernel("qt") |
| 37 |
|
| 38 |
def add_widgets(self): |
| 39 |
"""Add the widget.""" |
| 40 |
self.setGeometry(300, 300, 400, 70) |
| 41 |
self.setWindowTitle("IPython in your app") |
| 42 |
|
| 43 |
# Add simple buttons: |
| 44 |
console = Qt.QPushButton("Qt Console", self) |
| 45 |
console.setGeometry(10, 10, 100, 35) |
| 46 |
self.connect(console, Qt.SIGNAL("clicked()"), self.new_qt_console) |
| 47 |
|
| 48 |
namespace = Qt.QPushButton("Namespace", self) |
| 49 |
namespace.setGeometry(120, 10, 100, 35) |
| 50 |
self.connect(namespace, Qt.SIGNAL("clicked()"), self.print_namespace) |
| 51 |
|
| 52 |
count = Qt.QPushButton("Count++", self) |
| 53 |
count.setGeometry(230, 10, 80, 35) |
| 54 |
self.connect(count, Qt.SIGNAL("clicked()"), self.count) |
| 55 |
|
| 56 |
# Quit and cleanup |
| 57 |
quit = Qt.QPushButton("Quit", self) |
| 58 |
quit.setGeometry(320, 10, 60, 35) |
| 59 |
self.connect(quit, Qt.SIGNAL("clicked()"), Qt.qApp, Qt.SLOT("quit()")) |
| 60 |
|
| 61 |
self.app.connect(self.app, Qt.SIGNAL("lastWindowClosed()"), self.app, Qt.SLOT("quit()")) |
| 62 |
|
| 63 |
self.app.aboutToQuit.connect(self.cleanup_consoles) |
| 64 |
|
| 65 |
|
| 66 |
# ----------------------------------------------------------------------------- |
| 67 |
# Main script |
| 68 |
# ----------------------------------------------------------------------------- |
| 69 |
|
| 70 |
if __name__ == "__main__": |
| 71 |
app = Qt.QApplication([]) |
| 72 |
# Create our window |
| 73 |
win = SimpleWindow(app) |
| 74 |
win.show() |
| 75 |
|
| 76 |
# Very important, IPython-specific step: this gets GUI event loop |
| 77 |
# integration going, and it replaces calling app.exec_() |
| 78 |
win.ipkernel.start() |