update to latest ipykernel

This commit is contained in:
Vitor Santos Costa
2017-05-14 11:27:44 +01:00
parent 517667c1d5
commit 386c88e372
92 changed files with 10935 additions and 1009 deletions

View File

@@ -0,0 +1,46 @@
from __future__ import print_function
import os
from IPython.qt.console.rich_ipython_widget import RichIPythonWidget
from IPython.qt.inprocess import QtInProcessKernelManager
from IPython.lib import guisupport
def print_process_id():
print('Process ID is:', os.getpid())
def main():
# Print the ID of the main process
print_process_id()
app = guisupport.get_app_qt4()
# Create an in-process kernel
# >>> print_process_id()
# will print the same process ID as the main process
kernel_manager = QtInProcessKernelManager()
kernel_manager.start_kernel()
kernel = kernel_manager.kernel
kernel.gui = 'qt4'
kernel.shell.push({'foo': 43, 'print_process_id': print_process_id})
kernel_client = kernel_manager.client()
kernel_client.start_channels()
def stop():
kernel_client.stop_channels()
kernel_manager.shutdown_kernel()
app.exit()
control = RichIPythonWidget()
control.kernel_manager = kernel_manager
control.kernel_client = kernel_client
control.exit_requested.connect(stop)
control.show()
guisupport.start_event_loop_qt4(app)
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,31 @@
from __future__ import print_function
import os
from IPython.kernel.inprocess import InProcessKernelManager
from IPython.terminal.console.interactiveshell import ZMQTerminalInteractiveShell
def print_process_id():
print('Process ID is:', os.getpid())
def main():
print_process_id()
# Create an in-process kernel
# >>> print_process_id()
# will print the same process ID as the main process
kernel_manager = InProcessKernelManager()
kernel_manager.start_kernel()
kernel = kernel_manager.kernel
kernel.gui = 'qt4'
kernel.shell.push({'foo': 43, 'print_process_id': print_process_id})
client = kernel_manager.client()
client.start_channels()
shell = ZMQTerminalInteractiveShell(manager=kernel_manager, client=client)
shell.mainloop()
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,55 @@
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import sys
from IPython.lib.kernel import connect_qtconsole
from IPython.kernel.zmq.kernelapp import YAP_KernelApp
#-----------------------------------------------------------------------------
# Functions and classes
#-----------------------------------------------------------------------------
def mpl_kernel(gui):
"""Launch and return an IPython kernel with matplotlib support for the desired gui
"""
kernel = YAP_KernelApp.instance()
kernel.initialize(['python', '--matplotlib=%s' % gui,
#'--log-level=10'
])
return kernel
class InternalYAPKernel(object):
def init_yapkernel(self, backend):
# Start IPython kernel with GUI event loop and mpl support
self.yapkernel = mpl_kernel(backend)
# To create and track active qt consoles
self.consoles = []
# This application will also act on the shell user namespace
self.namespace = self.yapkernel.shell.user_ns
# Example: a variable that will be seen by the user in the shell, and
# that the GUI modifies (the 'Counter++' button increments it):
self.namespace['app_counter'] = 0
#self.namespace['yapkernel'] = self.yapkernel # dbg
def print_namespace(self, evt=None):
print("\n***Variables in User namespace***")
for k, v in self.namespace.items():
if not k.startswith('_'):
print('%s -> %r' % (k, v))
sys.stdout.flush()
def new_qt_console(self, evt=None):
"""start a new qtconsole connected to our kernel"""
return connect_qtconsole(self.yapkernel.abs_connection_file, profile=self.yapkernel.profile)
def count(self, evt=None):
self.namespace['app_counter'] += 1
def cleanup_consoles(self, evt=None):
for c in self.consoles:
c.kill()

View File

