##// 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 This is meant to be called automatically and will call show() if, during
141 This is meant to be called automatically and will call show() if, during
142 prior code execution, there had been any calls to draw_if_interactive.
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 if not show._draw_called:
149 if not show._draw_called:
145 return
150 return
146
151
147 if InlineBackend.instance().close_figures:
152 if InlineBackend.instance().close_figures:
148 # ignore the tracking, just draw and close all figures
153 # ignore the tracking, just draw and close all figures
149 return show(True)
154 try:
150
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 try:
163 try:
152 # exclude any figures that were closed:
164 # exclude any figures that were closed:
153 active = set([fm.canvas.figure for fm in Gcf.get_all_fig_managers()])
165 active = set([fm.canvas.figure for fm in Gcf.get_all_fig_managers()])
154 for fig in [ fig for fig in show._to_draw if fig in active ]:
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 finally:
176 finally:
157 # clear flags for next round
177 # clear flags for next round
158 show._to_draw = []
178 show._to_draw = []
@@ -162,12 +182,11 b' def flush_figures():'
162 def send_figure(fig):
182 def send_figure(fig):
163 """Draw the current figure and send it as a PNG payload.
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 fmt = InlineBackend.instance().figure_format
185 fmt = InlineBackend.instance().figure_format
170 data = print_figure(fig, fmt)
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 mimetypes = { 'png' : 'image/png', 'svg' : 'image/svg+xml' }
190 mimetypes = { 'png' : 'image/png', 'svg' : 'image/svg+xml' }
172 mime = mimetypes[fmt]
191 mime = mimetypes[fmt]
173 # flush text streams before sending figures, helps a little with output
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