Debian Codegraph @

docs/source/config/callbacks.rst

raw
1 .. _events:
2 .. _callbacks:
3
4 ==============
5 IPython Events
6 ==============
7
8 Extension code can register callbacks functions which will be called on specific
9 events within the IPython code. You can see the current list of available
10 callbacks, and the parameters that will be passed with each, in the callback
11 prototype functions defined in :mod:`IPython.core.events`.
12
13 To register callbacks, use :meth:`IPython.core.events.EventManager.register`.
14 For example::
15
16 class VarWatcher(object):
17 def __init__(self, ip):
18 self.shell = ip
19 self.last_x = None
20
21 def pre_execute(self):
22 self.last_x = self.shell.user_ns.get('x', None)
23
24 def pre_run_cell(self, info):
25 print('info.raw_cell =', info.raw_cell)
26 print('info.store_history =', info.store_history)
27 print('info.silent =', info.silent)
28 print('info.shell_futures =', info.shell_futures)
29 print('info.cell_id =', info.cell_id)
30 print(dir(info))
31
32 def post_execute(self):
33 if self.shell.user_ns.get('x', None) != self.last_x:
34 print("x changed!")
35
36 def post_run_cell(self, result):
37 print('result.execution_count = ', result.execution_count)
38 print('result.error_before_exec = ', result.error_before_exec)
39 print('result.error_in_exec = ', result.error_in_exec)
40 print('result.info = ', result.info)
41 print('result.result = ', result.result)
42
43 def load_ipython_extension(ip):
44 vw = VarWatcher(ip)
45 ip.events.register('pre_execute', vw.pre_execute)
46 ip.events.register('pre_run_cell', vw.pre_run_cell)
47 ip.events.register('post_execute', vw.post_execute)
48 ip.events.register('post_run_cell', vw.post_run_cell)
49
50 .. versionadded:: 8.3
51
52 Since IPython 8.3 and ipykernel 6.12.1, the ``info`` objects in the callback
53 now have a the ``cell_id`` that will be set to the value sent by the
54 frontened, when those send it.
55
56
57
58 Events
59 ======
60
61 These are the events IPython will emit. Callbacks will be passed no arguments, unless otherwise specified.
62
63 shell_initialized
64 -----------------
65
66 .. code-block:: python
67
68 def shell_initialized(ipython):
69 ...
70
71 This event is triggered only once, at the end of setting up IPython.
72 Extensions registered to load by default as part of configuration can use this to execute code to finalize setup.
73 Callbacks will be passed the InteractiveShell instance.
74
75 pre_run_cell
76 ------------
77
78 ``pre_run_cell`` fires prior to interactive execution (e.g. a cell in a notebook).
79 It can be used to note the state prior to execution, and keep track of changes.
80 An object containing information used for the code execution is provided as an argument.
81
82 pre_execute
83 -----------
84
85 ``pre_execute`` is like ``pre_run_cell``, but is triggered prior to *any* execution.
86 Sometimes code can be executed by libraries, etc. which
87 skipping the history/display mechanisms, in which cases ``pre_run_cell`` will not fire.
88
89 post_run_cell
90 -------------
91
92 ``post_run_cell`` runs after interactive execution (e.g. a cell in a notebook).
93 It can be used to cleanup or notify or perform operations on any side effects produced during execution.
94 For instance, the inline matplotlib backend uses this event to display any figures created but not explicitly displayed during the course of the cell.
95 The object which will be returned as the execution result is provided as an
96 argument.
97
98 post_execute
99 ------------
100
101 The same as ``pre_execute``, ``post_execute`` is like ``post_run_cell``,
102 but fires for *all* executions, not just interactive ones.
103
104
105 .. seealso::
106
107 Module :mod:`IPython.core.hooks`
108 The older 'hooks' system allows end users to customise some parts of
109 IPython's behaviour.
110
111 :doc:`inputtransforms`
112 By registering input transformers that don't change code, you can monitor
113 what is being executed.