CherryPy,从 matplotlib 加载图像,或者一般情况

发布于 2024-08-08 17:11:41 字数 850 浏览 2 评论 0原文

我不确定我做错了什么,如果你能指出我该读什么,那就太好了。我已经在第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风月客 2024-08-15 17:11:41

以下是一些对我有用的东西,但在您继续之前,我建议您阅读此页面 关于如何配置包含静态内容的目录。

问题 1:我如何知道文件的保存位置?
如果您指定文件的保存位置,查找文件的过程应该会变得更容易。
例如,您可以将图像文件保存到 CherryPy 应用程序目录中名为“img”的子目录中,如下所示:

fig.savefig('img/test.png') # note:  *no* forward slash before "img"

然后显示如下:

return '<img src="/img/test.png" />' # note:  forward slash before "img"

问题 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)

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:

fig.savefig('img/test.png') # note:  *no* forward slash before "img"

And then display like this:

return '<img src="/img/test.png" />' # note:  forward slash before "img"

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:

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文