import streamlit as st
import firebase_admin
from firebase_admin import credentials, db
import pandas as pd
import plotly.express as px
from datetime import datetime
# Initialize Firebase Realtime Database
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_usernames():
"""Fetch list of all usernames from Firebase"""
try:
ref = db.reference('financialMessages')
users = ref.get()
if users:
return list(users.keys())
return []
except Exception as e:
st.error(f"Error fetching usernames: {str(e)}")
return []
def fetch_user_transactions(username, selected_month):
"""Fetch financial messages for a specific user and month from Firebase"""
try:
ref = db.reference(f'financialMessages/{username}/{selected_month}')
transactions = ref.get()
if not transactions:
return []
messages = []
for transaction_id, data in transactions.items():
if isinstance(data, dict):
messages.append({
'Person Name': data.get('personName', ''),
'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 create_transaction_distribution_chart(df):
"""Create an enhanced transaction distribution visualization with multiple chart types"""
# Calculate transaction type summaries
type_summary = df.groupby('Transaction Type').agg({
'Person Name': 'count',
'Amount': ['sum', 'mean', 'min', 'max']
}).round(2)
type_summary.columns = ['Count', 'Total Amount', 'Average Amount', 'Min Amount', 'Max Amount']
type_summary = type_summary.reset_index()
# Create bar chart comparing transaction counts and amounts
fig_comparison = px.bar(
type_summary,
x='Transaction Type',
y=['Count', 'Total Amount'],
barmode='group',
title='Transaction Comparison by Type',
labels={'value': 'Value', 'variable': 'Metric'},
color_discrete_sequence=['#4C78A8', '#72B7B2'],
template='plotly_white'
)
fig_comparison.update_layout(
xaxis_title="Transaction Type",
yaxis_title="Value",
legend_title="Metric",
height=400,
showlegend=True,
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1
)
)
# Create detailed metrics visualization
fig_metrics = px.bar(
type_summary.melt(
id_vars=['Transaction Type'],
value_vars=['Average Amount', 'Min Amount', 'Max Amount']
),
x='Transaction Type',
y='value',
color='variable',
barmode='group',
title='Transaction Amount Metrics by Type',
labels={'value': 'Amount (₹)', 'variable': 'Metric'},
color_discrete_sequence=['#FF9DA7', '#9C755F', '#BAB0AC'],
template='plotly_white'
)
fig_metrics.update_layout(
xaxis_title="Transaction Type",
yaxis_title="Amount (₹)",
height=400,
showlegend=True,
legend=dict(
orientation="h",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1
)
)
# Add hover information
for fig in [fig_comparison, fig_metrics]:
fig.update_traces(
hovertemplate="
".join([
"Transaction Type: %{x}",
"Value: %{y:,.2f}",
""
])
)
return fig_comparison, fig_metrics
def main():
st.set_page_config(page_title="Financial Transactions Dashboard", layout="wide")
# Header
st.title("Financial Transactions Dashboard")
st.markdown("---")
# Sidebar filters
st.sidebar.header("Filters")
# Username dropdown
usernames = fetch_usernames()
username = st.sidebar.selectbox(
"Select Username",
options=usernames if usernames else ["No users found"]
)
# Month selection
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
selected_month = st.sidebar.selectbox("Select Month", options=months)
if username and username != "No users found":
# Fetch data
data = fetch_user_transactions(username, selected_month)
if data:
df = pd.DataFrame(data)
df['Amount'] = pd.to_numeric(df['Amount'])
# Transaction type dropdown
transaction_type = st.sidebar.selectbox(
"Select Transaction Type",
options=["All", "debited", "credited"]
)
# Date filter
dates = df['Transaction Date'].unique()
selected_dates = st.sidebar.multiselect(
"Select Dates",
options=dates,
default=dates
)
# Apply filters
if transaction_type != "All":
masked_df = df[
(df['Transaction Type'] == transaction_type) &
(df['Transaction Date'].isin(selected_dates))
]
else:
masked_df = df[df['Transaction Date'].isin(selected_dates)]
# Dashboard metrics
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}")
# Transactions table
st.subheader("Recent Transactions")
st.dataframe(
masked_df,
column_config={
"Amount": st.column_config.NumberColumn(
"Amount",
format="₹ %.2f"
)
},
hide_index=True
)
# Create transaction distribution visualizations
fig_count, fig_amount = create_transaction_distribution_chart(masked_df)
# Display visualizations in columns
st.subheader("Transaction Distribution Analysis")
col1, col2 = st.columns(2)
with col1:
st.plotly_chart(fig_count, use_container_width=True)
with col2:
st.plotly_chart(fig_amount, use_container_width=True)
# Daily transactions chart
st.subheader("Daily Transaction Amounts")
daily_amounts = masked_df.groupby('Transaction Date')['Amount'].sum()
st.line_chart(daily_amounts)
# Download button
if st.button("Download Transactions"):
csv = masked_df.to_csv(index=False)
st.download_button(
label="Download CSV",
data=csv,
file_name=f"{username}_{selected_month}_transactions.csv",
mime="text/csv"
)
else:
st.warning(f"No transactions found for user: {username} in {selected_month}")
if __name__ == "__main__":
main()