CherryPy,从 matplotlib 加载图像,或者一般情况
我不确定我做错了什么,如果你能指出我该读什么,那就太好了。我已经在第一个 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)
I am not sure what I am doing wrong, It would be great if you could point me toward what to read. I have taken the first CherryPy tutorial "hello world" added a little matplotlib plot.
Question 1: how do I know where the file will be saved? It happens to be where I am running the file.
Question 2: I don't seem to be get the image to open/view in my browser. When I view source in the browser everything looks right but no luck, even when I am including the full image path.
I think my problem is with the path but not sure the mechanics of what is happening
thanks for the help
Vincent
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是一些对我有用的东西,但在您继续之前,我建议您阅读此页面 关于如何配置包含静态内容的目录。
问题 1:我如何知道文件的保存位置?
如果您指定文件的保存位置,查找文件的过程应该会变得更容易。
例如,您可以将图像文件保存到 CherryPy 应用程序目录中名为“img”的子目录中,如下所示:
然后显示如下:
问题 2:我似乎无法[能够]将图像获取到在我的浏览器中打开/查看。
这是我用来为 CherryPy 应用程序提供静态图像的一种方法:
Below are some things that have worked for me, but before you proceed further I recommend that you read this page about how to configure directories which contain static content.
Question 1: How do I know where the file will be saved?
If you dictate where the file should be saved, the process of finding it should become easier.
For example, you could save image files to a subdirectory called "img" within your CherryPy application directory like this:
And then display like this:
Question 2: I don't seem to be [able to] get the image to open/view in my browser.
Here is one way I've used to make static images available to a CherryPy application: