尝试将简单的绘图(保存在StringIO中)返回到Web浏览器。经过几个小时的阅读,终于走近了,也许吧。
import cgi
import cStringIO
import matplotlib.pyplot as plt
import numpy as np
def doit():
x = np.linspace(-2,2,100)
y = np.sin(x)
format = "png"
ggg = cStringIO.StringIO()
plt.plot(x, y)
plt.savefig(ggg, format=format)
data_uri = ggg.read().encode('base64').replace('\n', '')
img_tag = '<img src="data:image/png;base64,{0}" alt="thisistheplot"/>'.format(data_uri)
print("Content-type: text/html\n")
print("<title>Try Ageen</title>")
print("<h1>Hi</h1>")
print(img_tag)
doit()
它返回了一张损坏的图片图标。我已经看过这个了:Dynamically serving a matplotlib image to the web using python,等等......
答案 0 :(得分:0)
实际上,只是搞清楚了。将留下发布,因为我没有看到这种方法被采取并希望它可以帮助:
#!/usr/bin/env python
import cgi
import cStringIO
import matplotlib.pyplot as plt
import numpy as np
def doit():
x = np.linspace(-2,2,100)
y = np.sin(x)
format = "png"
sio = cStringIO.StringIO()
plt.plot(x, y)
plt.savefig(sio, format=format)
data_uri = sio.getvalue().encode('base64').replace('\n', '')
img_tag = '<img src="data:image/png;base64,{0}" alt="sucka" />'.format(data_uri)
print("Content-type: text/html\n")
print("<title>Try Ageen</title>")
print("<h1>Hi</h1>")
print(img_tag)
doit()
目标是让客户输入三角函数并将其返回到后续页面。欢迎提出任何有助于此代码执行/看起来更好的评论。