[SciPy-user] Solved-ish: matplotlib, Tkinter and multi-threaded web applications
zunzun at zunzun.com
zunzun at zunzun.com
Sun Oct 14 20:27:32 EDT 2007
On Sun, Oct 14, 2007 at 07:23:19PM -0500, Robert Kern wrote:
>
> Please show us the code that you used on the matplotlib list.
timesRequested = 0
def getBufferForPNG(self):
global timesRequested # use the global variable defined at the top of the module
# make a buffer to hold our data
buffer = StringIO.StringIO()
# pylab uses Tkinter, so this must be the top-level thread calling Tk.
# calling pylab.close() here forces pylab to create a new top-level thread
pylab.close()
# tell matplotlib to use the 'Anti-Grain Geometry' toolkit
matplotlib.use('Agg')
canvas = pylab.get_current_fig_manager().canvas
# quick simple plot
pylab.plot([1+timesRequested, 2+timesRequested, 3+timesRequested],[40+timesRequested,
50+timesRequested, 60+timesRequested])
timesRequested += 1 # this is only to give a different graph with each refresh
canvas.draw()
imageSize = canvas.get_width_height()
imageRgb = canvas.tostring_rgb()
pilImage = PIL.Image.fromstring("RGB", imageSize, imageRgb)
pilImage.save(buffer, "PNG") # <-- we will be sending the browser a "PNG file"
pylab.close() # close again to be sure of Tk threading
return buffer
More information about the SciPy-User
mailing list