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+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
import hashlib
|
| 5 |
+
|
| 6 |
+
# File to store user account data
|
| 7 |
+
user_file = 'users.csv'
|
| 8 |
+
# File to store truck registration data
|
| 9 |
+
data_file = 'truck_data.csv'
|
| 10 |
+
|
| 11 |
+
# Initialize user_data and truck_data as empty dictionaries
|
| 12 |
+
user_data = {}
|
| 13 |
+
truck_data = {}
|
| 14 |
+
|
| 15 |
+
# Function to hash passwords
|
| 16 |
+
def hash_password(password):
|
| 17 |
+
return hashlib.sha256(password.encode()).hexdigest()
|
| 18 |
+
|
| 19 |
+
# Function to create an empty users.csv file with the correct headers
|
| 20 |
+
def create_user_file():
|
| 21 |
+
pd.DataFrame(columns=["Username", "Password"]).to_csv(user_file, index=False)
|
| 22 |
+
|
| 23 |
+
# Function to create an empty truck_data.csv file with the correct headers
|
| 24 |
+
def create_data_file():
|
| 25 |
+
pd.DataFrame(columns=["Truck Number", "Driver's Name", "Contact Number", "Registration Date", "Username"]).to_csv(data_file, index=False)
|
| 26 |
+
|
| 27 |
+
# Check if the user file exists
|
| 28 |
+
if not os.path.exists(user_file):
|
| 29 |
+
create_user_file()
|
| 30 |
+
st.warning(f"{user_file} created successfully.")
|
| 31 |
+
|
| 32 |
+
# Load existing user data if available
|
| 33 |
+
if os.path.exists(user_file):
|
| 34 |
+
try:
|
| 35 |
+
user_data = pd.read_csv(user_file).set_index('Username').to_dict(orient='index')
|
| 36 |
+
except pd.errors.EmptyDataError:
|
| 37 |
+
st.warning("The user file is empty. Please register an account.")
|
| 38 |
+
create_user_file()
|
| 39 |
+
|
| 40 |
+
# Check if the data file exists
|
| 41 |
+
if not os.path.exists(data_file):
|
| 42 |
+
create_data_file()
|
| 43 |
+
st.warning(f"{data_file} created successfully.")
|
| 44 |
+
|
| 45 |
+
# Load existing truck data if available
|
| 46 |
+
if os.path.exists(data_file):
|
| 47 |
+
try:
|
| 48 |
+
truck_data_df = pd.read_csv(data_file)
|
| 49 |
+
# Ensure the file is not empty and contains the correct columns
|
| 50 |
+
if not truck_data_df.empty and "Truck Number" in truck_data_df.columns:
|
| 51 |
+
truck_data = truck_data_df.set_index('Truck Number').to_dict(orient='index')
|
| 52 |
+
else:
|
| 53 |
+
st.warning("The truck data file is empty or missing the required columns.")
|
| 54 |
+
create_data_file()
|
| 55 |
+
except pd.errors.EmptyDataError:
|
| 56 |
+
st.warning("The truck data file is empty. Please register some trucks.")
|
| 57 |
+
create_data_file()
|
| 58 |
+
|
| 59 |
+
# Streamlit form for login or registration
|
| 60 |
+
st.title("Truck Registration and Login System")
|
| 61 |
+
|
| 62 |
+
if "logged_in" not in st.session_state:
|
| 63 |
+
st.session_state.logged_in = False
|
| 64 |
+
st.session_state.current_user = None
|
| 65 |
+
|
| 66 |
+
# Sign Up and Login tabs
|
| 67 |
+
tab1, tab2 = st.tabs(["Sign Up", "Login"])
|
| 68 |
+
|
| 69 |
+
# Sign Up tab
|
| 70 |
+
with tab1:
|
| 71 |
+
st.subheader("Register a New Account")
|
| 72 |
+
|
| 73 |
+
# Input fields for new user registration
|
| 74 |
+
new_username = st.text_input("Username", key="register_username")
|
| 75 |
+
new_password = st.text_input("Password", type="password", key="register_password")
|
| 76 |
+
|
| 77 |
+
if st.button("Register", key="register_button"):
|
| 78 |
+
if new_username in user_data:
|
| 79 |
+
st.error("Username already exists. Please choose a different username.")
|
| 80 |
+
elif new_username and new_password:
|
| 81 |
+
user_data[new_username] = {
|
| 82 |
+
"Password": hash_password(new_password)
|
| 83 |
+
}
|
| 84 |
+
pd.DataFrame.from_dict(user_data, orient='index').reset_index().rename(columns={"index": "Username"}).to_csv(user_file, index=False)
|
| 85 |
+
st.session_state.logged_in = True
|
| 86 |
+
st.session_state.current_user = new_username
|
| 87 |
+
st.success("Account registered successfully! You are now logged in.")
|
| 88 |
+
else:
|
| 89 |
+
st.error("Please fill out both fields.")
|
| 90 |
+
|
| 91 |
+
# Login tab
|
| 92 |
+
with tab2:
|
| 93 |
+
st.subheader("Login to Your Account")
|
| 94 |
+
|
| 95 |
+
# Input fields for user login
|
| 96 |
+
username = st.text_input("Username", key="login_username")
|
| 97 |
+
password = st.text_input("Password", type="password", key="login_password")
|
| 98 |
+
|
| 99 |
+
if st.button("Login", key="login_button"):
|
| 100 |
+
if username in user_data and user_data[username]["Password"] == hash_password(password):
|
| 101 |
+
st.session_state.logged_in = True
|
| 102 |
+
st.session_state.current_user = username
|
| 103 |
+
st.success(f"Welcome, {username}!")
|
| 104 |
+
else:
|
| 105 |
+
st.error("Invalid username or password.")
|
| 106 |
+
|
| 107 |
+
# If logged in, show the truck registration form
|
| 108 |
+
if st.session_state.logged_in:
|
| 109 |
+
st.subheader("Register a new truck")
|
| 110 |
+
|
| 111 |
+
# Input fields for truck details
|
| 112 |
+
truck_number = st.text_input("Truck Number")
|
| 113 |
+
driver_name = st.text_input("Driver's Name")
|
| 114 |
+
contact_number = st.text_input("Contact Number")
|
| 115 |
+
registration_date = st.date_input("Registration Date")
|
| 116 |
+
|
| 117 |
+
# Button to submit the form
|
| 118 |
+
if st.button("Register Truck"):
|
| 119 |
+
if truck_number and driver_name and contact_number:
|
| 120 |
+
truck_data[truck_number] = {
|
| 121 |
+
"Driver's Name": driver_name,
|
| 122 |
+
"Contact Number": contact_number,
|
| 123 |
+
"Registration Date": registration_date,
|
| 124 |
+
"Username": st.session_state.current_user,
|
| 125 |
+
}
|
| 126 |
+
# Convert truck_data to DataFrame and save to CSV
|
| 127 |
+
if truck_data:
|
| 128 |
+
truck_data_df = pd.DataFrame.from_dict(truck_data, orient='index').reset_index().rename(columns={"index": "Truck Number"})
|
| 129 |
+
truck_data_df.to_csv(data_file, index=False)
|
| 130 |
+
st.success(f"Truck {truck_number} registered successfully!")
|
| 131 |
+
else:
|
| 132 |
+
st.error("Error saving truck data.")
|
| 133 |
+
else:
|
| 134 |
+
st.error("Please fill out all the fields.")
|
| 135 |
+
|
| 136 |
+
# Display the registered trucks for the logged-in user
|
| 137 |
+
st.subheader(f"Registered Trucks for {st.session_state.current_user}")
|
| 138 |
+
user_trucks = {truck: details for truck, details in truck_data.items() if "Username" in details and details["Username"] == st.session_state.current_user}
|
| 139 |
+
|
| 140 |
+
if user_trucks:
|
| 141 |
+
for truck, details in user_trucks.items():
|
| 142 |
+
st.write(f"**Truck Number:** {truck}")
|
| 143 |
+
st.write(f"**Driver's Name:** {details['Driver\'s Name']}")
|
| 144 |
+
st.write(f"**Contact Number:** {details['Contact Number']}")
|
| 145 |
+
st.write(f"**Registration Date:** {details['Registration Date']}")
|
| 146 |
+
st.write("---")
|
| 147 |
+
else:
|
| 148 |
+
st.write("No trucks registered yet.")
|
| 149 |
+
|
| 150 |
+
# Logout button
|
| 151 |
+
if st.session_state.logged_in:
|
| 152 |
+
if st.button("Logout"):
|
| 153 |
+
st.session_state.logged_in = False
|
| 154 |
+
st.session_state.current_user = None
|
| 155 |
+
st.success("Logged out successfully!")
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|