########## BEGIN DESCRIPTION ############################################ ## ## Popularity Cloud. ## This program queries a MySQL database to extract the frequencies (count) ## of each value in a series of categories. ## ## The extracted data is output to a HTML "popularity" cloud, which displays ## values in a category which have the same frequency in the same colour ## and text size, and those which are more frequent in a larger text size. ## Each frequency is shown in a distinct text size. ## ########## END DESCRIPTION ############################################## ########## BEGIN PAIR DETAILS ##################################### ## ## Student Name: *** WRITE YOUR NAME HERE*** ## ## Student Number: *** WRITE YOUR STUDENT NUMBER HERE *** ## ## Student Name: *** WRITE YOUR NAME HERE*** ## ## Student Number: *** WRITE YOUR STUDENT NUMBER HERE *** ## ########## END PAIR DETAILS ####################################### ########## BEGIN MARKER'S FEEDBACK ################################### ## ## ########## END MARKER'S FEEDBACK ##################################### ########## BEGIN Popularity Code ########################### ## ## ## Program code: ## ## *** PUT YOUR PROGRAM CODE BELOW *** from MySQLdb import * def create_clouds(categories): ''' categories - a list of strings containing the names of each of the categories to create HTML files for ''' head = '[Create clouds]' print head, 'Please make sure you enter the correct details.' mysql_host = raw_input(head + ' MySQL host:') mysql_port = raw_input(head + ' MySQL port:') mysql_user = raw_input(head + ' Username:') mysql_pass = raw_input(head + ' Password:') mysql_db = raw_input(head + ' Database:') connection = connect(host = mysql_host, user = mysql_user, \ passwd = mysql_pass, port = int(mysql_port), \ db = mysql_db) cursor = connection.cursor() index_html_code = 'Popularity database' \ + '' \ + '
Popularity database - Home
' write_css() for category in categories: sql_query = 'SELECT A1.value Value, COUNT(A1.value)' + '"Frequency"' \ + ', A1.category Category' + ' FROM popularity A1' + ' WHERE Category' \ ' = ' + '"' + category + '"' + ' GROUP BY A1.value' cursor.execute(sql_query) rows = cursor.fetchall() index_html_code = index_html_code + '

' + category + '

' html_code = 'Popularity database - ' + category \ + '' \ + '' \ + '
Popularity database - ' + category \ + ' / Home
' count_html_code = '' for row in rows: if row[1] == 1: html_code = html_code + ' ' + row[0] + '' count_html_code = count_html_code + ' ' + str(row[1]) \ + '' elif row[1] == 2: html_code = html_code + ' ' + row[0] + '' count_html_code = count_html_code + ' ' + str(row[1]) \ + '' elif row[1] > 2: if row[1] < 5: html_code = html_code + ' ' + row[0] + '' count_html_code = count_html_code + ' ' + str(row[1]) \ + '' elif row[1] < 10: html_code = html_code + ' ' + row[0] + '' count_html_code = count_html_code + ' ' + str(row[1]) \ + '' elif row[1] < 20: html_code = html_code + ' ' + row[0] + '' count_html_code = count_html_code + ' ' + str(row[1]) \ + '' else: html_code = html_code + ' ' + row[0] + '' count_html_code = count_html_code + ' ' + str(row[1]) \ + '' html_code = html_code + '

' + count_html_code + '

' \ + '' open_file = open(category + '.html', 'w') open_file.write(html_code) open_file.close() cursor.close() connection.close() open_file = open('index.html', 'w') open_file.write(index_html_code) open_file.close() print head, 'Files were created.' def write_css(): css_code = ''' address { margin-top: 1em; padding-top: 1em; border-bottom: thin dotted; border-color:#94CBF8; color:FFFFFF; font-size:12pt; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; } links { color:FFFFFF; font-size:14; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; } s1 { color:#C6C6C6; font-size:pt; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; } s2 { color:#C6C6C6; font-size:12pt; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; font-weight:bold; } s3 { color:#D9ECFC; font-size:12pt; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; } s4 { color:#C9E4FC; font-size:15pt; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; font-weight:bold; } s5 { color:#94CBF8; font-size:18pt; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; font-weight:bold; } s6 { color:#FFFFFF; font-size:24pt; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; font-weight:bold; } body { background-color: #313131; } a:link { color:#C9E4FC; text-decoration:none; font-size:12pt; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; } a:visited { color:#C9E4FC; text-decoration:none; font-size:12pt; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; } a:hover { color:#94CBF8; text-decoration:none; font-size:12pt; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; } a:active { color:#FFFFFF; text-decoration:none; font-size:12pt; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; } ''' open_file = open('popstyle.css', 'w') open_file.write(css_code) open_file.close() ## ########## END Popularity Cloud Code ############################# ########## BEGIN AUTOMATIC START ################################### ## ## The following code will automatically run the given function ## when the program is "run". Do not change anything in this ## section. If you want to prevent it from running, comment ## out the code below, but ensure that the code is uncommented when ## you submit your program ## # Run the function if this is the main program: if __name__ == '__main__': create_clouds(['actors', 'movies', 'tv', 'sports', 'games', \ 'activities', 'musicians', 'books']) ## ########## END AUTOMATIC START #####################################