Vin*_*ent 4 python matplotlib cherrypy
我不确定我做错了什么,如果你能指出我要读的东西,那就太好了.我已经采取了第一个CherryPy教程"hello world"添加了一个小的matplotlib图.问题1:我如何知道文件的保存位置?它恰好是我运行文件的地方.问题2:我似乎没有在我的浏览器中打开/查看图像.当我在浏览器中查看源代码时,即使我包含完整的图像路径,一切看起来都正确但没有运气.我认为我的问题在于路径,但不确定发生了什么的机制
感谢文森特的帮助
import cherrypy
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
class HelloWorld:
def index(self):
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot([1,2,3])
fig.savefig('test.png')
return ''' <img src="test.png" width="640" height="480" border="0" /> '''
index.exposed = True
import os.path
tutconf = os.path.join(os.path.dirname(__file__), 'tutorial.conf')
if __name__ == '__main__':
cherrypy.quickstart(HelloWorld(), config=tutconf)
else:
cherrypy.tree.mount(HelloWorld(), config=tutconf)
Run Code Online (Sandbox Code Playgroud)
下面是一些对我有用的东西,但在你继续进行之前,我建议你阅读这个页面,了解如何配置包含静态内容的目录.
问题1:我如何知道文件的保存位置?
如果您指定应保存文件的位置,则查找文件的过程应该变得更加容易.
例如,您可以将图像文件保存到CherryPy应用程序目录中名为"img"的子目录中,如下所示:
fig.savefig('img/test.png') # note: *no* forward slash before "img"
Run Code Online (Sandbox Code Playgroud)
然后显示如下:
return '<img src="/img/test.png" />' # note: forward slash before "img"
Run Code Online (Sandbox Code Playgroud)
问题2:我似乎没有[能够]在浏览器中打开/查看图像.
这是我用来为CherryPy应用程序提供静态图像的一种方法:
if __name__ == '__main__':
import os.path
currdir = os.path.dirname(os.path.abspath(__file__))
conf = {'/css/style.css':{'tools.staticfile.on':True,
'tools.staticfile.filename':os.path.join(currdir,'css','style.css')},
'/img':{'tools.staticdir.on':True,
'tools.staticdir.dir':os.path.join(currdir,'img')}}
cherrypy.quickstart(root, "/", config=conf)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1925 次 |
| 最近记录: |