##// END OF EJS Templates
Fix `frontend` deprecation warnings in several examples
Martin Spacek -
Show More
@@ -1,58 +1,58 b''
1 1 """ A simple example of using the Qt console with an in-process kernel.
2 2
3 3 We shall see how to create the frontend widget, create an in-process kernel,
4 4 push Python objects into the kernel's namespace, and execute code in the
5 5 kernel, both directly and via the frontend widget.
6 6 """
7 7
8 8 from IPython.kernel.inprocess.ipkernel import InProcessKernel
9 from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget
10 from IPython.frontend.qt.inprocess_kernelmanager import QtInProcessKernelManager
9 from IPython.qt.console.rich_ipython_widget import RichIPythonWidget
10 from IPython.qt.inprocess_kernelmanager import QtInProcessKernelManager
11 11 from IPython.lib import guisupport
12 12
13 13
14 14 def main():
15 15 app = guisupport.get_app_qt4()
16 16
17 17 # Create a kernel.
18 18 #
19 19 # Setting the GUI is not necessary for the normal operation of the kernel,
20 20 # but it is used for IPython GUI's integration, particularly in pylab. By
21 21 # default, the inline backend is used, which is safe under all toolkits.
22 22 #
23 23 # WARNING: Under no circumstances should another GUI toolkit, like wx, be
24 24 # used when running a Qt application. This will lead to unexpected behavior,
25 25 # including segfaults.
26 26 kernel = InProcessKernel(gui='qt4')
27 27
28 28 # Populate the kernel's namespace.
29 29 kernel.shell.push({'x': 0, 'y': 1, 'z': 2})
30 30
31 31 # Create a kernel manager for the frontend and register it with the kernel.
32 32 km = QtInProcessKernelManager(kernel=kernel)
33 33 km.start_channels()
34 34 kernel.frontends.append(km)
35 35
36 36 # Create the Qt console frontend.
37 37 control = RichIPythonWidget()
38 38 control.exit_requested.connect(app.quit)
39 39 control.kernel_manager = km
40 40 control.show()
41 41
42 42 # Execute some code directly. Note where the output appears.
43 43 kernel.shell.run_cell('print "x=%r, y=%r, z=%r" % (x,y,z)')
44 44
45 45 # Execute some code through the frontend (once the event loop is
46 46 # running). Again, note where the output appears.
47 47 do_later(control.execute, '%who')
48 48
49 49 guisupport.start_event_loop_qt4(app)
50 50
51 51
52 52 def do_later(func, *args, **kwds):
53 53 from IPython.external.qt import QtCore
54 54 QtCore.QTimer.singleShot(0, lambda: func(*args, **kwds))
55 55
56 56
57 57 if __name__ == '__main__':
58 58 main()
@@ -1,47 +1,47 b''
1 1 """Quick code snippets for embedding IPython into other programs.
2 2
3 3 See example-embed.py for full details, this file has the bare minimum code for
4 4 cut and paste use once you understand how to use the system."""
5 5
6 6 #---------------------------------------------------------------------------
7 7 # This code loads IPython but modifies a few things if it detects it's running
8 8 # embedded in another IPython session (helps avoid confusion)
9 9
10 10 try:
11 11 get_ipython
12 12 except NameError:
13 13 banner=exit_msg=''
14 14 else:
15 15 banner = '*** Nested interpreter ***'
16 16 exit_msg = '*** Back in main IPython ***'
17 17
18 18 # First import the embed function
19 from IPython.frontend.terminal.embed import InteractiveShellEmbed
19 from IPython.terminal.embed import InteractiveShellEmbed
20 20 # Now create the IPython shell instance. Put ipshell() anywhere in your code
21 21 # where you want it to open.
22 22 ipshell = InteractiveShellEmbed(banner1=banner, exit_msg=exit_msg)
23 23
24 24 #---------------------------------------------------------------------------
25 25 # This code will load an embeddable IPython shell always with no changes for
26 26 # nested embededings.
27 27
28 28 from IPython import embed
29 29 # Now embed() will open IPython anywhere in the code.
30 30
31 31 #---------------------------------------------------------------------------
32 32 # This code loads an embeddable shell only if NOT running inside
33 33 # IPython. Inside IPython, the embeddable shell variable ipshell is just a
34 34 # dummy function.
35 35
36 36 try:
37 37 get_ipython
38 38 except NameError:
39 from IPython.frontend.terminal.embed import InteractiveShellEmbed
39 from IPython.terminal.embed import InteractiveShellEmbed
40 40 ipshell = InteractiveShellEmbed()
41 41 # Now ipshell() will open IPython anywhere in the code
42 42 else:
43 43 # Define a dummy ipshell() so the same code doesn't crash inside an
44 44 # interactive IPython
45 45 def ipshell(): pass
46 46
47 47 #******************* End of file <example-embed-short.py> ********************
@@ -1,138 +1,138 b''
1 1 #!/usr/bin/env python
2 2
3 3 """An example of how to embed an IPython shell into a running program.
4 4
5 5 Please see the documentation in the IPython.Shell module for more details.
6 6
7 7 The accompanying file example-embed-short.py has quick code fragments for
8 8 embedding which you can cut and paste in your code once you understand how
9 9 things work.
10 10
11 11 The code in this file is deliberately extra-verbose, meant for learning."""
12 12 from __future__ import print_function
13 13
14 14 # The basics to get you going:
15 15
16 16 # IPython sets the __IPYTHON__ variable so you can know if you have nested
17 17 # copies running.
18 18
19 19 # Try running this code both at the command line and from inside IPython (with
20 20 # %run example-embed.py)
21 21 from IPython.config.loader import Config
22 22 try:
23 23 get_ipython
24 24 except NameError:
25 25 nested = 0
26 26 cfg = Config()
27 27 prompt_config = cfg.PromptManager
28 28 prompt_config.in_template = 'In <\\#>: '
29 29 prompt_config.in2_template = ' .\\D.: '
30 30 prompt_config.out_template = 'Out<\\#>: '
31 31 else:
32 32 print("Running nested copies of IPython.")
33 33 print("The prompts for the nested copy have been modified")
34 34 cfg = Config()
35 35 nested = 1
36 36
37 37 # First import the embeddable shell class
38 from IPython.frontend.terminal.embed import InteractiveShellEmbed
38 from IPython.terminal.embed import InteractiveShellEmbed
39 39
40 40 # Now create an instance of the embeddable shell. The first argument is a
41 41 # string with options exactly as you would type them if you were starting
42 42 # IPython at the system command line. Any parameters you want to define for
43 43 # configuration can thus be specified here.
44 44 ipshell = InteractiveShellEmbed(config=cfg,
45 45 banner1 = 'Dropping into IPython',
46 46 exit_msg = 'Leaving Interpreter, back to program.')
47 47
48 48 # Make a second instance, you can have as many as you want.
49 49 cfg2 = cfg.copy()
50 50 prompt_config = cfg2.PromptManager
51 51 prompt_config.in_template = 'In2<\\#>: '
52 52 if not nested:
53 53 prompt_config.in_template = 'In2<\\#>: '
54 54 prompt_config.in2_template = ' .\\D.: '
55 55 prompt_config.out_template = 'Out<\\#>: '
56 56 ipshell2 = InteractiveShellEmbed(config=cfg,
57 57 banner1 = 'Second IPython instance.')
58 58
59 59 print('\nHello. This is printed from the main controller program.\n')
60 60
61 61 # You can then call ipshell() anywhere you need it (with an optional
62 62 # message):
63 63 ipshell('***Called from top level. '
64 64 'Hit Ctrl-D to exit interpreter and continue program.\n'
65 65 'Note that if you use %kill_embedded, you can fully deactivate\n'
66 66 'This embedded instance so it will never turn on again')
67 67
68 68 print('\nBack in caller program, moving along...\n')
69 69
70 70 #---------------------------------------------------------------------------
71 71 # More details:
72 72
73 73 # InteractiveShellEmbed instances don't print the standard system banner and
74 74 # messages. The IPython banner (which actually may contain initialization
75 75 # messages) is available as get_ipython().banner in case you want it.
76 76
77 77 # InteractiveShellEmbed instances print the following information everytime they
78 78 # start:
79 79
80 80 # - A global startup banner.
81 81
82 82 # - A call-specific header string, which you can use to indicate where in the
83 83 # execution flow the shell is starting.
84 84
85 85 # They also print an exit message every time they exit.
86 86
87 87 # Both the startup banner and the exit message default to None, and can be set
88 88 # either at the instance constructor or at any other time with the
89 89 # by setting the banner and exit_msg attributes.
90 90
91 91 # The shell instance can be also put in 'dummy' mode globally or on a per-call
92 92 # basis. This gives you fine control for debugging without having to change
93 93 # code all over the place.
94 94
95 95 # The code below illustrates all this.
96 96
97 97
98 98 # This is how the global banner and exit_msg can be reset at any point
99 99 ipshell.banner = 'Entering interpreter - New Banner'
100 100 ipshell.exit_msg = 'Leaving interpreter - New exit_msg'
101 101
102 102 def foo(m):
103 103 s = 'spam'
104 104 ipshell('***In foo(). Try %whos, or print s or m:')
105 105 print('foo says m = ',m)
106 106
107 107 def bar(n):
108 108 s = 'eggs'
109 109 ipshell('***In bar(). Try %whos, or print s or n:')
110 110 print('bar says n = ',n)
111 111
112 112 # Some calls to the above functions which will trigger IPython:
113 113 print('Main program calling foo("eggs")\n')
114 114 foo('eggs')
115 115
116 116 # The shell can be put in 'dummy' mode where calls to it silently return. This
117 117 # allows you, for example, to globally turn off debugging for a program with a
118 118 # single call.
119 119 ipshell.dummy_mode = True
120 120 print('\nTrying to call IPython which is now "dummy":')
121 121 ipshell()
122 122 print('Nothing happened...')
123 123 # The global 'dummy' mode can still be overridden for a single call
124 124 print('\nOverriding dummy mode manually:')
125 125 ipshell(dummy=False)
126 126
127 127 # Reactivate the IPython shell
128 128 ipshell.dummy_mode = False
129 129
130 130 print('You can even have multiple embedded instances:')
131 131 ipshell2()
132 132
133 133 print('\nMain program calling bar("spam")\n')
134 134 bar('spam')
135 135
136 136 print('Main program finished. Bye!')
137 137
138 138 #********************** End of file <example-embed.py> ***********************
@@ -1,45 +1,45 b''
1 1 import os
2 2
3 from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget
4 from IPython.frontend.qt.inprocess import QtInProcessKernelManager
3 from IPython.qt.console.rich_ipython_widget import RichIPythonWidget
4 from IPython.qt.inprocess import QtInProcessKernelManager
5 5 from IPython.lib import guisupport
6 6
7 7
8 8 def print_process_id():
9 9 print 'Process ID is:', os.getpid()
10 10
11 11
12 12 def main():
13 13 # Print the ID of the main process
14 14 print_process_id()
15 15
16 16 app = guisupport.get_app_qt4()
17 17
18 18 # Create an in-process kernel
19 19 # >>> print_process_id()
20 20 # will print the same process ID as the main process
21 21 kernel_manager = QtInProcessKernelManager()
22 22 kernel_manager.start_kernel()
23 23 kernel = kernel_manager.kernel
24 24 kernel.gui = 'qt4'
25 25 kernel.shell.push({'foo': 43, 'print_process_id': print_process_id})
26 26
27 27 kernel_client = kernel_manager.client()
28 28 kernel_client.start_channels()
29 29
30 30 def stop():
31 31 kernel_client.stop_channels()
32 32 kernel_manager.shutdown_kernel()
33 33 app.exit()
34 34
35 35 control = RichIPythonWidget()
36 36 control.kernel_manager = kernel_manager
37 37 control.kernel_client = kernel_client
38 38 control.exit_requested.connect(stop)
39 39 control.show()
40 40
41 41 guisupport.start_event_loop_qt4(app)
42 42
43 43
44 44 if __name__ == '__main__':
45 45 main()
@@ -1,30 +1,30 b''
1 1 import os
2 2
3 3 from IPython.kernel.inprocess import InProcessKernelManager
4 from IPython.frontend.terminal.console.interactiveshell import ZMQTerminalInteractiveShell
4 from IPython.terminal.console.interactiveshell import ZMQTerminalInteractiveShell
5 5
6 6
7 7 def print_process_id():
8 8 print 'Process ID is:', os.getpid()
9 9
10 10
11 11 def main():
12 12 print_process_id()
13 13
14 14 # Create an in-process kernel
15 15 # >>> print_process_id()
16 16 # will print the same process ID as the main process
17 17 kernel_manager = InProcessKernelManager()
18 18 kernel_manager.start_kernel()
19 19 kernel = kernel_manager.kernel
20 20 kernel.gui = 'qt4'
21 21 kernel.shell.push({'foo': 43, 'print_process_id': print_process_id})
22 22 client = kernel_manager.client()
23 23 client.start_channels()
24 24
25 25 shell = ZMQTerminalInteractiveShell(manager=kernel_manager, client=client)
26 26 shell.mainloop()
27 27
28 28
29 29 if __name__ == '__main__':
30 30 main()
@@ -1,7 +1,7 b''
1 1 """This tests passing local_ns and global_ns (for backwards compatibility only)
2 2 at activation of an embedded shell."""
3 from IPython.frontend.terminal.embed import InteractiveShellEmbed
3 from IPython.terminal.embed import InteractiveShellEmbed
4 4
5 5 user_ns = dict(cookie='monster')
6 6 ISE = InteractiveShellEmbed(banner1='check cookie in locals, and globals empty')
7 7 ISE(local_ns=user_ns, global_ns={})
General Comments 0
You need to be logged in to leave comments. Login now