Brython - Python in the browser

Stefan Behnel stefan_ml at behnel.de
Fri Dec 21 08:34:01 EST 2012


Duncan Booth, 21.12.2012 14:14:
> Pierre Quentel wrote:
>>> If that's your intention, then instead of coming up with something
>>> totally new, unpythonic and ugly, why not take the normal Python
>>> route and implement a subset of the ElementTree API?
>>
>> Because the tree implementation in ElementTree or other tree modules
>> in Python require a lot of typing and parenthesis 
>>
>> To produce the HTML code
>>
>> <DIV>hello <B>world</B></DIV>
>>
>> these modules require writing something like 
>>
>> div = Tag('DIV')
>> div.appendChild(TextNode('hello '))
>> b = Tag('B')
>> b.appendChild(TextNode('world'))
>> div.appendChild(b)
>> doc.appendChild(div)
> 
> Or you can do something like this:
> 
> >>> from lxml.html.builder import *
> >>> snippet = DIV("Hello ", B("world"))
> >>> etree.tostring(snippet)
> '<div>Hello <b>world</b></div>'

For which there even happens to be an ElementTree implementation available:

http://svn.effbot.org/public/stuff/sandbox/elementlib/builder.py

(It's not like I made this up ...)

Stefan





More information about the Python-list mailing list