-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9057_Database Final Project.py
533 lines (460 loc) · 22 KB
/
9057_Database Final Project.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# import the psycopg2 database adapter for PostgreSQL
from psycopg2 import connect, sql
# for the sys.exit() method call
import sys
# for regex expression
import re
# for plotting the data
import matplotlib.pyplot as plt
# import the Pygame libraries
import pygame
from pygame.locals import *
# set the DB name, table, and table data to 'None'
db_name = "DST2final_project" #need to modify based on the name of your database
# initialize the variables
# below variable are for query 1
country = '' # store the input country
year_month = '' # store the input year/month
province = '' # store the input province
query1_check = [] # store the output of check_country_province function
query1_return = [] # store the sql query return
# below variables are for query 2
n_th_place = 0 # store the input n_th_place
which_month = '' # store the input month
highest_rate_country = [] # store the country with the highest diagnosis rate
current_rate = 0 # use to find the highest rate
query2_return = [] # store the return of query 2
#setting for postgreSQL
#change these globals (user name and user password) to match your settings
user_name = "postgres" #the username for accessing your postgreSQL
user_pass = "123456" #the password for accessing your postgreSQL
# create a class for the buttons and labels
class Button():
# empty list for button registry
registry = []
# selected button (will have outline rect)
selected = None
# pygame RGBA colors
white = (255, 255, 255, 255)
black = (0, 0, 0, 255)
red = (255, 0, 0, 255)
green = (50, 205, 50, 255)
light_blue = (240, 248, 255, 255)
# default font color for buttons/labels is white
def __init__(self, name, loc, color=black):
# add button to registry
self.registry.append(self)
# paramater attributes
self.name = name
self.loc = loc
self.color = color
# text attr for button
self.text = ""
# size of button changes depending on length of text
self.size = (int(len(self.text)*200), 200)
# font.render(text, antialias, color, background=None) -> Surface
self.font = font.render (
self.name + " " + self.text, # display text
True, # antialias on
self.color, # font color
self.light_blue # background color
)
# rect for button
self.rect = self.font.get_rect()
self.rect.x = loc[0]
self.rect.y = loc[1]
# function that connects to Postgres
def connect_postgres(db):
# connect to PostgreSQL
print ("\nconnecting to PostgreSQL")
try:
conn = connect (
dbname = db,
user = user_name,
host = "localhost",
password = user_pass
)
except Exception as err:
print ("PostgreSQL Connect() ERROR:", err)
conn = None
# return the connection object
return conn
# function that returns movie with queried rating_button
def return_rate(conn):
if which_month == None or which_month == '':
return []
SQLquery='SELECT return_rate(\''+str(which_month)+"'"+')'+';'
print(SQLquery)
# instantiate a new cursor object
cursor = conn.cursor()
# (use sql.SQL() to prevent SQL injection attack)
sql_object = sql.SQL(
# pass SQL statement to sql.SQL() method
SQLquery
)
try:
# use the execute() method to put table data into cursor obj
cursor.execute( sql_object )
# use the fetchall() method to return a list of all the data
rate_return = cursor.fetchall()
# close cursor objects to avoid memory leaks
cursor.close()
except Exception as err:
# print psycopg2 error and set table data to None
print ("PostgreSQL psycopg2 cursor.execute() ERROR:", err)
rate_return = None
return rate_return
# function that returns movie with queried rating_button
def return_country(conn):
if country == None or country== '' or year_month == None or year_month == '':
return []
SQLquery='SELECT return_country(\''+str(country)+"'"+','+"'"+str(year_month)+"'"+')'+';'
print(SQLquery)
# instantiate a new cursor object
cursor = conn.cursor()
# (use sql.SQL() to prevent SQL injection attack)
sql_object = sql.SQL(
# pass SQL statement to sql.SQL() method
SQLquery
)
try:
# use the execute() method to put table data into cursor obj
cursor.execute( sql_object )
# use the fetchall() method to return a list of all the data
country_return = cursor.fetchall()
# close cursor objects to avoid memory leaks
cursor.close()
except Exception as err:
# print psycopg2 error and set table data to None
print ("PostgreSQL psycopg2 cursor.execute() ERROR:", err)
country_return = None
return country_return
def check_country_province (conn):
if country == None or country== '' or province == None or province == '':
return []
SQLquery='SELECT country_name from country_province where country_name='+"'"+str(country)+"'"+'and province_name='+"'"+str(province)+"'"+';'
print(SQLquery)
# instantiate a new cursor object
cursor = conn.cursor()
# (use sql.SQL() to prevent SQL injection attack)
sql_object = sql.SQL(
# pass SQL statement to sql.SQL() method
SQLquery
)
try:
# use the execute() method to put table data into cursor obj
cursor.execute( sql_object )
# use the fetchall() method to return a list of all the data
check_return = cursor.fetchall()
# close cursor objects to avoid memory leaks
cursor.close()
except Exception as err:
# print psycopg2 error and set table data to None
print ("PostgreSQL psycopg2 cursor.execute() ERROR:", err)
check_return = None
return check_return
def return_province(conn):
if country == None or country== '' or year_month == None or year_month == '':
return []
SQLquery='SELECT return_province(\''+str(province)+"'"+','+"'"+str(year_month)+"'"+')'+';'
print(SQLquery)
# instantiate a new cursor object
cursor = conn.cursor()
# (use sql.SQL() to prevent SQL injection attack)
sql_object = sql.SQL(
# pass SQL statement to sql.SQL() method
SQLquery
)
try:
# use the execute() method to put table data into cursor obj
cursor.execute( sql_object )
# use the fetchall() method to return a list of all the data
province_return = cursor.fetchall()
# close cursor objects to avoid memory leaks
cursor.close()
except Exception as err:
# print psycopg2 error and set table data to None
print ("PostgreSQL psycopg2 cursor.execute() ERROR:", err)
province_return = None
return province_return
def plot(results):
temp = []
date = []
number = []
# store date and confirmed cases in two lists seperately
for i in range(0,len(results)):
temp = re.findall(r'[0-9-]+',query1_return[i][0])
date.append(temp[0])
number.append(int(temp[1]))
# set the plot
fig, ax = plt.subplots()
ax.ticklabel_format(useOffset=False, style='plain')
# avoid scientific counting in the plot
# references: https://www.imooc.com/wenda/detail/604644
ax.plot(date,number)
"""
PYGAME STARTS HERE
"""
# initialize the pygame window
pygame.init()
pygame.display.set_mode((2000, 1000))#size of the UI,can add picture
mode = 0
# change the caption/title for the Pygame app
pygame.display.set_caption("9057_DST2 Final Project", "9057_DST2 Final Project")
# get the OS screen/monitor resolution
max_width = pygame.display.Info().current_w
max_height = pygame.display.Info().current_h
# create a pygame resizable screen
screen = pygame.display.set_mode(
(int(max_width*0.55), int(max_height*0.6)),
HWSURFACE | DOUBLEBUF| RESIZABLE
)
# calculate an int for the font size
font_size = int(max_width / 100)
try:
font = pygame.font.SysFont('Calibri', font_size)
except Exception as err:
print ("pygame.font ERROR:", err)
font = pygame.font.SysFont('Arial', font_size)
# create buttons for PostgreSQL database and table
query1_button_country = Button("country", (10, 10))
query1_button_year_month = Button("year/month", (10, 70))
query1_button_province = Button("province",(10,40))
query2_button_month = Button("month", (510, 10))
query2_button_no = Button ("n th place",(510,40))
#pygame.draw.line( screen, Button.white, (500,1), (500,600), 10 ) #error
# default Postgres connection is 'None'
connection = None
# begin the pygame loop
app_running = True
while app_running == True:
# reset the screen (set the color of the screen)
screen.fill( Button.light_blue )
# set the clock FPS for app
clock = pygame.time.Clock()
# iterate over the pygame events
for event in pygame.event.get():
# user clicks the quit button on app window
if event.type == QUIT:
app_running = False
pygame.display.quit()
pygame.quit() # close UI, quit the programme
sys.exit()
quit()
# user presses a key on keyboard
if event.type == KEYDOWN:
if Button.selected != None: #if there is a selected botton
# get the selected button
b = Button.selected
# user presses the return key
if event.key == K_RETURN:
# clear all input and output of the previous query
query1_return = []
query2_return = []
query1_check = []
country = ''
year_month = ''
province = ''
n_th_place = 0
# if the selected button is the query 1's and no province information
if ("country" in b.name or "year/month" in b.name or "province" in b.name) and (query1_button_province.text == '' or query1_button_province.text == None):
country = query1_button_country.text
year_month = query1_button_year_month.text
connection = connect_postgres( db_name )
query1_return = return_country( connection )
# combine 3 records into one line, avoid words overflow the screen
query1_return_adj = []
leng = len(query1_return)
temp = ''
for i in range(1,leng+1):
if i%3 == 0:
temp = temp +' '+ query1_return[i-1][0]
query1_return_adj.append(temp)
temp = ''
else:
temp = temp +' '+ query1_return[i-1][0]
query1_return_adj.append(temp)
plot(query1_return)
# if the selected button is the query 1's and has province information
elif ("country" in b.name or "year/month" in b.name or "province" in b.name) and (query1_button_province.text != '' or query1_button_province.text != None):
country = query1_button_country.text
year_month = query1_button_year_month.text
province = query1_button_province.text
connection = connect_postgres( db_name )
query1_check = check_country_province (connection)
query1_return = return_province( connection )
# combine 3 records into one line, avoid words overflow the screen
query1_return_adj = []
leng = len(query1_return)
temp = ''
for i in range(1,leng+1):
if i%3 == 0:
temp = temp +' '+ query1_return[i-1][0]
query1_return_adj.append(temp)
temp = ''
else:
temp = temp +' '+ query1_return[i-1][0]
query1_return_adj.append(temp)
plot(query1_return)
# if the selected button is the query 2's
elif "month" in b.name or "n th place" in b.name:
which_month = query2_button_month.text
if (len(query2_button_no.text) == 1 and query2_button_no.text >= '1' and query2_button_no.text <= '9') or (query2_button_no.text == '10'):
n_th_place = int(query2_button_no.text)
connection = connect_postgres( db_name )
query2_return = return_rate( connection )
print(country)
print(year_month)
print(province)
print(which_month)
else:
# get the key pressed
key_press = pygame.key.get_pressed()
# iterate over the keypresses
for keys in range(255):
if key_press[keys]:
if keys == 8: # backspace
b.text = b.text[:-1]
else:
# convert key to unicode string
b.text += event.unicode
print ("KEYPRESS:", event.unicode)
# append the button text to button font object
b.font = font.render(b.name + " " + b.text, True, Button.black, Button.light_blue)
# check for mouse button down events
if event.type == MOUSEBUTTONDOWN and event.button == 1:
print ("\nMOUSE CLICK:", event)
# iterate over the button registry list
for b in Button.registry:
# check if the mouse click collided with button
if b.rect.collidepoint(event.pos) == 1:
# store button object under selected attr
Button.selected = b
# iterate over the button registry list
for b in Button.registry:
# blit the button's font to screen
screen.blit(b.font, b.rect)
# check if the button has been clicked by user
if Button.selected == b:
# blit an outline around button if selected
rect_pos = (b.rect.x-5, b.rect.y-5, b.rect.width+4, b.rect.height+10) # change "+10" to "+4" to avoid the outline blot out the input
pygame.draw.rect(screen, Button.black, rect_pos, 3) # width 3 pixels
# if does not enter any query
if Button.selected == None:
blit_text = "Please select a query. "
conn_msg = font.render(blit_text, True, Button.green, Button.light_blue)
screen.blit(conn_msg, (10, 200))
# if enter query 1
if Button.selected == query1_button_country or Button.selected == query1_button_year_month or Button.selected == query1_button_province :
if country == '' and year_month == '':
# blit instruction messages
blit_text = "Type at least country and year_month into the fields and press 'Return'."
conn_msg = font.render(blit_text, True, Button.green, Button.light_blue)
screen.blit(conn_msg, (10, 200))
else:
# connection is valid, but actor doesn't exist
if connection != None:
if query1_return == [] and ((int(year_month) < 1) or (int(year_month) > 12 and int(year_month) != 2020)):
blit_text = "Please type the correct month (1-12) or year(2020). "
color = Button.red
# blit the message to the pygame screen
conn_msg = font.render(blit_text, True, color, Button.light_blue)
screen.blit(conn_msg, (10, 140))
# connection is valid, but rating doesn't exist
if query1_return == [] and (not((int(year_month) < 1) or (int(year_month) > 12 and int(year_month) != 2020))):
blit_text = "The PostgreSQL table does not have the record with this country/province. Please check the country_province.csv"
color = Button.red
# blit the message to the pygame screen
conn_msg = font.render(blit_text, True, color, Button.light_blue)
screen.blit(conn_msg, (10, 120))
# enumerate() the actor first name data if PostgreSQL API call successful
if country != '' and year_month != '' and province == '' and query1_return != []:
# enumerate the list of tuple rows
for num, row in enumerate(query1_return_adj):
# blit the table data to Pygame window
blit_text = str(row).encode("utf-8", "ignore")
table_font = font.render(blit_text, True, Button.black, Button.light_blue)
screen.blit(table_font, (10, 190 + int(num*50)))
# plot the data
plt.ylabel('Confirmed Cases')
plt.xticks(rotation=-50)
plt.title('Comfirmed COVID-19 cases in '+country+' in '+year_month)
# figure saved in the same directory where the python script is
plt.savefig('Comfirmed COVID-19 cases in '+country+' in '+year_month,type='png')
img = pygame.image.load('Comfirmed COVID-19 cases in '+country+' in '+year_month+".png").convert()
screen.blit(img,(750, 250))
if country != '' and year_month != '' and province != '' and query1_check == []:
blit_text = 'The country and province do not match. '
color = Button.red
conn_msg = font.render(blit_text, True, color, Button.light_blue)
screen.blit(conn_msg, (10, 100))
elif country != '' and year_month != '' and province != '' and query1_check != []:
# enumerate the list of tuple rows
for num, row in enumerate(query1_return_adj):
# blit the table data to Pygame window
blit_text = str(row).encode("utf-8", "ignore")
table_font = font.render(blit_text, True, Button.black, Button.light_blue)
screen.blit(table_font, (10, 190 + int(num*50)))
# plot the data
plt.ylabel('Confirmed Cases')
plt.xticks(rotation=-50)
plt.title('Comfirmed COVID-19 cases in '+province+' in '+year_month)
# figure saved in the same directory where the python script is
plt.savefig('Comfirmed COVID-19 cases in '+province+' in '+year_month,type='png')
img = pygame.image.load('Comfirmed COVID-19 cases in '+province+' in '+year_month+".png").convert()
screen.blit(img,(750, 250))
# connection is invalid
elif connection == None:
blit_text = "PostgreSQL connection is invalid."
color = Button.red
# blit the message to the pygame screen
conn_msg = font.render(blit_text, True, color, Button.light_blue)
screen.blit(conn_msg, (10, 140))
# enter query 2
if Button.selected == query2_button_month or Button.selected == query2_button_no:
if query2_return != [] and n_th_place != 0:
all_number = []
for i in range (0,len(query2_return)):
temp1 = re.findall(r'\d+',query2_return[i][0])
all_number.append(int(temp1[0]))
current_rate = 0
j=0
previous_rate = 100000000
# find the n th highest rate, more explanation in the documentation
while j < n_th_place:
current_rate = 0
highest_rate_country = []
i = 0
while i < len(all_number)-1:
if all_number[i] == 0:
i = i + 2
elif all_number[i] != 0:
temp_rate = (all_number[i+1]-all_number[i])/all_number[i]
if temp_rate > current_rate and temp_rate < previous_rate:
highest_rate_country = []
current_rate = temp_rate
temp2 = re.findall(r'[a-zA-Z\s]+',query2_return[i][0])
highest_rate_country.append(temp2[0])
if temp_rate == current_rate:
temp2 = re.findall(r'[a-zA-Z\s]+',query2_return[i][0])
highest_rate_country.append(temp2[0])
i = i + 2
previous_rate = current_rate
j = j + 1
blit_text = str(highest_rate_country[0]).encode("utf-8", "ignore")
table_font = font.render(blit_text, True, Button.black, Button.light_blue)
screen.blit(table_font, (10, 250 + int(1*50)))
blit_text = str(current_rate).encode("utf-8", "ignore")
table_font = font.render(blit_text, True, Button.black, Button.light_blue)
screen.blit(table_font, (10, 250 + int(2*50)))
else:
blit_text = 'Please type in valid month (2-12) and n th place (1-10) for diagnosis rate. '
color = Button.red
conn_msg = font.render(blit_text, True, color, Button.light_blue)
screen.blit(conn_msg, (10, 100))
clock.tick(200)
# use the flip() method to display text on surface
pygame.display.flip()
pygame.display.update()