Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #15019

Re: passing Python data to a javascript function

References <CACrcdXvi6NDY5CM_AqH0BB6CVx8TRbTBjATu4kbYbcyBJfJNwQ@mail.gmail.com> <CAMuTYXhjuYuouX7+u5h1bE43x9ceWm9aUZ5CoDtu0c2Geuz8ZA@mail.gmail.com> <CACrcdXsXUkLWtkwYADMF-wMjsKjL7VCcSaKW-Y9D83_7Ox5-OQ@mail.gmail.com>
Date 2011-10-26 19:42 -0700
Subject Re: passing Python data to a javascript function
From Chris Rebert <clp2@rebertia.com>
Newsgroups comp.lang.python
Message-ID <mailman.2244.1319683332.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Oct 26, 2011 at 7:25 PM, Bill Allen <wallenpb@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: passing Python data to a javascript function Chris Rebert <clp2@rebertia.com> - 2011-10-26 19:42 -0700

csiph-web