passing Python data to a javascript function
Chris Rebert
clp2 at rebertia.com
Wed Oct 26 22:42:09 EDT 2011
On Wed, Oct 26, 2011 at 7:25 PM, Bill Allen <wallenpb at 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
More information about the Python-list
mailing list