Connect to the display hook of the current shell. The display hook gets called when the read-evaluate-print-loop (REPL) of the shell has finished the execution of a command. We use this callback to be able to automatically update a figure in interactive mode. This works both w
()
| 301 | |
| 302 | |
| 303 | def install_repl_displayhook() -> None: |
| 304 | """ |
| 305 | Connect to the display hook of the current shell. |
| 306 | |
| 307 | The display hook gets called when the read-evaluate-print-loop (REPL) of |
| 308 | the shell has finished the execution of a command. We use this callback |
| 309 | to be able to automatically update a figure in interactive mode. |
| 310 | |
| 311 | This works both with IPython and with vanilla python shells. |
| 312 | """ |
| 313 | global _REPL_DISPLAYHOOK |
| 314 | |
| 315 | if _REPL_DISPLAYHOOK is _ReplDisplayHook.IPYTHON: |
| 316 | return |
| 317 | |
| 318 | # See if we have IPython hooks around, if so use them. |
| 319 | # Use ``sys.modules.get(name)`` rather than ``name in sys.modules`` as |
| 320 | # entries can also have been explicitly set to None. |
| 321 | mod_ipython = sys.modules.get("IPython") |
| 322 | if not mod_ipython: |
| 323 | _REPL_DISPLAYHOOK = _ReplDisplayHook.PLAIN |
| 324 | return |
| 325 | ip = mod_ipython.get_ipython() |
| 326 | if not ip: |
| 327 | _REPL_DISPLAYHOOK = _ReplDisplayHook.PLAIN |
| 328 | return |
| 329 | |
| 330 | ip.events.register("post_execute", _draw_all_if_interactive) |
| 331 | _REPL_DISPLAYHOOK = _ReplDisplayHook.IPYTHON |
| 332 | |
| 333 | if mod_ipython.version_info[:2] < (8, 24): |
| 334 | # Use of backend2gui is not needed for IPython >= 8.24 as that functionality |
| 335 | # has been moved to Matplotlib. |
| 336 | # This code can be removed when Python 3.12, the latest version supported by |
| 337 | # IPython < 8.24, reaches end-of-life in late 2028. |
| 338 | from IPython.core.pylabtools import backend2gui |
| 339 | ipython_gui_name = backend2gui.get(get_backend()) |
| 340 | else: |
| 341 | _, ipython_gui_name = backend_registry.resolve_backend(get_backend()) |
| 342 | # trigger IPython's eventloop integration, if available |
| 343 | if ipython_gui_name: |
| 344 | ip.enable_gui(ipython_gui_name) |
| 345 | |
| 346 | |
| 347 | def uninstall_repl_displayhook() -> None: |
no test coverage detected
searching dependent graphs…