I'm testing a Django app running locally using the test server.
One of my views returns a dictionary to its template, main.html
:
d = {'f1':'/f1.png','f2':'/f2.png'}
return render_to_response('myapp/main.html', d, context)
Each url (e.g., '/f1.png'
) in d
will generate and return a figure:
canvas=FigureCanvas(fig)
response=HttpResponse(content_type='image/png')
canvas.print_png(response,dpi=400)
return response
There are no errors in either view that generate the figures.
But, second figure sometimes fails to render on the main page (main.html
).
When this occurs, I get the following error from the test server:
"GET /url_for_my_app/f2.png HTTP/1.1" 500 59
error: [Errno 32] Broken pipe
I realize this may be due to browser-request timing (e.g., error: [Errno 32] Broken pipe).
(1) What may cause the error in this case?
(2) How is it best addressed?
Edit: I identified the apparent cause (but only empirically --- I don't know why it causes the error). The view that generates the second figure included plt.tight_layout()
prior to returning the fig
object (residual from running in notebook). The error goes away when plt.tight_layout()
is removed.
plt.tight_layout()
canvas=FigureCanvas(fig)
response=HttpResponse(content_type='image/png')
canvas.print_png(response,dpi=400)
return response