-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_leaderboard.py
37 lines (32 loc) · 1.05 KB
/
display_leaderboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/local/bin/python3
from cgitb import enable
enable()
from cgi import FieldStorage, escape
import pymysql as db
print('Content-Type: text/html')
print()
result = ''
try:
connection = db.connect(#login)
cursor = connection.cursor(db.cursors.DictCursor)
cursor.execute("""SELECT candidate_name, total_votes
FROM candidates
ORDER BY total_votes DESC, candidate_name ASC""")
result = '<table><tr><th>Candidate name</th><th>Total votes</th></tr>'
for row in cursor.fetchall():
result += '<tr><td>%s</td><td>%i</td></tr>' % (row['candidate_name'], row['total_votes'])
result += '</table>'
cursor.close()
connection.close()
except db.Error:
result = '<p>Sorry! We are experiencing problems at the moment. Please call back later.</p>'
print("""
<!DOCTYPE html>
<html lang="en">
<head>
<title>Leaderboard</title>
</head>
<body>
%s
</body>
</html>""" % (result))