Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,191 +1,118 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import json
|
| 3 |
import os
|
| 4 |
import random
|
| 5 |
-
import
|
| 6 |
-
|
| 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 |
-
|
| 17 |
-
|
|
|
|
| 18 |
json.dump({}, f)
|
| 19 |
|
| 20 |
-
# ---
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
# ---
|
| 25 |
-
def
|
| 26 |
-
with open(
|
| 27 |
return json.load(f)
|
| 28 |
|
| 29 |
-
def
|
| 30 |
-
with open(
|
| 31 |
-
json.dump(
|
| 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 |
-
|
| 80 |
-
|
| 81 |
-
st.title("🏦 Baryashah Bank")
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
st.session_state.user_name = None
|
| 87 |
|
| 88 |
-
|
| 89 |
-
if not st.session_state.logged_in:
|
| 90 |
-
menu = st.sidebar.selectbox("Menu", ["Create Account", "Login"])
|
| 91 |
-
else:
|
| 92 |
-
menu = "Dashboard"
|
| 93 |
|
| 94 |
-
# ---
|
| 95 |
-
if
|
| 96 |
-
st.subheader("
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
acc_no
|
| 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 |
-
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 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
if
|
| 187 |
-
st.
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
else:
|
| 191 |
-
st.
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import random
|
| 3 |
+
import json
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import plotly.express as px
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# --- File setup ---
|
| 8 |
+
if not os.path.exists("Account_Details.json"):
|
| 9 |
+
with open("Account_Details.json", "w") as f:
|
| 10 |
json.dump({}, f)
|
| 11 |
|
| 12 |
+
# --- App UI ---
|
| 13 |
+
st.set_page_config(page_title="Baryashah Bank App", layout="wide")
|
| 14 |
+
st.markdown("<h1 style='text-align:center;color:darkblue;'>🌟 Baryashah Bank 🌟</h1>", unsafe_allow_html=True)
|
| 15 |
+
st.write("---")
|
| 16 |
|
| 17 |
+
# --- Helper Functions ---
|
| 18 |
+
def load_accounts():
|
| 19 |
+
with open("Account_Details.json", "r") as f:
|
| 20 |
return json.load(f)
|
| 21 |
|
| 22 |
+
def save_accounts(data):
|
| 23 |
+
with open("Account_Details.json", "w") as f:
|
| 24 |
+
json.dump(data, f, indent=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
def send_otp():
|
| 27 |
+
return str(random.randint(1000,9999))
|
|
|
|
| 28 |
|
| 29 |
+
# --- User Options ---
|
| 30 |
+
menu = ["Create Account", "Login / Check Balance", "Reset Password"]
|
| 31 |
+
choice = st.sidebar.selectbox("Menu", menu)
|
|
|
|
| 32 |
|
| 33 |
+
accounts = load_accounts()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# --- Create Account ---
|
| 36 |
+
if choice == "Create Account":
|
| 37 |
+
st.subheader("Create New Account")
|
| 38 |
+
name = st.text_input("Full Name")
|
| 39 |
+
age = st.number_input("Age", min_value=1, max_value=120)
|
| 40 |
+
password = st.text_input("Create Password", type="password")
|
| 41 |
+
if st.button("Create Account", key="create"):
|
| 42 |
+
if name in accounts:
|
| 43 |
+
st.error("Account already exists!")
|
| 44 |
+
else:
|
| 45 |
+
acc_no = random.randint(1000,9999)
|
| 46 |
+
accounts[name] = {
|
| 47 |
+
"age": age,
|
| 48 |
+
"password": password,
|
| 49 |
+
"account_number": acc_no,
|
| 50 |
+
"balance": 0,
|
| 51 |
+
"transactions": []
|
| 52 |
+
}
|
| 53 |
+
save_accounts(accounts)
|
| 54 |
+
st.success(f"Account created! Your Account Number: {acc_no}")
|
| 55 |
+
st.balloons()
|
| 56 |
+
|
| 57 |
+
# --- Password Reset ---
|
| 58 |
+
elif choice == "Reset Password":
|
| 59 |
+
st.subheader("Reset Your Password")
|
| 60 |
+
name = st.text_input("Enter Account Name")
|
| 61 |
+
if name in accounts:
|
| 62 |
+
if st.button("Send OTP"):
|
| 63 |
+
otp = send_otp()
|
| 64 |
+
st.session_state['otp'] = otp
|
| 65 |
+
st.info(f"Simulated OTP (for demo): {otp}")
|
| 66 |
+
user_otp = st.text_input("Enter OTP")
|
| 67 |
+
new_pass = st.text_input("Enter New Password", type="password")
|
| 68 |
+
if st.button("Reset Password", key="reset"):
|
| 69 |
+
if 'otp' in st.session_state and user_otp == st.session_state['otp']:
|
| 70 |
+
accounts[name]['password'] = new_pass
|
| 71 |
+
save_accounts(accounts)
|
| 72 |
+
st.success("Password reset successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
else:
|
| 74 |
+
st.error("Invalid OTP!")
|
| 75 |
+
|
| 76 |
+
# --- Login / Check Balance ---
|
| 77 |
+
elif choice == "Login / Check Balance":
|
| 78 |
+
st.subheader("Login")
|
| 79 |
+
name = st.text_input("Account Name")
|
| 80 |
+
acc_no = st.number_input("Account Number", min_value=1000, max_value=9999)
|
| 81 |
+
password = st.text_input("Password", type="password")
|
| 82 |
+
if st.button("Login"):
|
| 83 |
+
if name in accounts and accounts[name]['account_number'] == acc_no and accounts[name]['password'] == password:
|
| 84 |
+
st.success(f"Welcome {name}! ✅")
|
| 85 |
+
st.write(f"💰 Current Balance: {accounts[name]['balance']}")
|
| 86 |
+
|
| 87 |
+
# --- Transaction Section ---
|
| 88 |
+
col1, col2 = st.columns(2)
|
| 89 |
+
with col1:
|
| 90 |
+
withdraw_amt = st.number_input("Withdraw Amount", min_value=0)
|
| 91 |
+
if st.button("Withdraw", key="withdraw"):
|
| 92 |
+
if withdraw_amt <= accounts[name]['balance']:
|
| 93 |
+
accounts[name]['balance'] -= withdraw_amt
|
| 94 |
+
accounts[name]['transactions'].append({"type":"Withdraw", "amount":withdraw_amt})
|
| 95 |
+
save_accounts(accounts)
|
| 96 |
+
st.success(f"💸 Withdraw Successful! New Balance: {accounts[name]['balance']}")
|
| 97 |
+
st.balloons()
|
| 98 |
+
else:
|
| 99 |
+
st.error("Insufficient Balance!")
|
| 100 |
+
|
| 101 |
+
with col2:
|
| 102 |
+
deposit_amt = st.number_input("Deposit Amount", min_value=0)
|
| 103 |
+
if st.button("Deposit", key="deposit"):
|
| 104 |
+
accounts[name]['balance'] += deposit_amt
|
| 105 |
+
accounts[name]['transactions'].append({"type":"Deposit", "amount":deposit_amt})
|
| 106 |
+
save_accounts(accounts)
|
| 107 |
+
st.success(f"💰 Deposit Successful! New Balance: {accounts[name]['balance']}")
|
| 108 |
+
st.balloons()
|
| 109 |
+
|
| 110 |
+
# --- Transaction Chart ---
|
| 111 |
+
if accounts[name]['transactions']:
|
| 112 |
+
df = accounts[name]['transactions']
|
| 113 |
+
amounts = [t['amount'] if t['type']=='Deposit' else -t['amount'] for t in df]
|
| 114 |
+
types = [t['type'] for t in df]
|
| 115 |
+
fig = px.bar(x=list(range(1,len(amounts)+1)), y=amounts, color=types, labels={'x':'Transaction','y':'Amount'}, title="Transaction History")
|
| 116 |
+
st.plotly_chart(fig)
|
| 117 |
else:
|
| 118 |
+
st.error("Login failed! Check credentials.")
|