How I can return jQuery's js code to frontend?

91 views
Skip to first unread message

HENG

unread,
Nov 8, 2012, 10:27:58 AM11/8/12
to python-tornado
Hi all:

I have some code like this:

class Handler(tornado.web.RequestHandler):
     def get(self):
          js = u"""
    // jquery's js code here
    ................
    // my js code here.
"""
          return self.write(js)

When I check the response from this handler in Chrome, I found that jQuery's code have some errors.

After debug, I found that some js Regexp has been change by python's string format. such as \\ -> \ or \"->"

Is anyone know it how to deal with? Thank you so much!

--
--------------------------------------------------------------------
@mywaiting
---------------------------------------------------------------------
--

Felinx Lee

unread,
Nov 8, 2012, 9:32:49 PM11/8/12
to python-...@googlegroups.com
Do you try to use raw string like this?

js = r"""
--
What can change the nature of a man?(Planescape Torment)
----------------------------------------------------------------------------------------
http://feilong.me            Felinx Lee's Blog (Chinese Only)

Jordon Mears

unread,
Nov 8, 2012, 11:36:03 PM11/8/12
to python-...@googlegroups.com

If you are just trying to serve out jquery's js without modifications, you may be better off using a static file handler.

HENG

unread,
Nov 9, 2012, 1:20:11 PM11/9/12
to python-tornado
It has problem to write code like that:

js = tornado.template.Template("""
// jquery here
.............
// my js codes here
...........
""")

and then in my request handler write code like that:

return self.finish(js.generate(*args, **kwargs))

But I run it as another way just save my js code into a file js.html, and it works right by the codes:

return self.render("js.html")

I am very confused that why tornado or python works like that. That two ways looks like as the same.

That any one would give me any tips to deal with it?

Thanks!


2012/11/9 Jordon Mears <jord...@gmail.com>



--
--------------------------------------------------------------------
HengZhou
---------------------------------------------------------------------
--

Russ Weeks

unread,
Nov 9, 2012, 1:32:16 PM11/9/12
to python-...@googlegroups.com
Hi, Heng,

Felinx and Jordon have both given you good advice.  Your first example does not work because python is treating some characters in the jQuery code as special characters.  To avoid this, use a raw string.  Look at this code, for instance:

>>> print """Hello\nWorld"""
Hello
World
>>> print r"""Hello\nWorld"""
Hello\nWorld
>>> 

In your second example, character sequences such as '\n' are not treated as escape characters because the data is read from a file.

But more importantly I think you should try to rewrite your code to do as Jordon suggests: use a static file handler to serve jQuery's javascript file, especially if you haven't modified it.

-Russ

Jordon Mears

unread,
Nov 9, 2012, 2:55:23 PM11/9/12
to python-...@googlegroups.com
http://www.tornadoweb.org/documentation/overview.html#static-files-and-aggressive-file-caching

This example is not the best but it shows a static file handler.
--
Jordon Mears
http://www.finefrog.com

HENG

unread,
Nov 10, 2012, 9:08:28 PM11/10/12
to python-tornado
Thanka all giving me tips to deal with the problem. Thanks.




2012/11/10 Jordon Mears <jord...@gmail.com>
Reply all
Reply to author
Forward
0 new messages