Install a repl display hook so that any stale figure are automatically redrawn when control is returned to the repl. This works both with IPython and with vanilla python shells.
()
| 80 | |
| 81 | |
| 82 | def install_repl_displayhook(): |
| 83 | """ |
| 84 | Install a repl display hook so that any stale figure are automatically |
| 85 | redrawn when control is returned to the repl. |
| 86 | |
| 87 | This works both with IPython and with vanilla python shells. |
| 88 | """ |
| 89 | global _IP_REGISTERED |
| 90 | global _INSTALL_FIG_OBSERVER |
| 91 | |
| 92 | class _NotIPython(Exception): |
| 93 | pass |
| 94 | |
| 95 | # see if we have IPython hooks around, if use them |
| 96 | |
| 97 | try: |
| 98 | if 'IPython' in sys.modules: |
| 99 | from IPython import get_ipython |
| 100 | ip = get_ipython() |
| 101 | if ip is None: |
| 102 | raise _NotIPython() |
| 103 | |
| 104 | if _IP_REGISTERED: |
| 105 | return |
| 106 | |
| 107 | def post_execute(): |
| 108 | if matplotlib.is_interactive(): |
| 109 | draw_all() |
| 110 | |
| 111 | # IPython >= 2 |
| 112 | try: |
| 113 | ip.events.register('post_execute', post_execute) |
| 114 | except AttributeError: |
| 115 | # IPython 1.x |
| 116 | ip.register_post_execute(post_execute) |
| 117 | |
| 118 | _IP_REGISTERED = post_execute |
| 119 | _INSTALL_FIG_OBSERVER = False |
| 120 | |
| 121 | # trigger IPython's eventloop integration, if available |
| 122 | from IPython.core.pylabtools import backend2gui |
| 123 | |
| 124 | ipython_gui_name = backend2gui.get(get_backend()) |
| 125 | if ipython_gui_name: |
| 126 | ip.enable_gui(ipython_gui_name) |
| 127 | else: |
| 128 | _INSTALL_FIG_OBSERVER = True |
| 129 | |
| 130 | # import failed or ipython is not running |
| 131 | except (ImportError, _NotIPython): |
| 132 | _INSTALL_FIG_OBSERVER = True |
| 133 | |
| 134 | |
| 135 | def uninstall_repl_displayhook(): |
no test coverage detected