Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: passing Python data to a javascript function

107 views
Skip to first unread message

Benjamin Kaplan

unread,
Oct 26, 2011, 6:58:07 PM10/26/11
to pytho...@python.org
On Wed, Oct 26, 2011 at 6:18 PM, Bill Allen <wall...@gmail.com> wrote:
>
> I am writing a Python CGI and am needing to pass a data value from Python to a javascript function.   My understanding is that I should use JSON as the middleman.  However, I have not found a good example of doing this.   The piece of data is a simple integer, but I could easily covert that to a string first if necessary.   Here is what I am trying, but unsuccessfully.  I am sure that I have more than one issue in this code.
>
>
>
> #!/usr/bin/python
>
> import json, os, cgi, cgitb
> cgitb.enable()
>
> pid = [{'p':str(os.getpid())}]
> pid_data = json.dumps(pid)
>
> print "Content-type: text/html"
> print
>
> print """
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
> <html xmlns="http://www.w3.org/1999/xhtml">
> <head><title>Test Page</title>
> <script type="text/javascript">
> function showPId(pid_data)
> {
> var a_pid=eval("(" + pid_data + ")");
> document.getElementById('txt').innerHTML=a_pid;
> }
> </script>
> </head>
> <body onload="startTime({0})">
>
> <center><div id="txt"></div></center><br>
>
> </body>
> </html>""".format(pid_data)
>

You're making this much more difficult than it needs to be. JSON is
used for sending data to JavaScript, meaning the JavaScript asks the
server for a bunch of data, the server sends the client JSON, and
everyone is happy. In your case, the information is available when the
javascript is being generated so you can just pass in the number, no
JSON needed.

Chris Rebert

unread,
Oct 26, 2011, 10:42:09 PM10/26/11
to Bill Allen, pytho...@python.org
On Wed, Oct 26, 2011 at 7:25 PM, Bill Allen <wall...@gmail.com> wrote:
>
> Benjamin,
>
> I was afraid I was doing that.   I have simplified it quite a bit, still not
> getting the output I am looking for.   I am down to that I am not passing
> the value in the onload=showPID() call correctly.  I know this is getting a
> bit far from a Python issue now, but if you have more insight on this, I
> would appreciate it.  Is there another way of passing the data from the
> Python script to the javascript than what I am doing in this CGI?

The problem is that you're not passing the data at all. You never
interpolate the pid_data value into the string(s) constituting your
embedded JavaScript (though you did do it just fine in the pure HTML).
The Python variable `pid_data` is not somehow magically accessible to
JavaScript; you must explicitly insert its value somewhere.

<snip>
> pid_data = str(os.getpid())
<snip>
> print """
<snip>
> <body onload="showPID(pid_data)">

Change that line to:
<body onload="showPID("""+pid_data+""")">

As an example, if the PID happens to be 42, then the outputted
fragment will end up being:
<body onload="showPID(42)">

As a sidenote, I would recommend using something higher-level than the
`cgi` module. Python has an abundance of web frameworks and templating
languages; take your pick.

Cheers,
Chris
--
http://rebertia.com

Chris Angelico

unread,
Oct 26, 2011, 11:55:28 PM10/26/11
to pytho...@python.org
On Thu, Oct 27, 2011 at 2:51 PM, Bill Allen <wall...@gmail.com> wrote:
> Chris,
>
> Wow, that seems so simple now that I see it.  I was dancing around that all
> day, but just not landing on it.   Thanks so very much for the assist.
>
> --Bill
>
> Final code that works perfectly, passes the value from the Python script to
> the javascript correctly:
>
> <body onload="showPID("""+pid_data+""")">

Congratulations! You've just written code that writes code. It takes a
bit to get your head around it (especially when you start worrying
about literal strings that might contain quotes, for instance), but
it's really cool and seriously powerful stuff. Your server-side Python
code can generate client-side Javascript code any way it likes...
unlimited possibilities.

ChrisA

en...@cleantechsolution.in

unread,
Aug 9, 2013, 4:36:09 AM8/9/13
to
On Thursday, October 27, 2011 9:25:28 AM UTC+5:30, Chris Angelico wrote:
> On Thu, Oct 27, 2011 at 2:51 PM, Bill Allen <wall...@gmail.com> wrote:
>
> > Chris,
>
> >
>
> > Wow, that seems so simple now that I see it.  I was dancing around that all
>
> > day, but just not landing on it.   Thanks so very much for the assist.
>
> >This code is giving an error 500 when run. Although the python code is independently running OK
0 new messages