Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import firebase_admin
|
| 3 |
+
from firebase_admin import credentials, db
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
# Initialize Firebase Realtime Database
|
| 8 |
+
try:
|
| 9 |
+
app = firebase_admin.get_app()
|
| 10 |
+
except ValueError:
|
| 11 |
+
cred = credentials.Certificate("serviceAccountKey.json")
|
| 12 |
+
app = firebase_admin.initialize_app(cred, {
|
| 13 |
+
'databaseURL': 'https://transacapp-22b6e-default-rtdb.firebaseio.com/' # Add your Firebase Realtime Database URL here
|
| 14 |
+
})
|
| 15 |
+
|
| 16 |
+
def fetch_user_transactions(username):
|
| 17 |
+
"""Fetch financial messages for a specific user from Firebase Realtime Database"""
|
| 18 |
+
try:
|
| 19 |
+
# Reference to the financial messages path for the specific user
|
| 20 |
+
ref = db.reference(f'financialMessages/{username}/Apr')
|
| 21 |
+
# Get all transactions
|
| 22 |
+
transactions = ref.get()
|
| 23 |
+
|
| 24 |
+
if not transactions:
|
| 25 |
+
return []
|
| 26 |
+
|
| 27 |
+
messages = []
|
| 28 |
+
# Convert the data to a list of dictionaries
|
| 29 |
+
for transaction_id, data in transactions.items():
|
| 30 |
+
if isinstance(data, dict): # Ensure we're only processing dictionary data
|
| 31 |
+
messages.append({
|
| 32 |
+
'Transaction ID': transaction_id,
|
| 33 |
+
'Account Number': data.get('accountNumber', ''),
|
| 34 |
+
'Amount': float(data.get('amount', 0)),
|
| 35 |
+
'Reference No': data.get('referenceNo', ''),
|
| 36 |
+
'Transaction Date': data.get('transactionDate', ''),
|
| 37 |
+
'Transaction Type': data.get('transactionType', '')
|
| 38 |
+
})
|
| 39 |
+
|
| 40 |
+
return messages
|
| 41 |
+
except Exception as e:
|
| 42 |
+
st.error(f"Error fetching data: {str(e)}")
|
| 43 |
+
return []
|
| 44 |
+
|
| 45 |
+
def main():
|
| 46 |
+
st.set_page_config(page_title="Financial Transactions Dashboard", layout="wide")
|
| 47 |
+
|
| 48 |
+
# Header
|
| 49 |
+
st.title("Financial Transactions Dashboard")
|
| 50 |
+
st.markdown("---")
|
| 51 |
+
|
| 52 |
+
# User input
|
| 53 |
+
username = st.text_input("Enter Username (e.g., Akshay Chame)", "Akshay Chame")
|
| 54 |
+
|
| 55 |
+
if username:
|
| 56 |
+
# Format username to match database structure (replace spaces with actual format)
|
| 57 |
+
formatted_username = username.strip()
|
| 58 |
+
|
| 59 |
+
# Fetch data
|
| 60 |
+
data = fetch_user_transactions(formatted_username)
|
| 61 |
+
|
| 62 |
+
if data:
|
| 63 |
+
df = pd.DataFrame(data)
|
| 64 |
+
|
| 65 |
+
# Convert amount to numeric
|
| 66 |
+
df['Amount'] = pd.to_numeric(df['Amount'])
|
| 67 |
+
|
| 68 |
+
# Sidebar filters
|
| 69 |
+
st.sidebar.header("Filters")
|
| 70 |
+
|
| 71 |
+
# Transaction type filter
|
| 72 |
+
transaction_types = st.sidebar.multiselect(
|
| 73 |
+
"Select Transaction Type",
|
| 74 |
+
options=df['Transaction Type'].unique(),
|
| 75 |
+
default=df['Transaction Type'].unique()
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
# Date range filter
|
| 79 |
+
dates = df['Transaction Date'].unique()
|
| 80 |
+
selected_dates = st.sidebar.multiselect(
|
| 81 |
+
"Select Dates",
|
| 82 |
+
options=dates,
|
| 83 |
+
default=dates
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# Apply filters
|
| 87 |
+
masked_df = df[
|
| 88 |
+
(df['Transaction Type'].isin(transaction_types)) &
|
| 89 |
+
(df['Transaction Date'].isin(selected_dates))
|
| 90 |
+
]
|
| 91 |
+
|
| 92 |
+
# Dashboard metrics
|
| 93 |
+
col1, col2, col3 = st.columns(3)
|
| 94 |
+
|
| 95 |
+
with col1:
|
| 96 |
+
st.metric("Total Transactions", len(masked_df))
|
| 97 |
+
|
| 98 |
+
with col2:
|
| 99 |
+
total_debited = masked_df[masked_df['Transaction Type'] == 'debited']['Amount'].sum()
|
| 100 |
+
st.metric("Total Debited", f"₹ {total_debited:,.2f}")
|
| 101 |
+
|
| 102 |
+
with col3:
|
| 103 |
+
total_credited = masked_df[masked_df['Transaction Type'] == 'credited']['Amount'].sum()
|
| 104 |
+
st.metric("Total Credited", f"₹ {total_credited:,.2f}")
|
| 105 |
+
|
| 106 |
+
# Transactions table
|
| 107 |
+
st.subheader("Recent Transactions")
|
| 108 |
+
st.dataframe(
|
| 109 |
+
masked_df,
|
| 110 |
+
column_config={
|
| 111 |
+
"Amount": st.column_config.NumberColumn(
|
| 112 |
+
"Amount",
|
| 113 |
+
format="₹ %.2f"
|
| 114 |
+
)
|
| 115 |
+
},
|
| 116 |
+
hide_index=True
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Charts
|
| 120 |
+
col1, col2 = st.columns(2)
|
| 121 |
+
|
| 122 |
+
with col1:
|
| 123 |
+
st.subheader("Transaction Type Distribution")
|
| 124 |
+
type_counts = masked_df['Transaction Type'].value_counts()
|
| 125 |
+
st.bar_chart(type_counts)
|
| 126 |
+
|
| 127 |
+
with col2:
|
| 128 |
+
st.subheader("Daily Transaction Amounts")
|
| 129 |
+
daily_amounts = masked_df.groupby('Transaction Date')['Amount'].sum()
|
| 130 |
+
st.line_chart(daily_amounts)
|
| 131 |
+
|
| 132 |
+
# Download button
|
| 133 |
+
if st.button("Download Transactions"):
|
| 134 |
+
csv = masked_df.to_csv(index=False)
|
| 135 |
+
st.download_button(
|
| 136 |
+
label="Download CSV",
|
| 137 |
+
data=csv,
|
| 138 |
+
file_name=f"{username}_transactions.csv",
|
| 139 |
+
mime="text/csv"
|
| 140 |
+
)
|
| 141 |
+
else:
|
| 142 |
+
st.warning(f"No transactions found for user: {username}")
|
| 143 |
+
|
| 144 |
+
if __name__ == "__main__":
|
| 145 |
+
main()
|