File size: 2,766 Bytes
d385752
 
60a49af
 
d385752
 
604aca4
86c9bc1
 
60a49af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d385752
85e0fcf
 
d385752
60a49af
 
 
 
 
 
 
 
d385752
60a49af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85e0fcf
 
 
60a49af
85e0fcf
 
60a49af
85e0fcf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60a49af
85e0fcf
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import streamlit as st
import pandas as pd
import firebase_admin
from firebase_admin import credentials, firestore
import matplotlib.pyplot as plt
import seaborn as sns

# Path to Firebase credentials file
cred = credentials.Certificate('firebase_credentials/your-credentials-file.json')
firebase_admin.initialize_app(cred)

# Connect to Firestore
db = firestore.client()

# Collection name for expenses
expenses_ref = db.collection('expenses')

# Function to add an expense to Firebase
def add_expense(date, category, description, amount):
    expenses_ref.add({
        'Date': date,
        'Category': category,
        'Description': description,
        'Amount': amount
    })

# Initialize the UI
st.title("Daily Expense Tracker")

# Add expense form
with st.form(key="expense_form"):
    date = st.date_input("Date")
    category = st.selectbox("Category", ['Food', 'Transport', 'Entertainment', 'Other'])
    description = st.text_input("Description")
    amount = st.number_input("Amount", min_value=0.0, format="%.2f")

    submit_button = st.form_submit_button(label="Add Expense")

    if submit_button:
        add_expense(date, category, description, amount)
        st.success(f"Added expense: {description} - ${amount:.2f}")

# Retrieve expenses from Firebase
expenses_query = expenses_ref.stream()

# Store expenses in a DataFrame
expenses_data = []
for expense in expenses_query:
    expenses_data.append(expense.to_dict())

if expenses_data:
    expenses_df = pd.DataFrame(expenses_data)
else:
    expenses_df = pd.DataFrame(columns=['Date', 'Category', 'Description', 'Amount'])

# Display expenses list
st.subheader("Expenses List")
st.write(expenses_df)

# Generate summary by category
category_summary = expenses_df.groupby('Category')['Amount'].sum().reset_index()

st.subheader("Category Summary")
st.write(category_summary)

# Visualize the expenses by category (Bar Chart)
st.subheader("Expense Visualization (Bar Chart)")
fig, ax = plt.subplots(figsize=(10, 6))
sns.barplot(x='Category', y='Amount', data=category_summary, ax=ax)
ax.set_title('Total Expenses by Category')
ax.set_xlabel('Category')
ax.set_ylabel('Amount Spent ($)')
ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
st.pyplot(fig)

# Pie chart for category-wise expenses
st.subheader("Expense Distribution (Pie Chart)")
fig2, ax2 = plt.subplots(figsize=(8, 8))
category_summary.set_index('Category')['Amount'].plot(kind='pie', autopct='%1.1f%%', legend=False, ax=ax2)
ax2.set_title('Expense Distribution by Category')
ax2.set_ylabel('')  # Hide ylabel
st.pyplot(fig2)

# Option to save the expenses to a CSV file
st.download_button(
    label="Download Expenses as CSV",
    data=expenses_df.to_csv(index=False),
    file_name="expenses.csv",
    mime="text/csv"
)