Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import matplotlib.pyplot as plt
|
4 |
+
import seaborn as sns
|
5 |
+
|
6 |
+
# Create an empty DataFrame for expenses
|
7 |
+
columns = ['Date', 'Category', 'Description', 'Amount']
|
8 |
+
expenses_df = pd.DataFrame(columns=columns)
|
9 |
+
|
10 |
+
# Streamlit app layout
|
11 |
+
st.title('Expense Tracker')
|
12 |
+
|
13 |
+
# Add new expense entry
|
14 |
+
st.subheader("Add New Expense")
|
15 |
+
|
16 |
+
date = st.date_input("Date")
|
17 |
+
category = st.selectbox("Category", ['Food', 'Transport', 'Entertainment', 'Other'])
|
18 |
+
description = st.text_input("Description")
|
19 |
+
amount = st.number_input("Amount", min_value=0.0, format="%.2f")
|
20 |
+
|
21 |
+
if st.button("Add Expense"):
|
22 |
+
new_expense = pd.DataFrame([[date, category, description, amount]], columns=expenses_df.columns)
|
23 |
+
expenses_df = pd.concat([expenses_df, new_expense], ignore_index=True)
|
24 |
+
st.success(f"Added expense: {description} - ${amount:.2f}")
|
25 |
+
|
26 |
+
# Display all expenses
|
27 |
+
st.subheader("Expenses List")
|
28 |
+
st.write(expenses_df)
|
29 |
+
|
30 |
+
# Generate summary by category
|
31 |
+
category_summary = expenses_df.groupby('Category')['Amount'].sum().reset_index()
|
32 |
+
|
33 |
+
# Show category summary
|
34 |
+
st.subheader("Category Summary")
|
35 |
+
st.write(category_summary)
|
36 |
+
|
37 |
+
# Plot bar chart of expenses by category
|
38 |
+
st.subheader("Expenses by Category (Bar Chart)")
|
39 |
+
plt.figure(figsize=(10, 6))
|
40 |
+
sns.barplot(x='Category', y='Amount', data=category_summary)
|
41 |
+
plt.title('Total Expenses by Category')
|
42 |
+
plt.xlabel('Category')
|
43 |
+
plt.ylabel('Amount Spent ($)')
|
44 |
+
plt.xticks(rotation=45)
|
45 |
+
st.pyplot()
|
46 |
+
|
47 |
+
# Pie chart for category-wise expenses
|
48 |
+
st.subheader("Expense Distribution (Pie Chart)")
|
49 |
+
plt.figure(figsize=(8, 8))
|
50 |
+
category_summary.set_index('Category')['Amount'].plot(kind='pie', autopct='%1.1f%%', legend=False)
|
51 |
+
plt.title('Expense Distribution by Category')
|
52 |
+
plt.ylabel('')
|
53 |
+
st.pyplot()
|
54 |
+
|
55 |
+
# Save the expenses to a CSV file
|
56 |
+
st.download_button(
|
57 |
+
label="Download Expenses as CSV",
|
58 |
+
data=expenses_df.to_csv(index=False),
|
59 |
+
file_name="expenses.csv",
|
60 |
+
mime="text/csv"
|
61 |
+
)
|