这个问题已经以类似的方式被问到了,here,但答案超出了我的理解(我是python和web开发的新手),所以我希望有一种更简单的方法,或者可以用不同的方式解释它。
我正在尝试使用matplotlib生成一个图像,并在不先向服务器写入文件的情况下提供它。我的代码可能有点傻,但它是这样的:
import cgi
import matplotlib.pyplot as pyplot
import cStringIO #I think I will need this but not sure how to use
...a bunch of matplotlib stuff happens....
pyplot.savefig('test.png')
print "Content-type: text/html\n"
print """<html><body>
...a bunch of text and html here...
<img src="test.png"></img>
...more text and html...
</body></html>
"""
我认为我应该创建一个cstringIO对象,然后执行如下操作,而不是执行pyplot.savefig('test.png'):
mybuffer=cStringIO.StringIO()
pyplot.savefig(mybuffer, format="png")
但从那时起我就迷失了方向。我见过的所有示例(例如http://lost-theory.org/python/dynamicimg.html)都涉及到这样的操作
print "Content-type: image/png\n"
我不知道如何将它与我已经输出的HTML集成在一起。
https://stackoverflow.com/questions/14824522
复制相似问题