Client-side web scripting in Python?

stalin woodsplitter at
Tue Jan 1 00:41:32 EST 2002

rolffreimuth at (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 
> accessible from Javascript.  It seems necessary to be able to 
> use that document object in order to modify the contents of the 
> page.

  You're absolutely right that it's necessary to access the document
object in order to dynamically modify page contents.  Javascript
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>

<script language="Python">
document = window.document
alert = window.alert
prompt = window.prompt

def toggleDivColor(div, colors=('#ff0000', '')): = ( != colors[0] and colors[0]) 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[0].href == url:
            return 1
    return 0



<div id="existingDiv">
Even when I'm doing an IE-specific project, I try to stick to the W3C
rather using Microsoft-specific methods/properties.  E.g.:
rather than
<code>document.all.coolSiteList</code> .  This soothes my
conscience and
makes the code more portable to other browsers if future support for
them is
desirable.  Even so, I wouldn't put anything IE-specific on the
Internet; only
on intranets or client-side apps with a DHTML interface.

<ul id="coolSiteList">
    <li><a href=""></a></li>
    <li><a href=""></a></li>
    <li><a href="">Gecko
DOM Reference (fairly standards-compliant)</a></li>
    <li><a href="">MS
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:', ''))">



More information about the Python-list mailing list