import streamlit as st import mysql.connector import bcrypt import datetime import re import pytz import numpy as np import pandas as pd import plotly.express as px import plotly.graph_objs as go # MySQL Connection connection = mysql.connector.connect( host="gateway01.ap-southeast-1.prod.aws.tidbcloud.com", port=4000, user="37QUb7dvTn3P6E8.root", password="MBAg14V0HaMdxwX0" ) mycursor = connection.cursor(buffered=True) mycursor.execute("CREATE DATABASE IF NOT EXISTS PerformancePrediction") mycursor.execute('USE PerformancePrediction') mycursor.execute('''CREATE TABLE IF NOT EXISTS User_data (id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) UNIQUE NOT NULL, password VARCHAR(255) NOT NULL, email VARCHAR(255) UNIQUE NOT NULL, registered_date TIMESTAMP, last_login TIMESTAMP)''') def username_exists(username): mycursor.execute("SELECT * FROM User_data WHERE username = %s", (username,)) return mycursor.fetchone() is not None def email_exists(email): mycursor.execute("SELECT * FROM User_data WHERE email = %s", (email,)) return mycursor.fetchone() is not None def is_valid_email(email): pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' return re.match(pattern, email) is not None def create_user(username, password, email, registered_date): if username_exists(username): return 'username_exists' if email_exists(email): return 'email_exists' hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) mycursor.execute( "INSERT INTO User_data (username, password, email, registered_date) VALUES (%s, %s, %s, %s)", (username, hashed_password, email, registered_date) ) connection.commit() return 'success' def verify_user(username, password): mycursor.execute("SELECT password FROM User_data WHERE username = %s", (username,)) record = mycursor.fetchone() if record and bcrypt.checkpw(password.encode('utf-8'), record[0].encode('utf-8')): mycursor.execute("UPDATE User_data SET last_login = %s WHERE username = %s", (datetime.datetime.now(pytz.timezone('Asia/Kolkata')), username)) connection.commit() return True return False def reset_password(username, new_password): hashed_password = bcrypt.hashpw(new_password.encode('utf-8'), bcrypt.gensalt()) mycursor.execute( "UPDATE User_data SET password = %s WHERE username = %s", (hashed_password, username) ) connection.commit() # Session state management if 'sign_up_successful' not in st.session_state: st.session_state.sign_up_successful = False if 'login_successful' not in st.session_state: st.session_state.login_successful = False if 'reset_password' not in st.session_state: st.session_state.reset_password = False if 'username' not in st.session_state: st.session_state.username = '' if 'current_page' not in st.session_state: st.session_state.current_page = 'login' # Page styling st.markdown(""" """, unsafe_allow_html=True) # Login page def login(): st.markdown('
Performance Nexus 🎯
', unsafe_allow_html=True) col1, col2, col3 = st.columns([1, 2, 1]) with col2: with st.form(key='login_form', clear_on_submit=True): username = st.text_input(label='Username', placeholder='Enter Username') password = st.text_input(label='Password', placeholder='Enter Password', type='password') st.markdown("Sign Up for Performance Prediction
', unsafe_allow_html=True) with st.form(key='signup_form', clear_on_submit=True): email = st.text_input(label='Email', placeholder='Enter Your Email') username = st.text_input(label='Username', placeholder='Enter Your Username') password = st.text_input(label='Password', placeholder='Enter Your Password', type='password') re_password = st.text_input(label='Confirm Password', placeholder='Confirm Your Password', type='password') if st.form_submit_button('Sign Up'): if not email or not username or not password or not re_password: st.error("Enter all the Credentials") elif len(password) <= 3: st.error("Password too short") elif password != re_password: st.error("Passwords do not match! Please Re-enter") else: result = create_user(username, password, email, datetime.datetime.now(pytz.timezone('Asia/Kolkata'))) if result == 'success': st.success("Account created successfully!") st.session_state.sign_up_successful = True st.session_state.current_page = 'login' st.experimental_rerun() if not st.session_state.get('sign_up_successful', False): st.markdown("Reset Password
', unsafe_allow_html=True) with st.form(key='reset_password_form', clear_on_submit=True): username = st.text_input(label='Username', value='', placeholder='Enter your username') new_password = st.text_input(label='New Password', type='password', placeholder='Enter new password') re_password = st.text_input(label='Confirm New Password', type='password', placeholder='Confirm new password') if st.form_submit_button('Reset Password'): if not username: st.error("Enter your username.") elif not username_exists(username): st.error("Username not found. Enter a valid username.") elif not new_password or not re_password: st.error("Enter new password and confirm password.") elif new_password != re_password: st.error("Passwords do not match. Please re-enter.") else: reset_password(username, new_password) st.success("Password reset successful. You can now login with your new password.") st.session_state.reset_password = False st.session_state.current_page = 'login' st.experimental_rerun() if not st.session_state.get('reset_password', False): st.markdown("Welcome to the Performance Nexus Dashboard, {st.session_state.username}!
', unsafe_allow_html=True) # Sidebar for user input (unchanged) st.sidebar.header("Input Parameters") employee_name = st.sidebar.text_input("Employee Name", "") employee_id = st.sidebar.text_input("Employee ID", "") employee_age = st.sidebar.number_input("Employee Age", min_value=18, max_value=100, value=25) employee_joining_date = st.sidebar.date_input("Employee Joining Date", datetime.date.today()) employee_last_appraisal_date = st.sidebar.date_input("Last Appraisal Date", datetime.date.today()) # Submit button if st.sidebar.button("Submit"): st.session_state.submitted = True # Main dashboard area if not st.session_state.get('submitted', False): # Display image only before submission st.image("homepageimage1.webp", use_column_width=True) st.markdown("