35
35
implicit and explicit interfaces.
36
36
"""
37
37
38
+ from enum import Enum
38
39
import functools
39
40
import importlib
40
41
import inspect
@@ -108,8 +109,15 @@ def _copy_docstring_and_deprecators(method, func=None):
108
109
109
110
## Global ##
110
111
111
- _IP_REGISTERED = None
112
- _INSTALL_FIG_OBSERVER = False
112
+
113
+ # The state controlled by {,un}install_repl_displayhook().
114
+ _ReplDisplayHook = Enum ("_ReplDisplayHook" , ["NONE" , "PLAIN" , "IPYTHON" ])
115
+ _REPL_DISPLAYHOOK = _ReplDisplayHook .NONE
116
+
117
+
118
+ def _draw_all_if_interactive ():
119
+ if matplotlib .is_interactive ():
120
+ draw_all ()
113
121
114
122
115
123
def install_repl_displayhook ():
@@ -119,30 +127,25 @@ def install_repl_displayhook():
119
127
120
128
This works both with IPython and with vanilla python shells.
121
129
"""
122
- global _IP_REGISTERED
123
- global _INSTALL_FIG_OBSERVER
130
+ global _REPL_DISPLAYHOOK
124
131
125
- if _IP_REGISTERED :
132
+ if _REPL_DISPLAYHOOK is _ReplDisplayHook . IPYTHON :
126
133
return
134
+
127
135
# See if we have IPython hooks around, if so use them.
128
136
# Use ``sys.modules.get(name)`` rather than ``name in sys.modules`` as
129
137
# entries can also have been explicitly set to None.
130
138
mod_ipython = sys .modules .get ("IPython" )
131
139
if not mod_ipython :
132
- _INSTALL_FIG_OBSERVER = True
140
+ _REPL_DISPLAYHOOK = _ReplDisplayHook . PLAIN
133
141
return
134
142
ip = mod_ipython .get_ipython ()
135
143
if not ip :
136
- _INSTALL_FIG_OBSERVER = True
144
+ _REPL_DISPLAYHOOK = _ReplDisplayHook . PLAIN
137
145
return
138
146
139
- def post_execute ():
140
- if matplotlib .is_interactive ():
141
- draw_all ()
142
-
143
- ip .events .register ("post_execute" , post_execute )
144
- _IP_REGISTERED = post_execute
145
- _INSTALL_FIG_OBSERVER = False
147
+ ip .events .register ("post_execute" , _draw_all_if_interactive )
148
+ _REPL_DISPLAYHOOK = _ReplDisplayHook .IPYTHON
146
149
147
150
from IPython .core .pylabtools import backend2gui
148
151
# trigger IPython's eventloop integration, if available
@@ -161,16 +164,12 @@ def uninstall_repl_displayhook():
161
164
this will reset `sys.displayhook` to what ever function was there when
162
165
Matplotlib installed its displayhook, possibly discarding your changes.
163
166
"""
164
- global _IP_REGISTERED
165
- global _INSTALL_FIG_OBSERVER
166
- if _IP_REGISTERED :
167
+ global _REPL_DISPLAYHOOK
168
+ if _REPL_DISPLAYHOOK is _ReplDisplayHook .IPYTHON :
167
169
from IPython import get_ipython
168
170
ip = get_ipython ()
169
- ip .events .unregister ('post_execute' , _IP_REGISTERED )
170
- _IP_REGISTERED = None
171
-
172
- if _INSTALL_FIG_OBSERVER :
173
- _INSTALL_FIG_OBSERVER = False
171
+ ip .events .unregister ("post_execute" , _draw_all_if_interactive )
172
+ _REPL_DISPLAYHOOK = _ReplDisplayHook .NONE
174
173
175
174
176
175
draw_all = _pylab_helpers .Gcf .draw_all
@@ -810,7 +809,7 @@ def figure(num=None, # autoincrement if None, else integer from 1-N
810
809
# FigureManager base class.
811
810
draw_if_interactive ()
812
811
813
- if _INSTALL_FIG_OBSERVER :
812
+ if _REPL_DISPLAYHOOK is _ReplDisplayHook . PLAIN :
814
813
fig .stale_callback = _auto_draw_if_interactive
815
814
816
815
if clear :
0 commit comments