Add these imports:
from cherrypy.lib import file_generator import StringIO
and then follow these steps:
def index(self): surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) cherrypy.response.headers['Content-Type'] = "image/png" buffer = StringIO.StringIO() surface.write_to_png(buffer) buffer.seek(0) return file_generator(buffer)
Additionally, if you are using a stand-alone file (i.e. it is not part of the web page) and you do not want it to be displayed in the browser, but rather viewed as a file for saving to disk, then you need one more header:
cherrypy.response.headers['Content-Disposition'] = 'attachment; filename="file.png"'
Besides, is it better to create and hold this image in memory (for example, I'm trying to make it) or write it to disk as a temporary file and file it from there? only i need the image once, then it can be discarded.
If the only thing you want to do is serve this file in a browser, there is no reason to create it on disk on the server. On the contrary, remember that access to the hard drive leads to poor performance.
zifot
source share