开发者

CherryPy, load image from matplotlib, or in general

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 hel开发者_如何学Pythonp 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)


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