Client-side web scripting in Python?

stalin woodsplitter at rocketmail.com
Tue Jan 1 06:41:32 CET 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 
> 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
reference:
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/dhtml_reference_entry.asp

  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:
http://www.brownhen.com/domref/dom_shortTOC.html

  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:
------------------------------------------
<html>

<head>
<title>Trivial Client-Side Python Example</title>

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

def toggleDivColor(div, colors=('#ff0000', '')):
    div.style.color = (div.style.color != colors[0] and colors[0]) or
colors[1]

def addCoolSiteLink(lst, url):
    if not url:
        return
    if isLinkAlreadyPresent(lst, url):
        alert('Link to\n%s\nalready present.' % url)
        return

    li = document.createElement('LI')
    a = document.createElement('A')
    a.href = a.innerHTML = url
    li.appendChild(a)
    lst.appendChild(li)

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
</script>

</head>

<body>

<div id="existingDiv">
Even when I'm doing an IE-specific project, I try to stick to the W3C
standard
rather using Microsoft-specific methods/properties.  E.g.:
<code>document.getElementById('coolSiteList')</code>
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.
</div>

<ul id="coolSiteList">
    <li><a href="http://debka.com">debka.com</a></li>
    <li><a href="http://zope.org">zope.org</a></li>
    <li><a href="http://www.brownhen.com/domref/dom_shortTOC.html">Gecko
DOM Reference (fairly standards-compliant)</a></li>
    <li><a href="http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/dhtml_reference_entry.asp">MS
DOM Reference (not standards-compliant)</a></li>
    
</ul>

<form>
<input type="button" value="Toggle Rant Color" 
    onclick="toggleDivColor(document.getElementById('existingDiv'))">

<input type="button" value="Insert Link to Cool Site" 
    onclick="addCoolSiteLink(document.getElementById('coolSiteList'),
prompt('Enter the URL of a cool site:', 'http://python.org'))">
</form>

</body>

</html>



More information about the Python-list mailing list