##// END OF EJS Templates
protect flush_figures post-exec function from user error...
MinRK -
Show More
@@ -140,19 +140,39 b' def flush_figures():'
140 140
141 141 This is meant to be called automatically and will call show() if, during
142 142 prior code execution, there had been any calls to draw_if_interactive.
143
144 This function is meant to be used as a post_execute callback in IPython,
145 so user-caused errors are handled with showtraceback() instead of being
146 allowed to raise. If this function is not called from within IPython,
147 then these exceptions will raise.
143 148 """
144 149 if not show._draw_called:
145 150 return
146 151
147 152 if InlineBackend.instance().close_figures:
148 153 # ignore the tracking, just draw and close all figures
149 return show(True)
150
154 try:
155 return show(True)
156 except Exception as e:
157 # safely show traceback if in IPython, else raise
158 try:
159 get_ipython().showtraceback()
160 return
161 except NameError:
162 raise e
151 163 try:
152 164 # exclude any figures that were closed:
153 165 active = set([fm.canvas.figure for fm in Gcf.get_all_fig_managers()])
154 166 for fig in [ fig for fig in show._to_draw if fig in active ]:
155 send_figure(fig)
167 try:
168 send_figure(fig)
169 except Exception as e:
170 # safely show traceback if in IPython, else raise
171 try:
172 get_ipython().showtraceback()
173 break
174 except NameError:
175 raise e
156 176 finally:
157 177 # clear flags for next round
158 178 show._to_draw = []
@@ -162,12 +182,11 b' def flush_figures():'
162 182 def send_figure(fig):
163 183 """Draw the current figure and send it as a PNG payload.
164 184 """
165 # For an empty figure, don't even bother calling figure_to_svg, to avoid
166 # big blank spaces in the qt console
167 if not fig.axes:
168 return
169 185 fmt = InlineBackend.instance().figure_format
170 186 data = print_figure(fig, fmt)
187 # print_figure will return None if there's nothing to draw:
188 if data is None:
189 return
171 190 mimetypes = { 'png' : 'image/png', 'svg' : 'image/svg+xml' }
172 191 mime = mimetypes[fmt]
173 192 # flush text streams before sending figures, helps a little with output
General Comments 0
You need to be logged in to leave comments. Login now