Spaces:
Sleeping
Sleeping
File size: 6,182 Bytes
b451f1a |
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 |
import streamlit as st
import pandas as pd
import os
import hashlib
# File to store user account data
user_file = 'users.csv'
# File to store truck registration data
data_file = 'truck_data.csv'
# Initialize user_data and truck_data as empty dictionaries
user_data = {}
truck_data = {}
# Function to hash passwords
def hash_password(password):
return hashlib.sha256(password.encode()).hexdigest()
# Function to create an empty users.csv file with the correct headers
def create_user_file():
pd.DataFrame(columns=["Username", "Password"]).to_csv(user_file, index=False)
# Function to create an empty truck_data.csv file with the correct headers
def create_data_file():
pd.DataFrame(columns=["Truck Number", "Driver's Name", "Contact Number", "Registration Date", "Username"]).to_csv(data_file, index=False)
# Check if the user file exists
if not os.path.exists(user_file):
create_user_file()
st.warning(f"{user_file} created successfully.")
# Load existing user data if available
if os.path.exists(user_file):
try:
user_data = pd.read_csv(user_file).set_index('Username').to_dict(orient='index')
except pd.errors.EmptyDataError:
st.warning("The user file is empty. Please register an account.")
create_user_file()
# Check if the data file exists
if not os.path.exists(data_file):
create_data_file()
st.warning(f"{data_file} created successfully.")
# Load existing truck data if available
if os.path.exists(data_file):
try:
truck_data_df = pd.read_csv(data_file)
# Ensure the file is not empty and contains the correct columns
if not truck_data_df.empty and "Truck Number" in truck_data_df.columns:
truck_data = truck_data_df.set_index('Truck Number').to_dict(orient='index')
else:
st.warning("The truck data file is empty or missing the required columns.")
create_data_file()
except pd.errors.EmptyDataError:
st.warning("The truck data file is empty. Please register some trucks.")
create_data_file()
# Streamlit form for login or registration
st.title("Truck Registration and Login System")
if "logged_in" not in st.session_state:
st.session_state.logged_in = False
st.session_state.current_user = None
# Sign Up and Login tabs
tab1, tab2 = st.tabs(["Sign Up", "Login"])
# Sign Up tab
with tab1:
st.subheader("Register a New Account")
# Input fields for new user registration
new_username = st.text_input("Username", key="register_username")
new_password = st.text_input("Password", type="password", key="register_password")
if st.button("Register", key="register_button"):
if new_username in user_data:
st.error("Username already exists. Please choose a different username.")
elif new_username and new_password:
user_data[new_username] = {
"Password": hash_password(new_password)
}
pd.DataFrame.from_dict(user_data, orient='index').reset_index().rename(columns={"index": "Username"}).to_csv(user_file, index=False)
st.session_state.logged_in = True
st.session_state.current_user = new_username
st.success("Account registered successfully! You are now logged in.")
else:
st.error("Please fill out both fields.")
# Login tab
with tab2:
st.subheader("Login to Your Account")
# Input fields for user login
username = st.text_input("Username", key="login_username")
password = st.text_input("Password", type="password", key="login_password")
if st.button("Login", key="login_button"):
if username in user_data and user_data[username]["Password"] == hash_password(password):
st.session_state.logged_in = True
st.session_state.current_user = username
st.success(f"Welcome, {username}!")
else:
st.error("Invalid username or password.")
# If logged in, show the truck registration form
if st.session_state.logged_in:
st.subheader("Register a new truck")
# Input fields for truck details
truck_number = st.text_input("Truck Number")
driver_name = st.text_input("Driver's Name")
contact_number = st.text_input("Contact Number")
registration_date = st.date_input("Registration Date")
# Button to submit the form
if st.button("Register Truck"):
if truck_number and driver_name and contact_number:
truck_data[truck_number] = {
"Driver's Name": driver_name,
"Contact Number": contact_number,
"Registration Date": registration_date,
"Username": st.session_state.current_user,
}
# Convert truck_data to DataFrame and save to CSV
if truck_data:
truck_data_df = pd.DataFrame.from_dict(truck_data, orient='index').reset_index().rename(columns={"index": "Truck Number"})
truck_data_df.to_csv(data_file, index=False)
st.success(f"Truck {truck_number} registered successfully!")
else:
st.error("Error saving truck data.")
else:
st.error("Please fill out all the fields.")
# Display the registered trucks for the logged-in user
st.subheader(f"Registered Trucks for {st.session_state.current_user}")
user_trucks = {truck: details for truck, details in truck_data.items() if "Username" in details and details["Username"] == st.session_state.current_user}
if user_trucks:
for truck, details in user_trucks.items():
st.write(f"**Truck Number:** {truck}")
st.write(f"**Driver's Name:** {details['Driver\'s Name']}")
st.write(f"**Contact Number:** {details['Contact Number']}")
st.write(f"**Registration Date:** {details['Registration Date']}")
st.write("---")
else:
st.write("No trucks registered yet.")
# Logout button
if st.session_state.logged_in:
if st.button("Logout"):
st.session_state.logged_in = False
st.session_state.current_user = None
st.success("Logged out successfully!")
|