##// END OF EJS Templates
DOC: Add module-level docstring to in-process kernel example.
epatters -
Show More
@@ -1,41 +1,48 b''
1 """ A simple example of using the Qt console with an in-process kernel.
2
3 We shall see how to create the frontend widget, create an in-process kernel,
4 push Python objects into the kernel's namespace, and execute code in the
5 kernel, both directly and via the frontend widget.
6 """
7
1 from IPython.inprocess.ipkernel import InProcessKernel
8 from IPython.inprocess.ipkernel import InProcessKernel
2 from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget
9 from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget
3 from IPython.frontend.qt.inprocess_kernelmanager import QtInProcessKernelManager
10 from IPython.frontend.qt.inprocess_kernelmanager import QtInProcessKernelManager
4 from IPython.lib import guisupport
11 from IPython.lib import guisupport
5
12
6
13
7 def main():
14 def main():
8 app = guisupport.get_app_qt4()
15 app = guisupport.get_app_qt4()
9
16
10 # Create a kernel and populate the namespace.
17 # Create a kernel and populate the namespace.
11 kernel = InProcessKernel()
18 kernel = InProcessKernel()
12 kernel.shell.push({'x': 0, 'y': 1, 'z': 2})
19 kernel.shell.push({'x': 0, 'y': 1, 'z': 2})
13
20
14 # Create a kernel manager for the frontend and register it with the kernel.
21 # Create a kernel manager for the frontend and register it with the kernel.
15 km = QtInProcessKernelManager(kernel=kernel)
22 km = QtInProcessKernelManager(kernel=kernel)
16 km.start_channels()
23 km.start_channels()
17 kernel.frontends.append(km)
24 kernel.frontends.append(km)
18
25
19 # Create the Qt console frontend.
26 # Create the Qt console frontend.
20 control = RichIPythonWidget()
27 control = RichIPythonWidget()
21 control.exit_requested.connect(app.quit)
28 control.exit_requested.connect(app.quit)
22 control.kernel_manager = km
29 control.kernel_manager = km
23 control.show()
30 control.show()
24
31
25 # Execute some code directly. Note where the output appears.
32 # Execute some code directly. Note where the output appears.
26 kernel.shell.run_cell('print "x=%r, y=%r, z=%r" % (x,y,z)')
33 kernel.shell.run_cell('print "x=%r, y=%r, z=%r" % (x,y,z)')
27
34
28 # Execute some code through the frontend (once the event loop is
35 # Execute some code through the frontend (once the event loop is
29 # running). Again, note where the output appears.
36 # running). Again, note where the output appears.
30 do_later(control.execute, '%who')
37 do_later(control.execute, '%who')
31
38
32 guisupport.start_event_loop_qt4(app)
39 guisupport.start_event_loop_qt4(app)
33
40
34
41
35 def do_later(func, *args, **kwds):
42 def do_later(func, *args, **kwds):
36 from IPython.external.qt import QtCore
43 from IPython.external.qt import QtCore
37 QtCore.QTimer.singleShot(0, lambda: func(*args, **kwds))
44 QtCore.QTimer.singleShot(0, lambda: func(*args, **kwds))
38
45
39
46
40 if __name__ == '__main__':
47 if __name__ == '__main__':
41 main()
48 main()
General Comments 0
You need to be logged in to leave comments. Login now