List Info

Thread: Dynamic image rendering example with cherrypy




Dynamic image rendering example with cherrypy
user name
2007-12-23 14:53:09
import cherrypy
from cherrypy import tools
from pylab import *
from numpy import *
import os
# To execute : python root.py then open your web browser at
the
appropriate cherrypy web server port. e.g :
# http://localhost:8080

class Root:
	cherrypy.expose
	def index(self):
		return """<html>
<head></head>
<body>
Please enter alpha value:
<form action="visu"
method="post">
<input type="text" name="alph"
value="1.0"  />
</form>
</body>
</html>
"""

	cherrypy.expose
        def showimage(self):
                cherrypy.response.headers['Content-Type']=
"image/png"
		f = open("sin.png", "rb")
		contents = f.read()
		f.close()
                return   contents


	cherrypy.expose
	def visu(self, alph = 1.0):
		_header = """
				<html>
				<head>
				<title>Random notes</<title>
				<link rel="stylesheet"
type="text/css"
href="/style.css"></link>
				</head>
				<body>
				<div
class="container">"""
		_footer = """
				</div>
				</body>
				</html>"""
		ioff()
		x = arange(0, 10, 0.01)
		alpha = eval(alph)
		subplot(1,2,1), plot(x, sin(alpha*x), '.-')
		subplot(1,2,2), plot(x,sin(alpha*x*cos(alpha*x)), 'o-')
		savefig("sin.png", dpi=96)
		cherrypy.response.headers['Content-Type']= 'text/html'
		page = [_header]
		page.append('<img src="/showimage/"
width="800" height="400" />' )
		page.append(_footer)
		return page

if __name__ == '__main__':
	cherrypy.quickstart(Root())

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Dynamic image rendering example with cherrypy
user name
2007-12-27 02:59:47
Hi there,

Some comments inline:

On Dec 23, 2007 8:53 PM, spock <florian.de-vuystecp.fr> wrote:
>
> import cherrypy
> from cherrypy import tools
> from pylab import *
> from numpy import *
> import os
> # To execute : python root.py then open your web
browser at the
> appropriate cherrypy web server port. e.g :
> # http://localhost:8080
>
> class Root:
>         cherrypy.expose
>         def index(self):
>                 return """<html>
> <head></head>
> <body>
> Please enter alpha value:
> <form action="visu"
method="post">
> <input type="text" name="alph"
value="1.0"  />
> </form>
> </body>
> </html>
> """
>
>         cherrypy.expose
>         def showimage(self):
>                
cherrypy.response.headers['Content-Type']=
"image/png"
>                 f = open("sin.png",
"rb")
>                 contents = f.read()
>                 f.close()
>                 return   contents
>
>
>         cherrypy.expose
>         def visu(self, alph = 1.0):
>                 _header = """
>                                 <html>
>                                 <head>
>                                 <title>Random
notes</<title>
>                                 <link
rel="stylesheet" type="text/css"
href="/style.css"></link>
>                                 </head>
>                                 <body>
>                                 <div
class="container">"""
>                 _footer = """
>                                 </div>
>                                 </body>
>                                
</html>"""
>                 ioff()
>                 x = arange(0, 10, 0.01)
>                 alpha = eval(alph)

Beware of the glaring security problem there, you are
eval()ing input
controlled by the remote user.

>                 subplot(1,2,1), plot(x, sin(alpha*x),
'.-')
>                 subplot(1,2,2),
plot(x,sin(alpha*x*cos(alpha*x)), 'o-')
>                 savefig("sin.png", dpi=96)

You might want to think about concurrency here, unless there
is only
one user. Reusing the same filename sin.png could lead to
problems
where one user's request to visu happens between another
one's
requests to visu and showimage, effectively overwriting
sin.png before
the latter can retreive his.

One idea is to use the tempfile module to generate a unique
temporary filename:
http:
//docs.python.org/lib/module-tempfile.html

Perhaps a better idea, is to output the image data directly
to the
response, without saving it to disk. Judging by

http://matplotlib.sourceforge.net/matplotlib.py
plot.html#-savefig

savefig can accept a file-like object instead of a filename.
In this
case, you would move the code that generates the plot to
showimage,
and call savefig with a StringIO instance as the first
parameter, and
return its value directly.

buf = StringIO()
savefig(buf, dpi=96, format='png')
return buf.getvalue()


>                
cherrypy.response.headers['Content-Type']= 'text/html'
>                 page = [_header]
>                 page.append('<img
src="/showimage/" width="800"
height="400" />' )
>                 page.append(_footer)
>                 return page
>
> if __name__ == '__main__':
>         cherrypy.quickstart(Root())


cheers,
Arnar

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )