Debian Codegraph @

docs/source/config/eventloops.rst

raw
1 ================================
2 Integrating with GUI event loops
3 ================================
4
5 When the user types ``%gui qt``, IPython integrates itself with the Qt event
6 loop, so you can use both a GUI and an interactive prompt together. IPython
7 supports a number of common GUI toolkits, and it is possible to integrate
8 other event loops without modifying IPython itself.
9
10 Supported event loops include ``qt5``, ``qt6``, ``gtk2``, ``gtk3``, ``gtk4``,
11 ``wx``, ``osx`` and ``tk``. Make sure the event loop you specify matches the
12 GUI toolkit used by your own code.
13
14 .. note::
15
16 ``gtk2`` is deprecated and GTK3 or GTK4 should be used instead for new projects.
17
18 To make IPython GUI event loop integration occur automatically at every
19 startup, set the ``c.InteractiveShellApp.gui`` configuration key in your
20 IPython profile (see :ref:`setting_config`).
21
22 If the event loop you use is supported by IPython, turning on event loop
23 integration follows the steps just described whether you use Terminal IPython
24 or an IPython kernel.
25
26 However, the way Terminal IPython handles event loops is very different from
27 the way IPython kernel does, so if you need to integrate with a new kind of
28 event loop, different steps are needed to integrate with each.
29
30 Integrating with a new event loop in the terminal
31 -------------------------------------------------
32
33 .. versionchanged:: 5.0
34
35 There is a new API for event loop integration using prompt_toolkit.
36
37 In the terminal, IPython uses prompt_toolkit to prompt the user for input.
38 prompt_toolkit provides hooks to integrate with an external event loop.
39
40 To integrate an event loop, define a function which runs the GUI event loop
41 until there is input waiting for prompt_toolkit to process. There are two ways
42 to detect this condition::
43
44 # Polling for input.
45 def inputhook(context):
46 while not context.input_is_ready():
47 # Replace this with the appropriate call for the event loop:
48 iterate_loop_once()
49
50 # Using a file descriptor to notify the event loop to stop.
51 def inputhook2(context):
52 fd = context.fileno()
53 # Replace the functions below with those for the event loop.
54 add_file_reader(fd, callback=stop_the_loop)
55 run_the_loop()
56
57 Once you have defined this function, register it with IPython:
58
59 .. currentmodule:: IPython.terminal.pt_inputhooks
60
61 .. function:: register(name, inputhook)
62
63 Register the function *inputhook* as the event loop integration for the
64 GUI *name*. If ``name='foo'``, then the user can enable this integration
65 by running ``%gui foo``.
66
67
68 Integrating with a new event loop in the kernel
69 -----------------------------------------------
70
71 The kernel runs its own event loop, so it's simpler to integrate with others.
72 IPython allows the other event loop to take control, but it must call
73 :meth:`IPython.kernel.zmq.kernelbase.Kernel.do_one_iteration` periodically.
74
75 To integrate with this, write a function that takes a single argument,
76 the IPython kernel instance, arranges for your event loop to call
77 ``kernel.do_one_iteration()`` at least every ``kernel._poll_interval`` seconds,
78 and starts the event loop.
79
80 Decorate this function with :func:`IPython.kernel.zmq.eventloops.register_integration`,
81 passing in the names you wish to register it for. Here is a slightly simplified
82 version of the Tkinter integration already included in IPython::
83
84 @register_integration('tk')
85 def loop_tk(kernel):
86 """Start a kernel with the Tk event loop."""
87 from tkinter import Tk
88
89 # Tk uses milliseconds
90 poll_interval = int(1000*kernel._poll_interval)
91 # For Tkinter, we create a Tk object and call its withdraw method.
92 class Timer(object):
93 def __init__(self, func):
94 self.app = Tk()
95 self.app.withdraw()
96 self.func = func
97
98 def on_timer(self):
99 self.func()
100 self.app.after(poll_interval, self.on_timer)
101
102 def start(self):
103 self.on_timer() # Call it once to get things going.
104 self.app.mainloop()
105
106 kernel.timer = Timer(kernel.do_one_iteration)
107 kernel.timer.start()
108
109 Some event loops can go one better, and integrate checking for messages on the
110 kernel's ZMQ sockets, making the kernel more responsive than plain polling. How
111 to do this is outside the scope of this document; if you are interested, look at
112 the integration with Qt in :mod:`IPython.kernel.zmq.eventloops`.