##// END OF EJS Templates
Make print syntax in GUI integration examples Python 3 compatible.
Thomas Kluyver -
Show More
@@ -1,40 +1,41 b''
1 1 """A simple interactive demo to illustrate the use of IPython's Demo class.
2 2
3 3 Any python script can be run as a demo, but that does little more than showing
4 4 it on-screen, syntax-highlighted in one shot. If you add a little simple
5 5 markup, you can stop at specified intervals and return to the ipython prompt,
6 6 resuming execution later.
7 7 """
8 from __future__ import print_function
8 9
9 print 'Hello, welcome to an interactive IPython demo.'
10 print 'Executing this block should require confirmation before proceeding,'
11 print 'unless auto_all has been set to true in the demo object'
10 print('Hello, welcome to an interactive IPython demo.')
11 print('Executing this block should require confirmation before proceeding,')
12 print('unless auto_all has been set to true in the demo object')
12 13
13 14 # The mark below defines a block boundary, which is a point where IPython will
14 15 # stop execution and return to the interactive prompt.
15 16 # Note that in actual interactive execution,
16 17 # <demo> --- stop ---
17 18
18 19 x = 1
19 20 y = 2
20 21
21 22 # <demo> --- stop ---
22 23
23 24 # the mark below makes this block as silent
24 25 # <demo> silent
25 26
26 print 'This is a silent block, which gets executed but not printed.'
27 print('This is a silent block, which gets executed but not printed.')
27 28
28 29 # <demo> --- stop ---
29 30 # <demo> auto
30 print 'This is an automatic block.'
31 print 'It is executed without asking for confirmation, but printed.'
31 print('This is an automatic block.')
32 print('It is executed without asking for confirmation, but printed.')
32 33 z = x+y
33 34
34 print 'z=',x
35 print('z=',x)
35 36
36 37 # <demo> --- stop ---
37 38 # This is just another normal block.
38 print 'z is now:', z
39 print('z is now:', z)
39 40
40 print 'bye!'
41 print('bye!')
@@ -1,39 +1,39 b''
1 1 #!/usr/bin/env python
2 2 """Simple GTK example to manually test event loop integration.
3 3
4 4 This is meant to run tests manually in ipython as:
5 5
6 6 In [5]: %gui gtk
7 7
8 8 In [6]: %run gui-gtk.py
9 9 """
10 10
11 11 import pygtk
12 12 pygtk.require('2.0')
13 13 import gtk
14 14
15 15
16 16 def hello_world(wigdet, data=None):
17 print "Hello World"
17 print("Hello World")
18 18
19 19 def delete_event(widget, event, data=None):
20 20 return False
21 21
22 22 def destroy(widget, data=None):
23 23 gtk.main_quit()
24 24
25 25 window = gtk.Window(gtk.WINDOW_TOPLEVEL)
26 26 window.connect("delete_event", delete_event)
27 27 window.connect("destroy", destroy)
28 28 button = gtk.Button("Hello World")
29 29 button.connect("clicked", hello_world, None)
30 30
31 31 window.add(button)
32 32 button.show()
33 33 window.show()
34 34
35 35 try:
36 36 from IPython.lib.inputhook import enable_gtk
37 37 enable_gtk()
38 38 except ImportError:
39 39 gtk.main()
@@ -1,32 +1,32 b''
1 1 #!/usr/bin/env python
2 2 """Simple Tk example to manually test event loop integration.
3 3
4 4 This is meant to run tests manually in ipython as:
5 5
6 6 In [5]: %gui tk
7 7
8 8 In [6]: %run gui-tk.py
9 9 """
10 10
11 11 from Tkinter import *
12 12
13 13 class MyApp:
14 14
15 15 def __init__(self, root):
16 16 frame = Frame(root)
17 17 frame.pack()
18 18
19 19 self.button = Button(frame, text="Hello", command=self.hello_world)
20 20 self.button.pack(side=LEFT)
21 21
22 22 def hello_world(self):
23 print "Hello World!"
23 print("Hello World!")
24 24
25 25 root = Tk()
26 26
27 27 app = MyApp(root)
28 28
29 29 try:
30 30 from IPython.lib.inputhook import enable_tk; enable_tk(root)
31 31 except ImportError:
32 32 root.mainloop()
@@ -1,106 +1,106 b''
1 1 #!/usr/bin/env python
2 2 """
3 3 A Simple wx example to test IPython's event loop integration.
4 4
5 5 To run this do:
6 6
7 7 In [5]: %gui wx # or start IPython with '--gui wx' or '--pylab wx'
8 8
9 9 In [6]: %run gui-wx.py
10 10
11 11 Ref: Modified from wxPython source code wxPython/samples/simple/simple.py
12 12 """
13 13
14 14 import wx
15 15
16 16
17 17 class MyFrame(wx.Frame):
18 18 """
19 19 This is MyFrame. It just shows a few controls on a wxPanel,
20 20 and has a simple menu.
21 21 """
22 22 def __init__(self, parent, title):
23 23 wx.Frame.__init__(self, parent, -1, title,
24 24 pos=(150, 150), size=(350, 200))
25 25
26 26 # Create the menubar
27 27 menuBar = wx.MenuBar()
28 28
29 29 # and a menu
30 30 menu = wx.Menu()
31 31
32 32 # add an item to the menu, using \tKeyName automatically
33 33 # creates an accelerator, the third param is some help text
34 34 # that will show up in the statusbar
35 35 menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample")
36 36
37 37 # bind the menu event to an event handler
38 38 self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT)
39 39
40 40 # and put the menu on the menubar
41 41 menuBar.Append(menu, "&File")
42 42 self.SetMenuBar(menuBar)
43 43
44 44 self.CreateStatusBar()
45 45
46 46 # Now create the Panel to put the other controls on.
47 47 panel = wx.Panel(self)
48 48
49 49 # and a few controls
50 50 text = wx.StaticText(panel, -1, "Hello World!")
51 51 text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
52 52 text.SetSize(text.GetBestSize())
53 53 btn = wx.Button(panel, -1, "Close")
54 54 funbtn = wx.Button(panel, -1, "Just for fun...")
55 55
56 56 # bind the button events to handlers
57 57 self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, btn)
58 58 self.Bind(wx.EVT_BUTTON, self.OnFunButton, funbtn)
59 59
60 60 # Use a sizer to layout the controls, stacked vertically and with
61 61 # a 10 pixel border around each
62 62 sizer = wx.BoxSizer(wx.VERTICAL)
63 63 sizer.Add(text, 0, wx.ALL, 10)
64 64 sizer.Add(btn, 0, wx.ALL, 10)
65 65 sizer.Add(funbtn, 0, wx.ALL, 10)
66 66 panel.SetSizer(sizer)
67 67 panel.Layout()
68 68
69 69
70 70 def OnTimeToClose(self, evt):
71 71 """Event handler for the button click."""
72 print "See ya later!"
72 print("See ya later!")
73 73 self.Close()
74 74
75 75 def OnFunButton(self, evt):
76 76 """Event handler for the button click."""
77 print "Having fun yet?"
77 print("Having fun yet?")
78 78
79 79
80 80 class MyApp(wx.App):
81 81 def OnInit(self):
82 82 frame = MyFrame(None, "Simple wxPython App")
83 83 self.SetTopWindow(frame)
84 84
85 print "Print statements go to this stdout window by default."
85 print("Print statements go to this stdout window by default.")
86 86
87 87 frame.Show(True)
88 88 return True
89 89
90 90
91 91 if __name__ == '__main__':
92 92
93 93 app = wx.GetApp()
94 94 if app is None:
95 95 app = MyApp(redirect=False, clearSigInt=False)
96 96 else:
97 97 frame = MyFrame(None, "Simple wxPython App")
98 98 app.SetTopWindow(frame)
99 print "Print statements go to this stdout window by default."
99 print("Print statements go to this stdout window by default.")
100 100 frame.Show(True)
101 101
102 102 try:
103 103 from IPython.lib.inputhook import enable_wx
104 104 enable_wx(app)
105 105 except ImportError:
106 106 app.MainLoop()
@@ -1,59 +1,59 b''
1 1 #-----------------------------------------------------------------------------
2 2 # Imports
3 3 #-----------------------------------------------------------------------------
4 4
5 5 import subprocess
6 6 import sys
7 7
8 8 from IPython.lib.kernel import connect_qtconsole
9 9 from IPython.zmq.ipkernel import IPKernelApp
10 10
11 11 #-----------------------------------------------------------------------------
12 12 # Functions and classes
13 13 #-----------------------------------------------------------------------------
14 14 def pylab_kernel(gui):
15 15 """Launch and return an IPython kernel with pylab support for the desired gui
16 16 """
17 17 kernel = IPKernelApp.instance()
18 18 kernel.initialize(['python', '--pylab=%s' % gui,
19 19 #'--log-level=10'
20 20 ])
21 21 return kernel
22 22
23 23
24 24 class InternalIPKernel(object):
25 25
26 26 def init_ipkernel(self, backend):
27 27 # Start IPython kernel with GUI event loop and pylab support
28 28 self.ipkernel = pylab_kernel(backend)
29 29 # To create and track active qt consoles
30 30 self.consoles = []
31 31
32 32 # This application will also act on the shell user namespace
33 33 self.namespace = self.ipkernel.shell.user_ns
34 34 # Keys present at startup so we don't print the entire pylab/numpy
35 35 # namespace when the user clicks the 'namespace' button
36 36 self._init_keys = set(self.namespace.keys())
37 37
38 38 # Example: a variable that will be seen by the user in the shell, and
39 39 # that the GUI modifies (the 'Counter++' button increments it):
40 40 self.namespace['app_counter'] = 0
41 41 #self.namespace['ipkernel'] = self.ipkernel # dbg
42 42
43 43 def print_namespace(self, evt=None):
44 print "\n***Variables in User namespace***"
44 print("\n***Variables in User namespace***")
45 45 for k, v in self.namespace.iteritems():
46 46 if k not in self._init_keys and not k.startswith('_'):
47 print '%s -> %r' % (k, v)
47 print('%s -> %r' % (k, v))
48 48 sys.stdout.flush()
49 49
50 50 def new_qt_console(self, evt=None):
51 51 """start a new qtconsole connected to our kernel"""
52 52 return connect_qtconsole(self.ipkernel.connection_file, profile=self.ipkernel.profile)
53 53
54 54 def count(self, evt=None):
55 55 self.namespace['app_counter'] += 1
56 56
57 57 def cleanup_consoles(self, evt=None):
58 58 for c in self.consoles:
59 59 c.kill()
@@ -1,119 +1,119 b''
1 1 #!/usr/bin/env python
2 2 """Example integrating an IPython kernel into a GUI App.
3 3
4 4 This trivial GUI application internally starts an IPython kernel, to which Qt
5 5 consoles can be connected either by the user at the command line or started
6 6 from the GUI itself, via a button. The GUI can also manipulate one variable in
7 7 the kernel's namespace, and print the namespace to the console.
8 8
9 9 Play with it by running the script and then opening one or more consoles, and
10 10 pushing the 'Counter++' and 'Namespace' buttons.
11 11
12 12 Upon exit, it should automatically close all consoles opened from the GUI.
13 13
14 14 Consoles attached separately from a terminal will not be terminated, though
15 15 they will notice that their kernel died.
16 16
17 17 Ref: Modified from wxPython source code wxPython/samples/simple/simple.py
18 18 """
19 19 #-----------------------------------------------------------------------------
20 20 # Imports
21 21 #-----------------------------------------------------------------------------
22 22 import sys
23 23
24 24 import wx
25 25
26 26 from internal_ipkernel import InternalIPKernel
27 27
28 28 #-----------------------------------------------------------------------------
29 29 # Functions and classes
30 30 #-----------------------------------------------------------------------------
31 31
32 32 class MyFrame(wx.Frame, InternalIPKernel):
33 33 """
34 34 This is MyFrame. It just shows a few controls on a wxPanel,
35 35 and has a simple menu.
36 36 """
37 37
38 38 def __init__(self, parent, title):
39 39 wx.Frame.__init__(self, parent, -1, title,
40 40 pos=(150, 150), size=(350, 285))
41 41
42 42 # Create the menubar
43 43 menuBar = wx.MenuBar()
44 44
45 45 # and a menu
46 46 menu = wx.Menu()
47 47
48 48 # add an item to the menu, using \tKeyName automatically
49 49 # creates an accelerator, the third param is some help text
50 50 # that will show up in the statusbar
51 51 menu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Exit this simple sample")
52 52
53 53 # bind the menu event to an event handler
54 54 self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT)
55 55
56 56 # and put the menu on the menubar
57 57 menuBar.Append(menu, "&File")
58 58 self.SetMenuBar(menuBar)
59 59
60 60 self.CreateStatusBar()
61 61
62 62 # Now create the Panel to put the other controls on.
63 63 panel = wx.Panel(self)
64 64
65 65 # and a few controls
66 66 text = wx.StaticText(panel, -1, "Hello World!")
67 67 text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
68 68 text.SetSize(text.GetBestSize())
69 69 qtconsole_btn = wx.Button(panel, -1, "Qt Console")
70 70 ns_btn = wx.Button(panel, -1, "Namespace")
71 71 count_btn = wx.Button(panel, -1, "Count++")
72 72 close_btn = wx.Button(panel, -1, "Quit")
73 73
74 74 # bind the button events to handlers
75 75 self.Bind(wx.EVT_BUTTON, self.new_qt_console, qtconsole_btn)
76 76 self.Bind(wx.EVT_BUTTON, self.print_namespace, ns_btn)
77 77 self.Bind(wx.EVT_BUTTON, self.count, count_btn)
78 78 self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, close_btn)
79 79
80 80 # Use a sizer to layout the controls, stacked vertically and with
81 81 # a 10 pixel border around each
82 82 sizer = wx.BoxSizer(wx.VERTICAL)
83 83 for ctrl in [text, qtconsole_btn, ns_btn, count_btn, close_btn]:
84 84 sizer.Add(ctrl, 0, wx.ALL, 10)
85 85 panel.SetSizer(sizer)
86 86 panel.Layout()
87 87
88 88 # Start the IPython kernel with gui support
89 89 self.init_ipkernel('wx')
90 90
91 91 def OnTimeToClose(self, evt):
92 92 """Event handler for the button click."""
93 print "See ya later!"
93 print("See ya later!")
94 94 sys.stdout.flush()
95 95 self.cleanup_consoles(evt)
96 96 self.Close()
97 97 # Not sure why, but our IPython kernel seems to prevent normal WX
98 98 # shutdown, so an explicit exit() call is needed.
99 99 sys.exit()
100 100
101 101
102 102 class MyApp(wx.App):
103 103 def OnInit(self):
104 104 frame = MyFrame(None, "Simple wxPython App")
105 105 self.SetTopWindow(frame)
106 106 frame.Show(True)
107 107 self.ipkernel = frame.ipkernel
108 108 return True
109 109
110 110 #-----------------------------------------------------------------------------
111 111 # Main script
112 112 #-----------------------------------------------------------------------------
113 113
114 114 if __name__ == '__main__':
115 115 app = MyApp(redirect=False, clearSigInt=False)
116 116
117 117 # Very important, IPython-specific step: this gets GUI event loop
118 118 # integration going, and it replaces calling app.MainLoop()
119 119 app.ipkernel.start()
General Comments 0
You need to be logged in to leave comments. Login now