views:

248

answers:

1

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)
+1  A: 

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)
Adam Bernier
so how is "CherryPy application directory" determined? I had thought it was where the myapp.py file was run from. I have not yet read the static file info you recommended. Doing that now. Thanks
Vincent
The CherryPy application directory is located wherever your myapp.py file is located, you are correct. In my example the "img" subdirectory is in the same directory as the myapp.py file.
Adam Bernier
I was running myapp.py by"python /Users/vmd/Downloads/CherryPy-2.3.0/cherrypy/tutorial/tut01_helloworld1.py"Which didn't work. It seems that the file is saved in the directory you are in when you run myapp.pyafter "cd /Users/vmd/Downloads/CherryPy-2.3.0/cherrypy/tutorial"then "python tut01_helloworld1.py" it workedThanks againVincent
Vincent
Vincent, that's why it's best to always use absolute paths when interacting with the filesystem in a web application--if they're relative, that makes it harder to play with various startup scenarios (like /etc/init.d scripts, for example). Use `abspath` and `__file__` like Adam did and you'll be happier. :)
fumanchu
Glad that helped out, Vincent :-)
Adam Bernier
Thank you, fumanchu, for your work and the work of the entire CherryPy team. Your product is flawless as far as I can see :-)
Adam Bernier