|
|
import streamlit as st |
|
|
import firebase_admin |
|
|
from firebase_admin import credentials, db |
|
|
import pandas as pd |
|
|
from datetime import datetime |
|
|
|
|
|
|
|
|
try: |
|
|
app = firebase_admin.get_app() |
|
|
except ValueError: |
|
|
cred = credentials.Certificate("serviceAccountKey.json") |
|
|
app = firebase_admin.initialize_app(cred, { |
|
|
'databaseURL': 'https://transacapp-22b6e-default-rtdb.firebaseio.com/' |
|
|
}) |
|
|
|
|
|
def fetch_user_transactions(username): |
|
|
"""Fetch financial messages for a specific user from Firebase Realtime Database""" |
|
|
try: |
|
|
|
|
|
ref = db.reference(f'financialMessages/{username}/Apr') |
|
|
|
|
|
transactions = ref.get() |
|
|
|
|
|
if not transactions: |
|
|
return [] |
|
|
|
|
|
messages = [] |
|
|
|
|
|
for transaction_id, data in transactions.items(): |
|
|
if isinstance(data, dict): |
|
|
messages.append({ |
|
|
'Transaction ID': transaction_id, |
|
|
'Account Number': data.get('accountNumber', ''), |
|
|
'Amount': float(data.get('amount', 0)), |
|
|
'Reference No': data.get('referenceNo', ''), |
|
|
'Transaction Date': data.get('transactionDate', ''), |
|
|
'Transaction Type': data.get('transactionType', '') |
|
|
}) |
|
|
|
|
|
return messages |
|
|
except Exception as e: |
|
|
st.error(f"Error fetching data: {str(e)}") |
|
|
return [] |
|
|
|
|
|
def main(): |
|
|
st.set_page_config(page_title="Financial Transactions Dashboard", layout="wide") |
|
|
|
|
|
|
|
|
st.title("Financial Transactions Dashboard") |
|
|
st.markdown("---") |
|
|
|
|
|
|
|
|
username = st.text_input("Enter Username (e.g., Akshay Chame)", "Akshay Chame") |
|
|
|
|
|
if username: |
|
|
|
|
|
formatted_username = username.strip() |
|
|
|
|
|
|
|
|
data = fetch_user_transactions(formatted_username) |
|
|
|
|
|
if data: |
|
|
df = pd.DataFrame(data) |
|
|
|
|
|
|
|
|
df['Amount'] = pd.to_numeric(df['Amount']) |
|
|
|
|
|
|
|
|
st.sidebar.header("Filters") |
|
|
|
|
|
|
|
|
transaction_types = st.sidebar.multiselect( |
|
|
"Select Transaction Type", |
|
|
options=df['Transaction Type'].unique(), |
|
|
default=df['Transaction Type'].unique() |
|
|
) |
|
|
|
|
|
|
|
|
dates = df['Transaction Date'].unique() |
|
|
selected_dates = st.sidebar.multiselect( |
|
|
"Select Dates", |
|
|
options=dates, |
|
|
default=dates |
|
|
) |
|
|
|
|
|
|
|
|
masked_df = df[ |
|
|
(df['Transaction Type'].isin(transaction_types)) & |
|
|
(df['Transaction Date'].isin(selected_dates)) |
|
|
] |
|
|
|
|
|
|
|
|
col1, col2, col3 = st.columns(3) |
|
|
|
|
|
with col1: |
|
|
st.metric("Total Transactions", len(masked_df)) |
|
|
|
|
|
with col2: |
|
|
total_debited = masked_df[masked_df['Transaction Type'] == 'debited']['Amount'].sum() |
|
|
st.metric("Total Debited", f"₹ {total_debited:,.2f}") |
|
|
|
|
|
with col3: |
|
|
total_credited = masked_df[masked_df['Transaction Type'] == 'credited']['Amount'].sum() |
|
|
st.metric("Total Credited", f"₹ {total_credited:,.2f}") |
|
|
|
|
|
|
|
|
st.subheader("Recent Transactions") |
|
|
st.dataframe( |
|
|
masked_df, |
|
|
column_config={ |
|
|
"Amount": st.column_config.NumberColumn( |
|
|
"Amount", |
|
|
format="₹ %.2f" |
|
|
) |
|
|
}, |
|
|
hide_index=True |
|
|
) |
|
|
|
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
|
|
|
with col1: |
|
|
st.subheader("Transaction Type Distribution") |
|
|
type_counts = masked_df['Transaction Type'].value_counts() |
|
|
st.bar_chart(type_counts) |
|
|
|
|
|
with col2: |
|
|
st.subheader("Daily Transaction Amounts") |
|
|
daily_amounts = masked_df.groupby('Transaction Date')['Amount'].sum() |
|
|
st.line_chart(daily_amounts) |
|
|
|
|
|
|
|
|
if st.button("Download Transactions"): |
|
|
csv = masked_df.to_csv(index=False) |
|
|
st.download_button( |
|
|
label="Download CSV", |
|
|
data=csv, |
|
|
file_name=f"{username}_transactions.csv", |
|
|
mime="text/csv" |
|
|
) |
|
|
else: |
|
|
st.warning(f"No transactions found for user: {username}") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |