To build a little D3-enabled graph, we could follow the same procedure we used in Chapter 9, Stack Overflow Project, in which we made a PHP script that queries the database, and then our JavaScript uses the results as the live input to a bar graph. Alternatively, we could generate a CSV file with Python and let D3 generate its graph from those results. Since we already performed the PHP method in the previous chapter, let's use the CSV file method here, just for variety. This is also a good excuse to continue on with Python in this chapter, since this is already the language we have been using.
The following script connects to the database, selects out the top 15 most-used URLs and their counts, and writes the entire thing to a CSV file:
import csv import MySQLdb # Open database connection db = MySQLdb.connect(host="localhost", user="username", passwd="password", db="ferguson", use_unicode=True, charset="utf8") cursor = db.cursor()...