The Python HTMLgen Module

Bar Charts from HTML Tables

The table facilities have also been extended to create fancy bar charts. Figure 2 shows a bar chart I generated from the output of the Linux ps command. The chart was created by the HTMLgen bar-chart module. The code for is the 20 lines of Python code shown in Listing 2. The original output from ps v looks something like the following:

 555 p1 S 0:01 232 237 1166 664 xx 2.1 -tcsh
1249 p2 S 0:00 424 514 2613 1676 xx 5.4 xv ps

I use the Python operating system module's popen function to return a file input pipe for the output stream from the command:

inpipe = os.popen("ps vax", "r");
I then read in the first line from the input pipe and split it into a list of column names.
colnames = string.split(inpipe.readline())
Now, I create the chart object, and the chart object's datalist object:
chart = barchart.StackedBarChart()
chart.datalist = barchart.DataList()
Datalists can have multiple data segments per bar, which results in a stacked bar chart such as the one in Figure 2. I need to tell the datalist object how many data segments are present by setting the list of segment_names. I decided the bars on my chart will have two segments, one for TSIZ (program text memory size) and one for DSIZ (program data memory size). To accomplish this, I need to copy the two column names from colnames into segment_names. Because lists in Python are numbered from zero, the two colnames I'm interested in are columns 5 (TSIZ) and 6 (DSIZ). I can extract them from the colnames list with a single slicing statement:
chart.datalist.segment_names = colnames[5:7]
data = chart.datalist
The [5:7] notation is a slicing notation. In Python, you can slice single items and ranges of items out of strings, lists and other sequence data types. The notation [low:high] means slice out a new list from low to high minus 1. On the second line, I assign the variable called “data” to the variable “chart.datalist” to shorten the length of the following lines to fit the column width required in Linux Journal.

After initializing the chart, I use a for loop to read the remaining lines from the ps output pipe. I extract the columns I need by using string.split(line) to break the line into a list of columns. I extract the text of each command by taking all the words from column 10 onward and joining them into a new barname string:

barname = string.join(cols[10:], " " )

I use the string module's atoi function to convert the ASCII strings in the numeric fields to integers. The last statement in the loop assembles the data into a tuple:

( barname, tsize, dsize )
A tuple is a Python structure much like a list, except that a tuple is immutable—you cannot insert or delete elements from a tuple. Although the two are similar, their differences lead to quite different implementation efficiencies. Python has both a tuple and a list, because this allows the programmer to choose the one most appropriate to the situation. Many features of Python and its modules are designed to be high-level interfaces to services that are then implemented efficiently in compiled languages such as C. This allows Python to be used for computer graphics programming using OpenGL and for numerical programming using fast numerical libraries.

Back to the example. The last statement in the loop inserts the tuple into the chart's datalist.

data.load_tuple(( barname, tsize, dsize ))

When the last line is processed, the loop terminates and I sort the data in decreasing order of TSIZ:

data.sort(key=colnames[5], direction="decreasing")
After that, I create the final document and save it to a file.
doc = HTMLgen.SimpleDocument(title='Memory')
Loading psv.html into a browser results in the chart seen in Figure 2. By altering the bar chart's parameters, such as the GIFs used for the chart's “atoms”, I can build the chart in different styles.

Figure 3. HTML Page Generated by Listing 3



Comment viewing options


Re: The Python HTMLgen Module

Anonymous's picture

Hello Michael


Waiting for your reply.... at

Thanks and Regards
Nitin Parikh
Pune , India



Learn More

Sponsored by Bit9

Linux Backup and Recovery Webinar



Learn More

Sponsored by Storix