尝试将简单的绘图(保存在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,还有其他的…
发布于 2017-01-06 15:34:30
实际上,我只是想通了。我会留下帖子,因为我还没有看到这种方法被采用,希望它能有所帮助:
#!/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()目标是让客户端输入一个三角函数,并将其返回到后续页面。不过,欢迎任何有助于此代码执行/看起来更好的注释。
https://stackoverflow.com/questions/41500921
复制相似问题