ClarzW commited on
Commit
71ed5f5
·
verified ·
1 Parent(s): 0a44ec0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -1
app.py CHANGED
@@ -3,12 +3,90 @@ import pandas as pd
3
  import matplotlib.pyplot as plt
4
  import seaborn as sns
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  st.title("Expense Tracker")
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
 
10
  # Save to CSV button
11
- # Read from CSV as temporary solution if no database (PostgreSQL, AWS)
 
 
12
 
13
 
14
 
 
3
  import matplotlib.pyplot as plt
4
  import seaborn as sns
5
 
6
+
7
+ def load_data():
8
+ try:
9
+ return pd.read_csv(DATA_FILE)
10
+ except FileNotFoundError:
11
+ return f"File not found, please try again."
12
+
13
+ def save_data(df):
14
+ df.to_csv(DATA_FILE, index=False)
15
+
16
+ # Initialize session state
17
+ if 'expenses' not in st.session_state:
18
+ st.session_state['expenses'] = load_data()
19
+
20
+ categories = ['Food', 'Transport', 'Bills', 'Entertainment', 'Other']
21
+
22
+
23
+
24
+
25
+ df = pd.DataFrame(columns=['Date', 'Category', 'Amount', 'Description'])
26
+
27
  st.title("Expense Tracker")
28
 
29
+ with st.form("expense_form"):
30
+ amount = st.number_input("Amount", format "%0.2f")
31
+ category = st.selectbox("Category", categories)
32
+ description = st.text_input("Description")
33
+ date = st.date_input("Date")
34
+
35
+ submit = st.form_submit_button("Add Expense")
36
+
37
+ if submitted:
38
+ new_expense = pd.DataFrame({
39
+ 'Date': [date],
40
+ 'Category': [category],
41
+ 'Amount': [amount],
42
+ 'Description': [description]
43
+ })
44
+ st.session_state['expenses'] = pd.concat([st.session_state['expenses'], new_expense], ignore_index=True)
45
+ save_data(st.session_state['expenses']) # Save data after adding a new expense, maybe better to have button for saving data?
46
+ st.success("Expense added successfully!")
47
+
48
+ if not st.session_state['expenses'].empty:
49
+ st.subheader("Current Expenses")
50
+ st.dataframe(st.session_state['expenses'])
51
+ else:
52
+ st.write("No expenses yet.")
53
+
54
+ if not st.session_state['expenses'].empty:
55
+ st.subheader("Expense Summary")
56
+ category_summary = st.session_state['expenses'].groupby('Category')['Amount'].sum().reset_index()
57
+
58
+ fig, ax = plt.subplots()
59
+ sns.barplot(x='Category', y='Amount', data=category_summary, ax=ax)
60
+ ax.set_title("Total Expenses by Category")
61
+ st.pyplot(fig)
62
+
63
+ st.subheader("Expenses Over Time")
64
+ st.line_chart(st.session_state['expenses'].set_index('Date')['Amount'])
65
+
66
+ csv = st.session_state['expenses'].to_csv(index=False).encode("utf-8")
67
+
68
+
69
+
70
+ st.download_button(
71
+ label = "Download Expenses as CSV",
72
+ data = csv,
73
+ file_name = "expenses.csv",
74
+ mime = "text/csv"
75
+ )
76
+
77
+
78
+ #import_button = st.button("Import CSV")
79
+ #vis_button = st.button("Visualise data")
80
+
81
+ # Line graph for money over time
82
+ # Pie chart for money by category
83
+ # 1 or 2 more graphs
84
 
85
 
86
  # Save to CSV button
87
+ # Read from CSV as temporary solution if no database (PostgreSQL, AWS)
88
+ # Read with input so that it doesn't have to be expense.csv
89
+ # Button for visualisations
90
 
91
 
92