Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,89 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
|
|
|
| 3 |
import random
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# ----------------
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
age INTEGER,
|
| 13 |
-
password TEXT,
|
| 14 |
-
account_number INTEGER PRIMARY KEY,
|
| 15 |
-
balance INTEGER
|
| 16 |
-
)
|
| 17 |
-
''')
|
| 18 |
-
conn.commit()
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return acc_no, "β
Account Created!"
|
| 34 |
|
| 35 |
def login(name, acc_no, password):
|
| 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 |
if "logged_in" not in st.session_state:
|
| 64 |
st.session_state.logged_in = False
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
st.title("π¦ Baryashah Bank")
|
| 69 |
|
| 70 |
# ---------------- SIDEBAR ----------------
|
| 71 |
if not st.session_state.logged_in:
|
|
@@ -75,74 +93,99 @@ else:
|
|
| 75 |
|
| 76 |
# ---------------- CREATE ACCOUNT ----------------
|
| 77 |
if menu == "Create Account":
|
| 78 |
-
st.subheader("Create New Account")
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
| 91 |
|
| 92 |
# ---------------- LOGIN ----------------
|
| 93 |
elif menu == "Login":
|
| 94 |
-
st.subheader("Login")
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
# ---------------- DASHBOARD ----------------
|
| 111 |
elif menu == "Dashboard":
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
user =
|
| 115 |
-
|
| 116 |
-
st.subheader(f"Welcome, {name}
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
if option == "Check Balance":
|
| 122 |
-
st.
|
| 123 |
|
| 124 |
-
# DEPOSIT
|
| 125 |
elif option == "Deposit":
|
| 126 |
amount = st.number_input("Enter Amount", min_value=1)
|
| 127 |
-
if st.button("Deposit"):
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
| 133 |
elif option == "Withdraw":
|
| 134 |
amount = st.number_input("Enter Amount", min_value=1)
|
| 135 |
-
if st.button("Withdraw"):
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
if new_balance is None:
|
| 139 |
-
st.error(msg)
|
| 140 |
else:
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
import random
|
| 5 |
+
import hashlib
|
| 6 |
+
from datetime import datetime
|
| 7 |
|
| 8 |
+
# ---------------- FILE SETUP ----------------
|
| 9 |
+
USER_FILE = "users.json"
|
| 10 |
+
TRANS_FILE = "transactions.json"
|
| 11 |
|
| 12 |
+
if not os.path.exists(USER_FILE):
|
| 13 |
+
with open(USER_FILE, "w") as f:
|
| 14 |
+
json.dump({}, f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
if not os.path.exists(TRANS_FILE):
|
| 17 |
+
with open(TRANS_FILE, "w") as f:
|
| 18 |
+
json.dump({}, f)
|
| 19 |
|
| 20 |
+
# ---------------- PASSWORD HASHING ----------------
|
| 21 |
+
def hash_password(password):
|
| 22 |
+
return hashlib.sha256(password.encode()).hexdigest()
|
| 23 |
|
| 24 |
+
# ---------------- USER FUNCTIONS ----------------
|
| 25 |
+
def load_users():
|
| 26 |
+
with open(USER_FILE, "r") as f:
|
| 27 |
+
return json.load(f)
|
| 28 |
|
| 29 |
+
def save_users(users):
|
| 30 |
+
with open(USER_FILE, "w") as f:
|
| 31 |
+
json.dump(users, f)
|
| 32 |
|
| 33 |
+
def create_account(name, age, password):
|
| 34 |
+
users = load_users()
|
| 35 |
+
if name in users:
|
| 36 |
+
return None, "β Account already exists!"
|
| 37 |
+
acc_no = random.randint(1000, 9999)
|
| 38 |
+
users[name] = {
|
| 39 |
+
"age": age,
|
| 40 |
+
"password": hash_password(password),
|
| 41 |
+
"account_number": acc_no,
|
| 42 |
+
"balance": 0
|
| 43 |
+
}
|
| 44 |
+
save_users(users)
|
| 45 |
return acc_no, "β
Account Created!"
|
| 46 |
|
| 47 |
def login(name, acc_no, password):
|
| 48 |
+
users = load_users()
|
| 49 |
+
if name in users:
|
| 50 |
+
user = users[name]
|
| 51 |
+
if user["account_number"] == acc_no and user["password"] == hash_password(password):
|
| 52 |
+
return user
|
| 53 |
+
return None
|
| 54 |
+
|
| 55 |
+
def update_balance(name, new_balance):
|
| 56 |
+
users = load_users()
|
| 57 |
+
users[name]["balance"] = new_balance
|
| 58 |
+
save_users(users)
|
| 59 |
+
|
| 60 |
+
# ---------------- TRANSACTION FUNCTIONS ----------------
|
| 61 |
+
def record_transaction(name, type_, amount):
|
| 62 |
+
with open(TRANS_FILE, "r") as f:
|
| 63 |
+
transactions = json.load(f)
|
| 64 |
+
if name not in transactions:
|
| 65 |
+
transactions[name] = []
|
| 66 |
+
transactions[name].append({
|
| 67 |
+
"type": type_,
|
| 68 |
+
"amount": amount,
|
| 69 |
+
"time": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 70 |
+
})
|
| 71 |
+
with open(TRANS_FILE, "w") as f:
|
| 72 |
+
json.dump(transactions, f)
|
| 73 |
+
|
| 74 |
+
def get_transactions(name):
|
| 75 |
+
with open(TRANS_FILE, "r") as f:
|
| 76 |
+
transactions = json.load(f)
|
| 77 |
+
return transactions.get(name, [])
|
| 78 |
+
|
| 79 |
+
# ---------------- STREAMLIT UI ----------------
|
| 80 |
+
st.set_page_config(page_title="Baryashah Bank", layout="wide")
|
| 81 |
+
st.title("π¦ Baryashah Bank")
|
| 82 |
+
|
| 83 |
if "logged_in" not in st.session_state:
|
| 84 |
st.session_state.logged_in = False
|
| 85 |
+
if "user_name" not in st.session_state:
|
| 86 |
+
st.session_state.user_name = None
|
|
|
|
|
|
|
| 87 |
|
| 88 |
# ---------------- SIDEBAR ----------------
|
| 89 |
if not st.session_state.logged_in:
|
|
|
|
| 93 |
|
| 94 |
# ---------------- CREATE ACCOUNT ----------------
|
| 95 |
if menu == "Create Account":
|
| 96 |
+
st.subheader("π Create New Account")
|
| 97 |
+
col1, col2 = st.columns([2,3])
|
| 98 |
+
with col1:
|
| 99 |
+
name = st.text_input("Name")
|
| 100 |
+
age = st.number_input("Age", min_value=1)
|
| 101 |
+
with col2:
|
| 102 |
+
password = st.text_input("Password", type="password")
|
| 103 |
+
if st.button("Create Account"):
|
| 104 |
+
acc_no, msg = create_account(name, age, password)
|
| 105 |
+
if acc_no:
|
| 106 |
+
st.success(f"{msg} π")
|
| 107 |
+
st.info(f"Your Account Number: {acc_no}")
|
| 108 |
+
else:
|
| 109 |
+
st.error(msg)
|
| 110 |
|
| 111 |
# ---------------- LOGIN ----------------
|
| 112 |
elif menu == "Login":
|
| 113 |
+
st.subheader("π Login")
|
| 114 |
+
col1, col2 = st.columns([2,3])
|
| 115 |
+
with col1:
|
| 116 |
+
name = st.text_input("Name")
|
| 117 |
+
acc_no = st.number_input("Account Number")
|
| 118 |
+
with col2:
|
| 119 |
+
password = st.text_input("Password", type="password")
|
| 120 |
+
if st.button("Login"):
|
| 121 |
+
user = login(name, acc_no, password)
|
| 122 |
+
if user:
|
| 123 |
+
st.session_state.logged_in = True
|
| 124 |
+
st.session_state.user_name = name
|
| 125 |
+
st.success("β
Login Successful")
|
| 126 |
+
else:
|
| 127 |
+
st.error("β Invalid Credentials")
|
| 128 |
|
| 129 |
# ---------------- DASHBOARD ----------------
|
| 130 |
elif menu == "Dashboard":
|
| 131 |
+
name = st.session_state.user_name
|
| 132 |
+
users = load_users()
|
| 133 |
+
user = users[name]
|
| 134 |
+
|
| 135 |
+
st.subheader(f"π Welcome, {name}")
|
| 136 |
+
|
| 137 |
+
# ---------------- BALANCE CARD ----------------
|
| 138 |
+
st.markdown(f"""
|
| 139 |
+
<div style="background-color:#d1f7c4;padding:20px;border-radius:15px">
|
| 140 |
+
<h3>π° Balance: {user['balance']}</h3>
|
| 141 |
+
<p>Account Number: {user['account_number']}</p>
|
| 142 |
+
</div>
|
| 143 |
+
""", unsafe_allow_html=True)
|
| 144 |
+
|
| 145 |
+
# ---------------- ACTIONS ----------------
|
| 146 |
+
col1, col2 = st.columns(2)
|
| 147 |
+
with col1:
|
| 148 |
+
option = st.selectbox("Choose Action", ["Check Balance", "Deposit", "Withdraw", "Transactions"])
|
| 149 |
+
with col2:
|
| 150 |
+
st.markdown("<br>", unsafe_allow_html=True)
|
| 151 |
+
if st.button("Logout"):
|
| 152 |
+
st.session_state.logged_in = False
|
| 153 |
+
st.session_state.user_name = None
|
| 154 |
+
st.success("π Logged out successfully")
|
| 155 |
+
|
| 156 |
+
# ---------------- CHECK BALANCE ----------------
|
| 157 |
if option == "Check Balance":
|
| 158 |
+
st.success(f"π° Your Balance: {user['balance']}")
|
| 159 |
|
| 160 |
+
# ---------------- DEPOSIT ----------------
|
| 161 |
elif option == "Deposit":
|
| 162 |
amount = st.number_input("Enter Amount", min_value=1)
|
| 163 |
+
if st.button("Deposit Now"):
|
| 164 |
+
user["balance"] += amount
|
| 165 |
+
update_balance(name, user["balance"])
|
| 166 |
+
record_transaction(name, "Deposit", amount)
|
| 167 |
+
st.success(f"β
Deposited {amount}")
|
| 168 |
+
st.info(f"π° New Balance: {user['balance']}")
|
| 169 |
+
|
| 170 |
+
# ---------------- WITHDRAW ----------------
|
| 171 |
elif option == "Withdraw":
|
| 172 |
amount = st.number_input("Enter Amount", min_value=1)
|
| 173 |
+
if st.button("Withdraw Now"):
|
| 174 |
+
if user["balance"] < amount:
|
| 175 |
+
st.error("β Insufficient Balance")
|
|
|
|
|
|
|
| 176 |
else:
|
| 177 |
+
user["balance"] -= amount
|
| 178 |
+
update_balance(name, user["balance"])
|
| 179 |
+
record_transaction(name, "Withdraw", amount)
|
| 180 |
+
st.success(f"β
Withdrawn {amount}")
|
| 181 |
+
st.info(f"π° Remaining Balance: {user['balance']}")
|
| 182 |
+
|
| 183 |
+
# ---------------- TRANSACTIONS ----------------
|
| 184 |
+
elif option == "Transactions":
|
| 185 |
+
txns = get_transactions(name)
|
| 186 |
+
if txns:
|
| 187 |
+
st.subheader("π Transaction History")
|
| 188 |
+
for t in reversed(txns):
|
| 189 |
+
st.info(f"{t['time']} β {t['type']}: {t['amount']}")
|
| 190 |
+
else:
|
| 191 |
+
st.info("No transactions yet")
|