rendered paste body
########## 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 = '<html><head><title>Popularity database</title></head>' \
+ '<link rel="stylesheet" href="popstyle.css"><body>' \
+ '<address> Popularity database - Home</address>'
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 + '<p><a href="' + category \
+ '.html">' + category + '</a></p>'
html_code = '<html><head><title>Popularity database - ' + category \
+ '</title></head>' \
+ '<link rel="stylesheet" href="popstyle.css"><body>' \
+ '<address>Popularity database - ' + category \
+ ' / <a href="index.html">Home</a></address>'
count_html_code = ''
for row in rows:
if row[1] == 1:
html_code = html_code + '<s1> ' + row[0] + '</s1>'
count_html_code = count_html_code + '<s1> ' + str(row[1]) \
+ '</s1>'
elif row[1] == 2:
html_code = html_code + ' <s2> ' + row[0] + '</s2>'
count_html_code = count_html_code + '<s2> ' + str(row[1]) \
+ '</s2>'
elif row[1] > 2:
if row[1] < 5:
html_code = html_code + '<s3> ' + row[0] + '</s3>'
count_html_code = count_html_code + '<s3> ' + str(row[1]) \
+ '</s3>'
elif row[1] < 10:
html_code = html_code + '<s4> ' + row[0] + '</s4>'
count_html_code = count_html_code + '<s4> ' + str(row[1]) \
+ '</s4>'
elif row[1] < 20:
html_code = html_code + '<s5> ' + row[0] + '</s5>'
count_html_code = count_html_code + '<s5> ' + str(row[1]) \
+ '</s5>'
else:
html_code = html_code + '<s6> ' + row[0] + '</s6>'
count_html_code = count_html_code + '<s6> ' + str(row[1]) \
+ '</s6>'
html_code = html_code + '<p>' + count_html_code + '</p>' \
+ '</body></html>'
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 #####################################