Commit
•
f3928c1
1
Parent(s):
14e6735
Update app.py
Browse files
app.py
CHANGED
@@ -1,126 +1,202 @@
|
|
1 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
2 |
import pandas as pd
|
3 |
-
import numpy as np
|
4 |
-
from sklearn.model_selection import train_test_split
|
5 |
-
from sklearn.ensemble import RandomForestRegressor
|
6 |
-
from sklearn.preprocessing import StandardScaler
|
7 |
import plotly.express as px
|
8 |
-
import plotly.graph_objects as go
|
9 |
|
10 |
-
#
|
11 |
-
def
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
'project_completion': np.random.uniform(60, 100, 1000),
|
20 |
-
'performance_score': np.random.uniform(50, 100, 1000)
|
21 |
-
})
|
22 |
-
return data
|
23 |
|
24 |
-
#
|
25 |
-
def
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
X_train_scaled = scaler.fit_transform(X_train)
|
33 |
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
employee = data[data['employee_id'] == employee_id].iloc[0]
|
53 |
-
|
54 |
-
# Display current employee details
|
55 |
-
st.sidebar.subheader('Current Details')
|
56 |
-
st.sidebar.write(f"Age: {employee['age']}")
|
57 |
-
st.sidebar.write(f"Experience: {employee['experience']} years")
|
58 |
-
st.sidebar.write(f"Training Score: {employee['training_score']:.2f}")
|
59 |
-
st.sidebar.write(f"Project Completion: {employee['project_completion']:.2f}%")
|
60 |
-
|
61 |
-
# Input fields for updating employee details
|
62 |
-
st.sidebar.subheader('Update Details')
|
63 |
-
age = st.sidebar.number_input('Age', min_value=22, max_value=65, value=int(employee['age']))
|
64 |
-
experience = st.sidebar.number_input('Years of Experience', min_value=0, max_value=40, value=int(employee['experience']))
|
65 |
-
training_score = st.sidebar.slider('Training Score', 50.0, 100.0, float(employee['training_score']))
|
66 |
-
project_completion = st.sidebar.slider('Project Completion Rate', 60.0, 100.0, float(employee['project_completion']))
|
67 |
-
|
68 |
-
# Create a dataframe with user input
|
69 |
-
user_input = pd.DataFrame({
|
70 |
-
'age': [age],
|
71 |
-
'experience': [experience],
|
72 |
-
'training_score': [training_score],
|
73 |
-
'project_completion': [project_completion]
|
74 |
-
})
|
75 |
-
|
76 |
-
# Scale the input
|
77 |
-
user_input_scaled = scaler.transform(user_input)
|
78 |
-
|
79 |
-
# Make prediction
|
80 |
-
prediction = model.predict(user_input_scaled)
|
81 |
-
|
82 |
-
# Display results
|
83 |
-
col1, col2 = st.columns(2)
|
84 |
-
|
85 |
-
with col1:
|
86 |
-
st.subheader('Predicted Performance Score')
|
87 |
-
fig = go.Figure(go.Indicator(
|
88 |
-
mode = "gauge+number",
|
89 |
-
value = prediction[0],
|
90 |
-
domain = {'x': [0, 1], 'y': [0, 1]},
|
91 |
-
title = {'text': "Performance"},
|
92 |
-
gauge = {'axis': {'range': [None, 100]},
|
93 |
-
'steps' : [
|
94 |
-
{'range': [0, 60], 'color': "lightgray"},
|
95 |
-
{'range': [60, 80], 'color': "gray"},
|
96 |
-
{'range': [80, 100], 'color': "darkgray"}],
|
97 |
-
'threshold': {'line': {'color': "red", 'width': 4}, 'thickness': 0.75, 'value': 90}}))
|
98 |
-
st.plotly_chart(fig)
|
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 |
st.plotly_chart(fig)
|
|
|
|
|
|
|
|
|
124 |
|
125 |
-
if __name__ ==
|
126 |
main()
|
|
|
1 |
import streamlit as st
|
2 |
+
import mysql.connector
|
3 |
+
import bcrypt
|
4 |
+
import re
|
5 |
+
import datetime
|
6 |
+
import pytz
|
7 |
import pandas as pd
|
|
|
|
|
|
|
|
|
8 |
import plotly.express as px
|
|
|
9 |
|
10 |
+
# MySQL Connection
|
11 |
+
def get_database_connection():
|
12 |
+
connection = mysql.connector.connect(
|
13 |
+
host="gateway01.ap-southeast-1.prod.aws.tidbcloud.com",
|
14 |
+
port=4000,
|
15 |
+
user="37QUb7dvTn3P6E8.root",
|
16 |
+
password="MBAg14V0HaMdxwX0"
|
17 |
+
)
|
18 |
+
return connection
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
# Initialize database and tables
|
21 |
+
def init_database():
|
22 |
+
connection = get_database_connection()
|
23 |
+
cursor = connection.cursor(buffered=True)
|
24 |
+
|
25 |
+
cursor.execute("CREATE DATABASE IF NOT EXISTS EmployeePerformance")
|
26 |
+
cursor.execute('USE EmployeePerformance')
|
27 |
+
|
28 |
+
# Create User_data table
|
29 |
+
cursor.execute('''CREATE TABLE IF NOT EXISTS User_data (
|
30 |
+
id INT AUTO_INCREMENT PRIMARY KEY,
|
31 |
+
username VARCHAR(50) UNIQUE NOT NULL,
|
32 |
+
password VARCHAR(255) NOT NULL,
|
33 |
+
email VARCHAR(255) UNIQUE NOT NULL,
|
34 |
+
registered_date TIMESTAMP,
|
35 |
+
last_login TIMESTAMP
|
36 |
+
)''')
|
37 |
+
|
38 |
+
# Create Employee_data table
|
39 |
+
cursor.execute('''CREATE TABLE IF NOT EXISTS Employee_data (
|
40 |
+
id INT AUTO_INCREMENT PRIMARY KEY,
|
41 |
+
employee_id VARCHAR(50) UNIQUE NOT NULL,
|
42 |
+
name VARCHAR(100) NOT NULL,
|
43 |
+
age INT,
|
44 |
+
last_performance_score FLOAT,
|
45 |
+
current_performance_score FLOAT,
|
46 |
+
future_advancement TEXT,
|
47 |
+
rank INT
|
48 |
+
)''')
|
49 |
+
|
50 |
+
connection.commit()
|
51 |
+
cursor.close()
|
52 |
+
connection.close()
|
53 |
+
|
54 |
+
# User authentication functions
|
55 |
+
def username_exists(username):
|
56 |
+
connection = get_database_connection()
|
57 |
+
cursor = connection.cursor(buffered=True)
|
58 |
+
cursor.execute('USE EmployeePerformance')
|
59 |
+
cursor.execute("SELECT * FROM User_data WHERE username = %s", (username,))
|
60 |
+
result = cursor.fetchone() is not None
|
61 |
+
cursor.close()
|
62 |
+
connection.close()
|
63 |
+
return result
|
64 |
+
|
65 |
+
def email_exists(email):
|
66 |
+
connection = get_database_connection()
|
67 |
+
cursor = connection.cursor(buffered=True)
|
68 |
+
cursor.execute('USE EmployeePerformance')
|
69 |
+
cursor.execute("SELECT * FROM User_data WHERE email = %s", (email,))
|
70 |
+
result = cursor.fetchone() is not None
|
71 |
+
cursor.close()
|
72 |
+
connection.close()
|
73 |
+
return result
|
74 |
+
|
75 |
+
def is_valid_email(email):
|
76 |
+
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
|
77 |
+
return re.match(pattern, email) is not None
|
78 |
+
|
79 |
+
def create_user(username, password, email):
|
80 |
+
if username_exists(username):
|
81 |
+
return 'username_exists'
|
82 |
+
if email_exists(email):
|
83 |
+
return 'email_exists'
|
84 |
+
|
85 |
+
connection = get_database_connection()
|
86 |
+
cursor = connection.cursor(buffered=True)
|
87 |
+
cursor.execute('USE EmployeePerformance')
|
88 |
+
|
89 |
+
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
90 |
+
registered_date = datetime.datetime.now(pytz.timezone('Asia/Kolkata'))
|
91 |
+
|
92 |
+
cursor.execute(
|
93 |
+
"INSERT INTO User_data (username, password, email, registered_date) VALUES (%s, %s, %s, %s)",
|
94 |
+
(username, hashed_password, email, registered_date)
|
95 |
+
)
|
96 |
+
|
97 |
+
connection.commit()
|
98 |
+
cursor.close()
|
99 |
+
connection.close()
|
100 |
+
return 'success'
|
101 |
+
|
102 |
+
def verify_user(username, password):
|
103 |
+
connection = get_database_connection()
|
104 |
+
cursor = connection.cursor(buffered=True)
|
105 |
+
cursor.execute('USE EmployeePerformance')
|
106 |
+
|
107 |
+
cursor.execute("SELECT password FROM User_data WHERE username = %s", (username,))
|
108 |
+
record = cursor.fetchone()
|
109 |
+
|
110 |
+
if record and bcrypt.checkpw(password.encode('utf-8'), record[0].encode('utf-8')):
|
111 |
+
cursor.execute("UPDATE User_data SET last_login = %s WHERE username = %s",
|
112 |
+
(datetime.datetime.now(pytz.timezone('Asia/Kolkata')), username))
|
113 |
+
connection.commit()
|
114 |
+
cursor.close()
|
115 |
+
connection.close()
|
116 |
+
return True
|
117 |
+
|
118 |
+
cursor.close()
|
119 |
+
connection.close()
|
120 |
+
return False
|
121 |
+
|
122 |
+
# Employee data functions
|
123 |
+
def get_employee_data():
|
124 |
+
connection = get_database_connection()
|
125 |
+
cursor = connection.cursor(buffered=True)
|
126 |
+
cursor.execute('USE EmployeePerformance')
|
127 |
+
|
128 |
+
cursor.execute("SELECT * FROM Employee_data")
|
129 |
+
columns = [col[0] for col in cursor.description]
|
130 |
+
employees = [dict(zip(columns, row)) for row in cursor.fetchall()]
|
131 |
+
|
132 |
+
cursor.close()
|
133 |
+
connection.close()
|
134 |
+
return employees
|
135 |
+
|
136 |
+
# Streamlit app
|
137 |
+
def main():
|
138 |
+
st.set_page_config(page_title="Employee Performance Dashboard", layout="wide")
|
139 |
|
140 |
+
init_database()
|
|
|
141 |
|
142 |
+
if 'logged_in' not in st.session_state:
|
143 |
+
st.session_state.logged_in = False
|
144 |
|
145 |
+
if not st.session_state.logged_in:
|
146 |
+
login_page()
|
147 |
+
else:
|
148 |
+
home_page()
|
149 |
|
150 |
+
def login_page():
|
151 |
+
st.title("Login")
|
152 |
+
|
153 |
+
username = st.text_input("Username")
|
154 |
+
password = st.text_input("Password", type="password")
|
155 |
+
|
156 |
+
if st.button("Login"):
|
157 |
+
if verify_user(username, password):
|
158 |
+
st.session_state.logged_in = True
|
159 |
+
st.session_state.username = username
|
160 |
+
st.experimental_rerun()
|
161 |
+
else:
|
162 |
+
st.error("Invalid username or password")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
|
164 |
+
def home_page():
|
165 |
+
st.title("Employee Performance Dashboard")
|
166 |
+
st.write(f"Welcome, {st.session_state.username}!")
|
167 |
+
|
168 |
+
employees = get_employee_data()
|
169 |
+
|
170 |
+
if not employees:
|
171 |
+
st.warning("No employee data available.")
|
172 |
+
return
|
173 |
+
|
174 |
+
# Display employee information
|
175 |
+
for employee in employees:
|
176 |
+
st.subheader(f"Employee: {employee['name']}")
|
177 |
+
col1, col2, col3 = st.columns(3)
|
178 |
+
col1.metric("Employee ID", employee['employee_id'])
|
179 |
+
col2.metric("Age", employee['age'])
|
180 |
+
col3.metric("Rank", employee['rank'])
|
181 |
+
|
182 |
+
col4, col5 = st.columns(2)
|
183 |
+
col4.metric("Last Performance Score", f"{employee['last_performance_score']:.2f}")
|
184 |
+
col5.metric("Current Performance Score", f"{employee['current_performance_score']:.2f}")
|
185 |
+
|
186 |
+
st.write("Future Advancement:", employee['future_advancement'])
|
187 |
+
st.markdown("---")
|
188 |
+
|
189 |
+
# Performance visualization
|
190 |
+
st.subheader("Performance Overview")
|
191 |
+
df = pd.DataFrame(employees)
|
192 |
+
fig = px.scatter(df, x="age", y="current_performance_score", size="rank",
|
193 |
+
hover_data=["name", "employee_id"], color="current_performance_score",
|
194 |
+
labels={"current_performance_score": "Current Performance Score"})
|
195 |
st.plotly_chart(fig)
|
196 |
+
|
197 |
+
if st.button("Logout"):
|
198 |
+
st.session_state.logged_in = False
|
199 |
+
st.experimental_rerun()
|
200 |
|
201 |
+
if __name__ == "__main__":
|
202 |
main()
|