The complete data set for this example looks like this:
The date will be used to label each column, and the height of each column is determined by the value. You don't have to use the date - any string will do. You could use names if you wanted. When I created data.txt, I changed its owner so that Apache can access it:
$ sudo chown www-data:www-data /var/www/data.txt
I wrote a script called chart.py and stored it in /usr/lib/cgi-bin.
I made the file executable with this command:
$ sudo chmod +x /usr/lib/cgi-bin/chart.py
This function opens data.txt and reads the data contained in it. A 'for' loop is used to read the file line by line. The strip and split methods are used to remove the trailing newline and break the string into tuples. The tuples are then formatted like this:
Each of these newly formed strings is appended to a larger string that contains the whole table. Note that the last line doesn't need to have a comma at the end of it, so it needs to be handled differently than the other lines. The for loop executes for all but the last line. The 'else' statement after the loop handles the last line, and formats it without a comma at the end.
Each chart has options that can be used to customize the way data is displayed. For different types of chart you may need to to modify the format of the table generated by the script. You can find more at Google Charts.