Spaces:
Sleeping
Sleeping
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" | |
) | |