The Python HTMLgen Module
In my next example, I'll show you how a data stream can be processed to produce a series of documents that are interlinked. The script in Listing 3 creates a set of two documents summarizing all the Red Hat packages installed on a Linux system. The resulting HTML page is shown in Figure 3. An index document summarizes the RPM major groups, and a second main document summarizes the RPMs in each group. A link for each group in the index document jumps directly to each group's entries in the main document.
The HTML is generated from the output of the following rpm command:
rpm -q -a --queryformat \
'%{group} %{name} %{summary}\n'
The output typically looks like this:
System/Base sh-utils GNU shell utilities. Browser/WWW lynx tty WWW browser Programming/Tools make GNU Make. System/Library xpm X11 Pixmap Library System/Shell pdksh Public Domain Korn ShellI read this output into a Python list and pre-process it by sorting it into alphabetic order.
I produce two documents, an index document (idoc), and a main document (mdoc), using HTMLgen's SeriesDocument to give both documents the same look-and-feel. By using a SeriesDocument, I can configure standard document headers, footers and navigation buttons via an rcfile and other optional arguments.
The index document (idoc) has only one HTMLgen component: an HTML list of RPM groups. I've used the HTMLgen.List columns option to create a multi-column list:
ilist = HTMLgen.List(style="compact", columns=3) idoc.append(ilist)
The for loop processes each line from the rpm command and generates both idoc and mdoc text. Each time the group name changes, I add a new list entry to the ilist:
if group != lastgroup:
lastgroup = group
title = HTMLgen.Text(group)
href = HTMLgen.Href(mainfile+"#"+ group,
title)
index.append(href)
I've wrapped the list text in an HTML-named HREF, linking it back
into mdoc. I've used the name of the main file
and group title to form the HREF link. For example, in the case of
the “Browser/FTP” RPM group, my code would generate the following
HREF link:
<A HREF="rpmlist.html#Browser/FTP">Browser/FTP</A>The main document (mdoc) has a more complex structure. It consists of a series of HTML definition lists, one per RPM group. Each time the group name changes, I generate the named anchor that is the target for the reference generated above:
anchor = HTMLgen.Name(group, title)I append the anchor to mdoc as a new group heading:
mdoc.append(HTMLgen.Heading(2, anchor))For the “Browser/FTP” group, this would generate the following HTML:
<H2><A NAME="Browser/FTP">Browser/FTP</A></H2>Once the group heading has been appended, I start a list of RPMs in the group:
grplist = HTMLgen.DefinitionList()Once a new group list has been started, my for loop will keep appending RPM summaries to the mdoc until the next change in group name occurs:
grplist.append( (HTMLgen.Text(name),HTMLgen.Text(summary)))When the entire rpmlist has been processed, I generate the two documents you see in Figure 3.
In this example, I simultaneously generated two simple documents and linked one to the other. This example could easily be extended to provide further links to individual documents for each RPM, and from each RPM to the RPMs it depends on.
I've only scratched the surface of what's possible with HTMLgen and Python. I haven't covered the HTMLgen objects for HTML Forms, Image Maps, Nested Tables, Frames, or Netscape Scripts. I also haven't made use of Python's object-oriented nature. For example, I could have sub-classed some of the HTMLgen objects to customize them for specifics of each application. I haven't discussed the Python module for CGI handling. You can read more about these topics by pointing your browser at some of the references accompanying the article (see Resources).
If you're trying to get started with HTMLgen, the HTMLtest.py file distributed with HTMLgen provides some good examples. The HTMLgen documentation is quite good, although in some cases, more examples would help. I don't think my examples require any particular distribution of Linux, libc or Python. All of them were written using HTMLgen 2.0 with Python 1.4 on Caldera OpenLinux Standard version 1.2.

Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Linux Systems Administrator
- New Products
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Have you tried Boxen? It's a
1 hour 56 min ago - seo services in india
6 hours 28 min ago - For KDE install kio-mtp
6 hours 28 min ago - Evernote is much more...
8 hours 28 min ago - Reply to comment | Linux Journal
17 hours 14 min ago - Dynamic DNS
17 hours 48 min ago - Reply to comment | Linux Journal
18 hours 46 min ago - Reply to comment | Linux Journal
19 hours 37 min ago - Not free anymore
23 hours 38 min ago - Great
1 day 3 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
Re: The Python HTMLgen Module
Hello Michael
I read your article on "http://www.linuxjournal.com/article.php?sid=2986" regarding making barchart using HTMLgen , i was in need of that thing and your article has helped me a lot , but the bar chart also displays the AVERAGE of the data supplied to it and also display average graphically. Is there any way that i can remove the average part and my bar chart will not display the average bar.
Waiting for your reply.... at nitinparikh2000@yahoo.com
Thanks and Regards
Nitin Parikh
Pune , India