-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathapp.py
136 lines (103 loc) · 4 KB
/
app.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
from flask import Flask, render_template, request
from pymysql import connections
import os
import random
import argparse
app = Flask(__name__)
DBHOST = os.environ.get("DBHOST") or "localhost"
DBUSER = os.environ.get("DBUSER") or "root"
DBPWD = os.environ.get("DBPWD") or "passwors"
DATABASE = os.environ.get("DATABASE") or "employees"
COLOR_FROM_ENV = os.environ.get('APP_COLOR') or "lime"
DBPORT = int(os.environ.get("DBPORT"))
# Create a connection to the MySQL database
db_conn = connections.Connection(
host= DBHOST,
port=DBPORT,
user= DBUSER,
password= DBPWD,
db= DATABASE
)
output = {}
table = 'employee';
# Define the supported color codes
color_codes = {
"red": "#e74c3c",
"green": "#16a085",
"blue": "#89CFF0",
"blue2": "#30336b",
"pink": "#f4c2c2",
"darkblue": "#130f40",
"lime": "#C1FF9C",
}
# Create a string of supported colors
SUPPORTED_COLORS = ",".join(color_codes.keys())
# Generate a random color
COLOR = random.choice(["red", "green", "blue", "blue2", "darkblue", "pink", "lime"])
@app.route("/", methods=['GET', 'POST'])
def home():
return render_template('addemp.html', color=color_codes[COLOR])
@app.route("/about", methods=['GET','POST'])
def about():
return render_template('about.html', color=color_codes[COLOR])
@app.route("/addemp", methods=['POST'])
def AddEmp():
emp_id = request.form['emp_id']
first_name = request.form['first_name']
last_name = request.form['last_name']
primary_skill = request.form['primary_skill']
location = request.form['location']
insert_sql = "INSERT INTO employee VALUES (%s, %s, %s, %s, %s)"
cursor = db_conn.cursor()
try:
cursor.execute(insert_sql,(emp_id, first_name, last_name, primary_skill, location))
db_conn.commit()
emp_name = "" + first_name + " " + last_name
finally:
cursor.close()
print("all modification done...")
return render_template('addempoutput.html', name=emp_name, color=color_codes[COLOR])
@app.route("/getemp", methods=['GET', 'POST'])
def GetEmp():
return render_template("getemp.html", color=color_codes[COLOR])
@app.route("/fetchdata", methods=['GET','POST'])
def FetchData():
emp_id = request.form['emp_id']
output = {}
select_sql = "SELECT emp_id, first_name, last_name, primary_skill, location from employee where emp_id=%s"
cursor = db_conn.cursor()
try:
cursor.execute(select_sql,(emp_id))
result = cursor.fetchone()
# Add No Employee found form
output["emp_id"] = result[0]
output["first_name"] = result[1]
output["last_name"] = result[2]
output["primary_skills"] = result[3]
output["location"] = result[4]
except Exception as e:
print(e)
finally:
cursor.close()
return render_template("getempoutput.html", id=output["emp_id"], fname=output["first_name"],
lname=output["last_name"], interest=output["primary_skills"], location=output["location"], color=color_codes[COLOR])
if __name__ == '__main__':
# Check for Command Line Parameters for color
parser = argparse.ArgumentParser()
parser.add_argument('--color', required=False)
args = parser.parse_args()
if args.color:
print("Color from command line argument =" + args.color)
COLOR = args.color
if COLOR_FROM_ENV:
print("A color was set through environment variable -" + COLOR_FROM_ENV + ". However, color from command line argument takes precendence.")
elif COLOR_FROM_ENV:
print("No Command line argument. Color from environment variable =" + COLOR_FROM_ENV)
COLOR = COLOR_FROM_ENV
else:
print("No command line argument or environment variable. Picking a Random Color =" + COLOR)
# Check if input color is a supported one
if COLOR not in color_codes:
print("Color not supported. Received '" + COLOR + "' expected one of " + SUPPORTED_COLORS)
exit(1)
app.run(host='0.0.0.0',port=8080,debug=True)