Dearsawan commited on
Commit
008d8f8
·
verified ·
1 Parent(s): 2c0c05d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -12
app.py CHANGED
@@ -3,9 +3,10 @@ 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')
@@ -19,42 +20,73 @@ 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
  # Ensure 'Amount' is numeric (convert if necessary)
31
- expenses_df['Amount'] = pd.to_numeric(expenses_df['Amount'], errors='coerce')
32
 
33
- # Generate summary by category
34
  st.subheader("Category Summary")
35
- category_summary = expenses_df.groupby('Category')['Amount'].sum().reset_index()
36
  st.write(category_summary)
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  # Bar chart for total expenses by category
 
39
  fig, ax = plt.subplots(figsize=(10, 6))
40
  sns.barplot(x='Category', y='Amount', data=category_summary, ax=ax)
41
- ax.set_title('Total Expenses by Category')
42
  ax.set_xlabel('Category')
43
  ax.set_ylabel('Amount Spent ($)')
44
  ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
45
  st.pyplot(fig)
46
 
47
  # Pie chart for expense distribution by category
 
48
  fig, ax = plt.subplots(figsize=(8, 8))
49
  category_summary.set_index('Category')['Amount'].plot(kind='pie', autopct='%1.1f%%', legend=False, ax=ax)
50
- ax.set_title('Expense Distribution by Category')
51
  ax.set_ylabel('') # Hide ylabel
52
  st.pyplot(fig)
53
 
54
  # Save the expenses to a CSV file
55
  st.download_button(
56
  label="Download Expenses as CSV",
57
- data=expenses_df.to_csv(index=False),
58
  file_name="expenses.csv",
59
  mime="text/csv"
60
  )
 
3
  import matplotlib.pyplot as plt
4
  import seaborn as sns
5
 
6
+ # Initialize the DataFrame in the session state if it doesn't exist
7
+ if 'expenses_df' not in st.session_state:
8
+ columns = ['Date', 'Category', 'Description', 'Amount']
9
+ st.session_state.expenses_df = pd.DataFrame(columns=columns)
10
 
11
  # Streamlit app layout
12
  st.title('Expense Tracker')
 
20
  amount = st.number_input("Amount", min_value=0.0, format="%.2f")
21
 
22
  if st.button("Add Expense"):
23
+ new_expense = pd.DataFrame([[date, category, description, amount]], columns=st.session_state.expenses_df.columns)
24
+ st.session_state.expenses_df = pd.concat([st.session_state.expenses_df, new_expense], ignore_index=True)
25
  st.success(f"Added expense: {description} - ${amount:.2f}")
26
 
27
  # Display all expenses
28
  st.subheader("Expenses List")
29
+ st.write(st.session_state.expenses_df)
30
 
31
  # Ensure 'Amount' is numeric (convert if necessary)
32
+ st.session_state.expenses_df['Amount'] = pd.to_numeric(st.session_state.expenses_df['Amount'], errors='coerce')
33
 
34
+ # Show category summary
35
  st.subheader("Category Summary")
36
+ category_summary = st.session_state.expenses_df.groupby('Category')['Amount'].sum().reset_index()
37
  st.write(category_summary)
38
 
39
+ # Generate individual bar and pie charts per Category (grouped by Description)
40
+ st.subheader("Bar Charts and Pie Charts per Category")
41
+
42
+ # Get all unique categories
43
+ unique_categories = st.session_state.expenses_df['Category'].unique()
44
+
45
+ # Loop through each unique category to plot bar and pie charts by description
46
+ for category in unique_categories:
47
+ category_df = st.session_state.expenses_df[st.session_state.expenses_df['Category'] == category]
48
+
49
+ # Bar chart: Expense distribution by description for this category
50
+ fig, ax = plt.subplots(figsize=(10, 6))
51
+ sns.barplot(x='Description', y='Amount', data=category_df, ax=ax)
52
+ ax.set_title(f'Total Expenses for Category: {category}')
53
+ ax.set_xlabel('Description')
54
+ ax.set_ylabel('Amount Spent ($)')
55
+ ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
56
+ st.pyplot(fig)
57
+
58
+ # Pie chart: Expense distribution by description for this category
59
+ fig, ax = plt.subplots(figsize=(8, 8))
60
+ description_summary = category_df.groupby('Description')['Amount'].sum().reset_index()
61
+ description_summary.set_index('Description')['Amount'].plot(kind='pie', autopct='%1.1f%%', legend=False, ax=ax)
62
+ ax.set_title(f'Expense Distribution for Category: {category}')
63
+ ax.set_ylabel('') # Hide ylabel
64
+ st.pyplot(fig)
65
+
66
+ # Create overall bar and pie chart for all categories
67
+
68
  # Bar chart for total expenses by category
69
+ st.subheader("Overall Expenses by Category")
70
  fig, ax = plt.subplots(figsize=(10, 6))
71
  sns.barplot(x='Category', y='Amount', data=category_summary, ax=ax)
72
+ ax.set_title('Total Expenses by Category (Overall)')
73
  ax.set_xlabel('Category')
74
  ax.set_ylabel('Amount Spent ($)')
75
  ax.set_xticklabels(ax.get_xticklabels(), rotation=45)
76
  st.pyplot(fig)
77
 
78
  # Pie chart for expense distribution by category
79
+ st.subheader("Overall Expense Distribution")
80
  fig, ax = plt.subplots(figsize=(8, 8))
81
  category_summary.set_index('Category')['Amount'].plot(kind='pie', autopct='%1.1f%%', legend=False, ax=ax)
82
+ ax.set_title('Expense Distribution by Category (Overall)')
83
  ax.set_ylabel('') # Hide ylabel
84
  st.pyplot(fig)
85
 
86
  # Save the expenses to a CSV file
87
  st.download_button(
88
  label="Download Expenses as CSV",
89
+ data=st.session_state.expenses_df.to_csv(index=False),
90
  file_name="expenses.csv",
91
  mime="text/csv"
92
  )