Render JSON

182 views
Skip to first unread message

Rob Sandhu

unread,
Sep 17, 2009, 5:16:50 PM9/17/09
to Tornado Web Server
Developing an application with Google AJAX APIs, I would like to
return a JSON string result but am encountering an error with the
render method:

Handler code:

url = options.google_local_search + "q=Vancouver"
request = urllib2.Request(url)
response = urllib2.urlopen(request)
self.render(response)

Errors:

File "/Library/Python/2.5/site-packages/tornado/web.py", line 688, in
_execute
getattr(self, self.request.method.lower())(*args, **kwargs)
File "myapp.py", line 207, in get
self.render(response)
File "/Library/Python/2.5/site-packages/tornado/web.py", line
301, in render
html = self.render_string(template_name, **kwargs)
File "/Library/Python/2.5/site-packages/tornado/web.py", line
387, in render_string
t = RequestHandler._templates[template_path].load
(template_name)
File "/Library/Python/2.5/site-packages/tornado/template.py",
line 182, in load
path = os.path.join(self.root, name)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/
lib/python2.5/posixpath.py", line 60, in join
if b.startswith('/'):
AttributeError: addinfourl instance has no attribute 'startswith'


Thanks,
Rob

Benjamin Golub

unread,
Sep 17, 2009, 5:22:02 PM9/17/09
to python-...@googlegroups.com
render() is used to render a template: http://github.com/facebook/tornado/blob/master/tornado/web.py#L299. If you just want to write out a string use write(). See http://github.com/facebook/tornado/blob/master/demos/helloworld/helloworld.py for an example.

Christian Scholz

unread,
Sep 17, 2009, 5:22:19 PM9/17/09
to python-...@googlegroups.com
Rob Sandhu wrote:
> Developing an application with Google AJAX APIs, I would like to
> return a JSON string result but am encountering an error with the
> render method:
>
> Handler code:
>
> url = options.google_local_search + "q=Vancouver"
> request = urllib2.Request(url)
> response = urllib2.urlopen(request)
> self.render(response)

urlopen() returns not a string but a file-like object, see
http://docs.python.org/library/urllib2.html

So you first need to read that file and pass the result to the render()
method.

-- Christian


>
> Errors:
>
> File "/Library/Python/2.5/site-packages/tornado/web.py", line 688, in
> _execute
> getattr(self, self.request.method.lower())(*args, **kwargs)
> File "myapp.py", line 207, in get
> self.render(response)
> File "/Library/Python/2.5/site-packages/tornado/web.py", line
> 301, in render
> html = self.render_string(template_name, **kwargs)
> File "/Library/Python/2.5/site-packages/tornado/web.py", line
> 387, in render_string
> t = RequestHandler._templates[template_path].load
> (template_name)
> File "/Library/Python/2.5/site-packages/tornado/template.py",
> line 182, in load
> path = os.path.join(self.root, name)
> File "/System/Library/Frameworks/Python.framework/Versions/2.5/
> lib/python2.5/posixpath.py", line 60, in join
> if b.startswith('/'):
> AttributeError: addinfourl instance has no attribute 'startswith'
>
>
> Thanks,
> Rob


--
Christian Scholz Homepage: http://comlounge.net
COM.lounge GmbH blog: http://mrtopf.de/blog
Hanbrucher Str. 33 Skype: HerrTopf
52064 Aachen Video Blog: http://comlounge.tv
Tel: +49 241 400 730 0 E-Mail c...@comlounge.net
Fax: +49 241 979 00 850 IRC: MrTopf, Tao_T

neuer Podcast: Der OpenWeb-Podcast (http://openwebpodcast.de)
new podcast: Data Without Borders (http://datawithoutborders.net)

Jed Schmidt

unread,
Sep 17, 2009, 5:28:01 PM9/17/09
to Tornado Web Server
I think you're better off doing it asynchronously, since that's where
this framework shines:

class MyHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous

def get(self):
url = options.google_local_search + "q=Vancouver"
http = tornado.httpclient.AsyncHTTPClient()
http.fetch( url, callback=self.async_callback
( self.on_response ) )

def on_response( self, response ):
if response.error: raise tornado.web.HTTPError( 500 )
self.write( response.body )
self.finish()

Jed Schmidt

Rob Sandhu

unread,
Sep 17, 2009, 5:30:22 PM9/17/09
to Tornado Web Server
Added the JSON parsing:

results = simplejson.load(response)

The 'write()' method worked also, now I can make use of the results on
the client end.

Thanks for the quick replies,
Rob

On Sep 17, 2:22 pm, Christian Scholz <c...@comlounge.net> wrote:
> Rob Sandhu wrote:
> > Developing an application with Google AJAX APIs, I would like to
> > return a JSON string result but am encountering an error with the
> > render method:
>
> > Handler code:
>
> > url = options.google_local_search + "q=Vancouver"
> > request = urllib2.Request(url)
> > response = urllib2.urlopen(request)
> > self.render(response)
>
> urlopen() returns not a string but a file-like object, seehttp://docs.python.org/library/urllib2.html

Bret Taylor

unread,
Sep 17, 2009, 6:51:05 PM9/17/09
to python-...@googlegroups.com
tornado.escape.json_decode is recommended - it uses the Python 2.6 json module if available, or simplejson if it is not available.

Bret
Reply all
Reply to author
Forward
0 new messages