Hello!
Are you ready for instant, easy, super dependable HTML generation?! Well get ready because it is here.
I am now releasing version 2.4 of htmlgen. This is now a fully featured toolkit that takes away many of the insufficiencies and annoyances from version 1.
Here are some of the new features:
- Now supports Python 2 and 3
- A fully featured smart document that organizes your HTML tags for you! Now there is no need to create things like the “body” and “head” tags. All you have to do is add the document to one of either SmartDocument or SimpleDocument classes, and your tags will be organized into place.
- New html parser module allows you to parse HTML. Uses Python’s build in HTML parser to parse html and put it into the new DOMDocument class. (Yes I know the built in Parser sucks…but it will hopefully soon support things like Beautiful Soup (!py3k))
- Introduces the DOMDocument class. This class creates an interface much like that of the javascript document object.
- Introduces the SimpleDocument class. (Basically a SmartDocument without a head tag)
- Two new utility functions getfromname and getfromclass make retrieving elements based on their tagname or class very easy.
- Elements no longer need parents. I did some debugging and such and found that they really don’t require anything in the constructor.
Those are just a few of the great benefits of htmlgen.
Here are some code samples:
from htmlgen.tags import (Document, Head, Body, Title, Style, H2, Heading, HR,
Table, TableRow, TableData, LineBreak, Script, Select, Option, Span)
doc = Document()
x = 10
y = 10
head = Head(doc)
head.add()
body = Body(doc)
body.add()
Title(head, 'The row and table page!').add()
Style(head, 'body {font-family: "Trebuchet MS", Helvetica, sans-serif}').add()
H2(body, 'Welcome to the test page; there is more to see on the bottom!').add()
Heading(body, 3, 'Multiplication Table').add()
HR(body).add()
table = Table(body, cellpadding=5, border=2)
table.add()
for ir in range(y):
row = TableRow(table)
row.add()
for ic in range(x):
TableData(row, '%s'%((ir+1)*(ic+1))).add()
HR(body).add()
LineBreak(body).add()
script = """
function onChange() {
var val = document.getElementById('dropdown').value;
document.getElementById('output').innerHTML = ' Current Value = '+val;
alert('You changed me to '+val+'!');
}
"""
Script(body, script, type="text/javascript").add()
dropdown = Select(body, id='dropdown', onchange='onChange()', onload='onChange()')
dropdown.add()
for car in ['volvo', 'saab', 'ferari', 'audi']:
Option(dropdown, car.capitalize(), value=car).add()
Span(body, 'Change the dropdown to see what happens!', id='output').add()
doc.print_final()
Next is an example of how htmlgen looks now. (Much more orgainized)
from htmlgen.tags import (SmartDocument, Head, Body, Title, Style, H2, Heading, HR,
Table, TableRow, TableData, LineBreak, Script, Select, Option, Span)
doc = SmartDocument()
x = 10
y = 10
# no need for head or body tags. It is all implicitly created
Title(doc, 'The row and table page!').add()
H2(doc, 'Welcome to the test page; there is more to see on the bottom!').add()
Heading(doc, 3, 'Multiplication Table').add()
HR(doc).add()
table = Table(doc, cellpadding=5, border=2)
table.add()
for ir in range(y):
row = TableRow(table)
row.add()
for ic in range(x):
TableData(row, '%s'%((ir+1)*(ic+1))).add()
HR(doc).add()
LineBreak(doc).add()
script = """
function onChange() {
var val = document.getElementById('dropdown').value;
document.getElementById('output').innerHTML = ' Current Value = '+val;
alert('You changed me to '+val+'!');
}
"""
Script(doc, script, type="text/javascript").add()
dropdown = Select(doc, id='dropdown', onchange='onChange()', onload='onChange()')
dropdown.add()
for car in ['volvo', 'saab', 'ferari', 'audi']:
Option(dropdown, car.capitalize(), value=car).add()
Span(doc, 'Change the dropdown to see what happens!', id='output').add()
# this style tag is created and added down here, but it will still render in the
# head tag
Style(doc, 'body {font-family: "Trebuchet MS", Helvetica, sans-serif}').add()
doc.print_final() # same methods as Document
Not only do you save tons and tons of lines, but you also get to just forget about who your parenting an element to when you add it. You can add an element at any time you want, and never have to worry about it going to the wrong place.
Downloads:
You can download htmlgen at http://download.sunjay-varma.com/ghi/htmlgen.zip
I will also hopefully soon have a working sourceforge, or some other such page coming up.
I hope you enjoy the excitement of effortlessly creating perfectly valid, smart HTML…
-Sunjay Varma