Client-side web scripting in Python?
woodsplitter at rocketmail.com
Tue Jan 1 00:41:32 EST 2002
rolffreimuth at hotmail.com (Rolf) wrote:
> I am considering writing a simple application in Python. I am
> evaluating the possibility of writing the GUI for the app as a static
> HTML page that uses client-side script.
I agree with Paul that this is a bad idea if the general Internet
user is your target audience, but I suspect you know that. You're
probably developing an intranet application or a 'classic desktop
application' with a DHTML interface.
> Python does not seem to know about the document object which is
> use that document object in order to modify the contents of the
You're absolutely right that it's necessary to access the document
establishes various aliases for the purpose of convenience ('document'
is actually 'window.document', 'alert' is actually 'window.alert',
etc.). If I recall correctly, older version of the Win32 bindings for
Python did not establish these shortcuts; the programmer had to refer
to objects by their fully qualified names. An easy way to circumvent
this is to establish the aliases yourself as global variables at the
beginning of the first <SCRIPT language="Python"> element in HEAD
(i.e., 'document = window.document', etc.).
That said, right now I'm using IE 5.01 + standard Python 2.2 +
win32all 141 on WinNT 4.0, and the aforementioned aliases seem to be
established automatically; that behavior must be specific to recent
versions of the Win32 bindings.
To learn the fully qualified names for JScript's various aliases,
you can consult the Microsoft HTML/DHTML object model and element
Beware that IE's DHTML DOM support has diverged quite a bit from the
W3C standard; Gecko is much closer to the standard at this point:
IE's failure to comply with standards probably doesn't matter much
in your situation, since what you're doing is IE-specific from the
start (though something similar might be possible in Mozilla with
XPCOM?). I just had to mention it to preserve my self-respect.
Here's a trivial example:
<title>Trivial Client-Side Python Example</title>
document = window.document
alert = window.alert
prompt = window.prompt
def toggleDivColor(div, colors=('#ff0000', '')):
div.style.color = (div.style.color != colors and colors) or
def addCoolSiteLink(lst, url):
if not url:
if isLinkAlreadyPresent(lst, url):
alert('Link to\n%s\nalready present.' % url)
li = document.createElement('LI')
a = document.createElement('A')
a.href = a.innerHTML = url
def isLinkAlreadyPresent(lst, url):
for li in lst.getElementsByTagName('LI'):
anchors = li.getElementsByTagName('A')
if anchors.length > 0 and anchors.href == url:
Even when I'm doing an IE-specific project, I try to stick to the W3C
rather using Microsoft-specific methods/properties. E.g.:
<code>document.all.coolSiteList</code> . This soothes my
makes the code more portable to other browsers if future support for
desirable. Even so, I wouldn't put anything IE-specific on the
on intranets or client-side apps with a DHTML interface.
DOM Reference (fairly standards-compliant)</a></li>
DOM Reference (not standards-compliant)</a></li>
<input type="button" value="Toggle Rant Color"
<input type="button" value="Insert Link to Cool Site"
prompt('Enter the URL of a cool site:', 'http://python.org'))">
More information about the Python-list