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 | 8 | from IPython.inprocess.ipkernel import InProcessKernel |
|
2 | 9 | from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget |
|
3 | 10 | from IPython.frontend.qt.inprocess_kernelmanager import QtInProcessKernelManager |
|
4 | 11 | from IPython.lib import guisupport |
|
5 | 12 | |
|
6 | 13 | |
|
7 | 14 | def main(): |
|
8 | 15 | app = guisupport.get_app_qt4() |
|
9 | 16 | |
|
10 | 17 | # Create a kernel and populate the namespace. |
|
11 | 18 | kernel = InProcessKernel() |
|
12 | 19 | kernel.shell.push({'x': 0, 'y': 1, 'z': 2}) |
|
13 | 20 | |
|
14 | 21 | # Create a kernel manager for the frontend and register it with the kernel. |
|
15 | 22 | km = QtInProcessKernelManager(kernel=kernel) |
|
16 | 23 | km.start_channels() |
|
17 | 24 | kernel.frontends.append(km) |
|
18 | 25 | |
|
19 | 26 | # Create the Qt console frontend. |
|
20 | 27 | control = RichIPythonWidget() |
|
21 | 28 | control.exit_requested.connect(app.quit) |
|
22 | 29 | control.kernel_manager = km |
|
23 | 30 | control.show() |
|
24 | 31 | |
|
25 | 32 | # Execute some code directly. Note where the output appears. |
|
26 | 33 | kernel.shell.run_cell('print "x=%r, y=%r, z=%r" % (x,y,z)') |
|
27 | 34 | |
|
28 | 35 | # Execute some code through the frontend (once the event loop is |
|
29 | 36 | # running). Again, note where the output appears. |
|
30 | 37 | do_later(control.execute, '%who') |
|
31 | 38 | |
|
32 | 39 | guisupport.start_event_loop_qt4(app) |
|
33 | 40 | |
|
34 | 41 | |
|
35 | 42 | def do_later(func, *args, **kwds): |
|
36 | 43 | from IPython.external.qt import QtCore |
|
37 | 44 | QtCore.QTimer.singleShot(0, lambda: func(*args, **kwds)) |
|
38 | 45 | |
|
39 | 46 | |
|
40 | 47 | if __name__ == '__main__': |
|
41 | 48 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now