@@ -0,0 +1,75 @@
#!/usr/bin/env python
"""Example integrating an IPython kernel into a GUI App.
This trivial GUI application internally starts an IPython kernel, to which Qt
consoles can be connected either by the user at the command line or started
from the GUI itself, via a button. The GUI can also manipulate one variable in
the kernel's namespace, and print the namespace to the console.
Play with it by running the script and then opening one or more consoles, and
pushing the 'Counter++' and 'Namespace' buttons.
Upon exit, it should automatically close all consoles opened from the GUI.
Consoles attached separately from a terminal will not be terminated, though
they will notice that their kernel died.
"""
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
from PyQt4 import Qt
from internal_yapkernel import InternalYAPKernel
#-----------------------------------------------------------------------------
# Functions and classes
#-----------------------------------------------------------------------------
class SimpleWindow(Qt.QWidget, InternalYAPKernel):
def __init__(self, app):
Qt.QWidget.__init__(self)
self.app = app
self.add_widgets()
self.init_yapkernel('qt')
def add_widgets(self):
self.setGeometry(300, 300, 400, 70)
self.setWindowTitle('IPython in your app')
# Add simple buttons:
console = Qt.QPushButton('Qt Console', self)
console.setGeometry(10, 10, 100, 35)
self.connect(console, Qt.SIGNAL('clicked()'), self.new_qt_console)
namespace = Qt.QPushButton('Namespace', self)
namespace.setGeometry(120, 10, 100, 35)
self.connect(namespace, Qt.SIGNAL('clicked()'), self.print_namespace)
count = Qt.QPushButton('Count++', self)
count.setGeometry(230, 10, 80, 35)
self.connect(count, Qt.SIGNAL('clicked()'), self.count)
# Quit and cleanup
quit = Qt.QPushButton('Quit', self)
quit.setGeometry(320, 10, 60, 35)
self.connect(quit, Qt.SIGNAL('clicked()'), Qt.qApp, Qt.SLOT('quit()'))
self.app.connect(self.app, Qt.SIGNAL("lastWindowClosed()"),
self.app, Qt.SLOT("quit()"))
self.app.aboutToQuit.connect(self.cleanup_consoles)
#-----------------------------------------------------------------------------
# Main script
#-----------------------------------------------------------------------------
if __name__ == "__main__":
app = Qt.QApplication([])
# Create our window
win = SimpleWindow(app)
win.show()
# Very important, IPython-specific step: this gets GUI event loop
# integration going, and it replaces calling app.exec_()
win.yapkernel.start()

View File

@@ -0,0 +1,119 @@
#!/usr/bin/env python
"""Example integrating an IPython kernel into a GUI App.
This trivial GUI application internally starts an IPython kernel, to which Qt
consoles can be connected either by the user at the command line or started
from the GUI itself, via a button. The GUI can also manipulate one variable in
the kernel's namespace, and print the namespace to the console.
Play with it by running the script and then opening one or more consoles, and
pushing the 'Counter++' and 'Namespace' buttons.
Upon exit, it should automatically close all consoles opened from the GUI.
Consoles attached separately from a terminal will not be terminated, though
they will notice that their kernel died.
Ref: Modified from wxPython source code wxPython/samples/simple/simple.py
"""
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
import sys
import wx
from internal_yapkernel import InternalYAPKernel
#-----------------------------------------------------------------------------
# Functions and classes
#-----------------------------------------------------------------------------
class MyFrame(wx.Frame, InternalYAPKernel):
"""
This is MyFrame. It just shows a few controls on a wxPanel,
and has a simple menu.
"""
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title,
pos=(150, 150), size=(350, 285))
# Create the menubar
menuBar = wx.MenuBar()
# and a menu
menu = wx.Menu()
# add an item to the menu, using \tKeyName automatically
# creates an accelerator, the third param is some help text
# that will show up in the statusbar
menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample")
# bind the menu event to an event handler
self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT)
# and put the menu on the menubar
menuBar.Append(menu, "&File")
self.SetMenuBar(menuBar)
self.CreateStatusBar()
# Now create the Panel to put the other controls on.
panel = wx.Panel(self)
# and a few controls
text = wx.StaticText(panel, -1, "Hello World!")
text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
text.SetSize(text.GetBestSize())
qtconsole_btn = wx.Button(panel, -1, "Qt Console")
ns_btn = wx.Button(panel, -1, "Namespace")
count_btn = wx.Button(panel, -1, "Count++")
close_btn = wx.Button(panel, -1, "Quit")
# bind the button events to handlers
self.Bind(wx.EVT_BUTTON, self.new_qt_console, qtconsole_btn)
self.Bind(wx.EVT_BUTTON, self.print_namespace, ns_btn)
self.Bind(wx.EVT_BUTTON, self.count, count_btn)
self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, close_btn)
# Use a sizer to layout the controls, stacked vertically and with
# a 10 pixel border around each
sizer = wx.BoxSizer(wx.VERTICAL)
for ctrl in [text, qtconsole_btn, ns_btn, count_btn, close_btn]:
sizer.Add(ctrl, 0, wx.ALL, 10)
panel.SetSizer(sizer)
panel.Layout()
# Start the IPython kernel with gui support
self.init_yapkernel('wx')
def OnTimeToClose(self, evt):
"""Event handler for the button click."""
print("See ya later!")
sys.stdout.flush()
self.cleanup_consoles(evt)
self.Close()
# Not sure why, but our IPython kernel seems to prevent normal WX
# shutdown, so an explicit exit() call is needed.
sys.exit()
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, "Simple wxPython App")
self.SetTopWindow(frame)
frame.Show(True)
self.yapkernel = frame.yapkernel
return True
#-----------------------------------------------------------------------------
# Main script
#-----------------------------------------------------------------------------
if __name__ == '__main__':
app = MyApp(redirect=False, clearSigInt=False)
# Very important, IPython-specific step: this gets GUI event loop
# integration going, and it replaces calling app.MainLoop()
app.yapkernel.start()