Debian Codegraph @

examples/embedding/ipkernel_wxapp.py

raw
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 Ref: Modified from wxPython source code wxPython/samples/simple/simple.py
18 """
19
20 # -----------------------------------------------------------------------------
21 # Imports
22 # -----------------------------------------------------------------------------
23 import sys
24
25 import wx
26 from internal_ipkernel import InternalIPKernel
27
28 # -----------------------------------------------------------------------------
29 # Functions and classes
30 # -----------------------------------------------------------------------------
31
32
33 class MyFrame(wx.Frame, InternalIPKernel):
34 """
35 This is MyFrame. It just shows a few controls on a wxPanel,
36 and has a simple menu.
37 """
38
39 def __init__(self, parent, title):
40 """Initialize the frame."""
41 wx.Frame.__init__(self, parent, -1, title, pos=(150, 150), size=(350, 285))
42
43 # Create the menubar
44 menuBar = wx.MenuBar()
45
46 # and a menu
47 menu = wx.Menu()
48
49 # add an item to the menu, using \tKeyName automatically
50 # creates an accelerator, the third param is some help text
51 # that will show up in the statusbar
52 menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample")
53
54 # bind the menu event to an event handler
55 self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT)
56
57 # and put the menu on the menubar
58 menuBar.Append(menu, "&File")
59 self.SetMenuBar(menuBar)
60
61 self.CreateStatusBar()
62
63 # Now create the Panel to put the other controls on.
64 panel = wx.Panel(self)
65
66 # and a few controls
67 text = wx.StaticText(panel, -1, "Hello World!")
68 text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
69 text.SetSize(text.GetBestSize())
70 qtconsole_btn = wx.Button(panel, -1, "Qt Console")
71 ns_btn = wx.Button(panel, -1, "Namespace")
72 count_btn = wx.Button(panel, -1, "Count++")
73 close_btn = wx.Button(panel, -1, "Quit")
74
75 # bind the button events to handlers
76 self.Bind(wx.EVT_BUTTON, self.new_qt_console, qtconsole_btn)
77 self.Bind(wx.EVT_BUTTON, self.print_namespace, ns_btn)
78 self.Bind(wx.EVT_BUTTON, self.count, count_btn)
79 self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, close_btn)
80
81 # Use a sizer to layout the controls, stacked vertically and with
82 # a 10 pixel border around each
83 sizer = wx.BoxSizer(wx.VERTICAL)
84 for ctrl in [text, qtconsole_btn, ns_btn, count_btn, close_btn]:
85 sizer.Add(ctrl, 0, wx.ALL, 10)
86 panel.SetSizer(sizer)
87 panel.Layout()
88
89 # Start the IPython kernel with gui support
90 self.init_ipkernel("wx")
91
92 def OnTimeToClose(self, evt):
93 """Event handler for the button click."""
94 print("See ya later!")
95 sys.stdout.flush()
96 self.cleanup_consoles(evt)
97 self.Close()
98 # Not sure why, but our IPython kernel seems to prevent normal WX
99 # shutdown, so an explicit exit() call is needed.
100 sys.exit()
101
102
103 class MyApp(wx.App):
104 """A custom wx app."""
105
106 def OnInit(self):
107 """Initialize app."""
108 frame = MyFrame(None, "Simple wxPython App")
109 self.SetTopWindow(frame)
110 frame.Show(True)
111 self.ipkernel = frame.ipkernel
112 return True
113
114
115 # -----------------------------------------------------------------------------
116 # Main script
117 # -----------------------------------------------------------------------------
118
119 if __name__ == "__main__":
120 app = MyApp(redirect=False, clearSigInt=False)
121
122 # Very important, IPython-specific step: this gets GUI event loop
123 # integration going, and it replaces calling app.MainLoop()
124 app.ipkernel.start()