Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,30 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
import seaborn as sns
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
| 7 |
# Define the path for reading CSV file
|
| 8 |
DATA_FILE = 'expenses.csv'
|
| 9 |
|
| 10 |
-
|
| 11 |
# Functions
|
| 12 |
-
|
| 13 |
def load_data():
|
| 14 |
'''
|
| 15 |
Reads the CSV file saved within HuggingFace storage. If not found, then creates a dataframe to be used for storing expense data
|
|
@@ -41,21 +56,6 @@ if 'file_processed' not in st.session_state:
|
|
| 41 |
st.session_state['file_processed'] = False
|
| 42 |
|
| 43 |
|
| 44 |
-
# Front End Design
|
| 45 |
-
st.title("Expense Tracker")
|
| 46 |
-
|
| 47 |
-
# Instructions and overview for the user
|
| 48 |
-
st.markdown("""
|
| 49 |
-
## Instructions
|
| 50 |
-
- Welcome! This is an app used to track your expenses.
|
| 51 |
-
- You can import data from a csv file for previous data or add each expense separately through the form on this app.
|
| 52 |
-
- There is a "Download Expenses as CSV" button. This is highly recommended, as the CSV will not be saved if the app sleeps or is restarted. You can import the data again afterwards.
|
| 53 |
-
- There is a "Visualise Data" button. This allows you to see graphs showing how you are spending your money.
|
| 54 |
-
""")
|
| 55 |
-
|
| 56 |
-
st.write("")
|
| 57 |
-
|
| 58 |
-
|
| 59 |
with st.form("expense_form"):
|
| 60 |
# Input for entering expense amount, with min value of 1 pence
|
| 61 |
amount = st.number_input("Amount", format="%0.2f", min_value=0.01)
|
|
@@ -92,6 +92,13 @@ with st.form("expense_form"):
|
|
| 92 |
|
| 93 |
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
# Creates two columns, so importing and exporting files are displayed side by side
|
| 96 |
col1, col2 = st.columns([2,1]) # col1 takes up twice the width of col2
|
| 97 |
|
|
@@ -133,6 +140,7 @@ with col1:
|
|
| 133 |
# Handle any errors in reading the CSV file
|
| 134 |
st.error(f"Error uploading file: {e}")
|
| 135 |
|
|
|
|
| 136 |
with col2:
|
| 137 |
# Adding empty spaces for formatting
|
| 138 |
st.write("")
|
|
@@ -150,9 +158,7 @@ with col2:
|
|
| 150 |
mime = "text/csv" # Specify the file type (MIME type) as CSV
|
| 151 |
)
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
|
| 157 |
# Check if there are any expenses in session state
|
| 158 |
if not st.session_state['expenses'].empty:
|
|
@@ -172,6 +178,9 @@ else:
|
|
| 172 |
st.write("No expenses yet.")
|
| 173 |
|
| 174 |
|
|
|
|
|
|
|
|
|
|
| 175 |
# Button to trigger data visualisation
|
| 176 |
viz_button = st.button("Visualise Data")
|
| 177 |
|
|
@@ -204,7 +213,6 @@ if viz_button:
|
|
| 204 |
# Show the plot
|
| 205 |
st.pyplot(fig)
|
| 206 |
|
| 207 |
-
|
| 208 |
|
| 209 |
# Display expenses over time
|
| 210 |
st.subheader("Expenses Over Time")
|
|
@@ -220,8 +228,31 @@ if viz_button:
|
|
| 220 |
|
| 221 |
date_group.set_index('Date', inplace=True)
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
st.line_chart(date_group['Amount'])
|
|
|
|
|
|
|
|
|
|
| 224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
else:
|
| 226 |
# Message displayed when there is no data to visualise
|
| 227 |
st.write("No data to visualise.")
|
|
|
|
| 1 |
+
# PART 1
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
| 4 |
import matplotlib.pyplot as plt
|
| 5 |
import seaborn as sns
|
| 6 |
|
| 7 |
+
# Front End Design
|
| 8 |
+
st.title("Expense Tracker")
|
| 9 |
+
|
| 10 |
+
# Instructions and overview for the user
|
| 11 |
+
st.markdown("""
|
| 12 |
+
## Instructions
|
| 13 |
+
- Welcome! This is an app used to track your expenses.
|
| 14 |
+
- You can import data from a csv file for previous data or add each expense separately through the form on this app.
|
| 15 |
+
- There is a "Download Expenses as CSV" button. This is highly recommended, as the CSV will not be saved if the app sleeps or is restarted. You can import the data again afterwards.
|
| 16 |
+
- There is a "Visualise Data" button. This allows you to see graphs showing how you are spending your money.
|
| 17 |
+
""")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
|
| 22 |
+
|
| 23 |
+
# PART 2
|
| 24 |
# Define the path for reading CSV file
|
| 25 |
DATA_FILE = 'expenses.csv'
|
| 26 |
|
|
|
|
| 27 |
# Functions
|
|
|
|
| 28 |
def load_data():
|
| 29 |
'''
|
| 30 |
Reads the CSV file saved within HuggingFace storage. If not found, then creates a dataframe to be used for storing expense data
|
|
|
|
| 56 |
st.session_state['file_processed'] = False
|
| 57 |
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
with st.form("expense_form"):
|
| 60 |
# Input for entering expense amount, with min value of 1 pence
|
| 61 |
amount = st.number_input("Amount", format="%0.2f", min_value=0.01)
|
|
|
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
# PART 5
|
| 98 |
+
|
| 99 |
+
if 'file_processed' not in st.session_state:
|
| 100 |
+
st.session_state['file_processed'] = False
|
| 101 |
+
|
| 102 |
# Creates two columns, so importing and exporting files are displayed side by side
|
| 103 |
col1, col2 = st.columns([2,1]) # col1 takes up twice the width of col2
|
| 104 |
|
|
|
|
| 140 |
# Handle any errors in reading the CSV file
|
| 141 |
st.error(f"Error uploading file: {e}")
|
| 142 |
|
| 143 |
+
# Part 4
|
| 144 |
with col2:
|
| 145 |
# Adding empty spaces for formatting
|
| 146 |
st.write("")
|
|
|
|
| 158 |
mime = "text/csv" # Specify the file type (MIME type) as CSV
|
| 159 |
)
|
| 160 |
|
| 161 |
+
|
|
|
|
|
|
|
| 162 |
|
| 163 |
# Check if there are any expenses in session state
|
| 164 |
if not st.session_state['expenses'].empty:
|
|
|
|
| 178 |
st.write("No expenses yet.")
|
| 179 |
|
| 180 |
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
# PART 3
|
| 184 |
# Button to trigger data visualisation
|
| 185 |
viz_button = st.button("Visualise Data")
|
| 186 |
|
|
|
|
| 213 |
# Show the plot
|
| 214 |
st.pyplot(fig)
|
| 215 |
|
|
|
|
| 216 |
|
| 217 |
# Display expenses over time
|
| 218 |
st.subheader("Expenses Over Time")
|
|
|
|
| 228 |
|
| 229 |
date_group.set_index('Date', inplace=True)
|
| 230 |
|
| 231 |
+
|
| 232 |
+
# Extract dates and expenses for plotting
|
| 233 |
+
#dates = date_group['Date']
|
| 234 |
+
#expenses = date_group['Amount']
|
| 235 |
+
|
| 236 |
st.line_chart(date_group['Amount'])
|
| 237 |
+
|
| 238 |
+
# Create a line plot
|
| 239 |
+
#fig, ax = plt.subplots()
|
| 240 |
|
| 241 |
+
# Plot the data
|
| 242 |
+
#ax.plot(dates, expenses, marker='o')
|
| 243 |
+
|
| 244 |
+
# Set plot title and labels
|
| 245 |
+
#ax.set_title('Expenses over Time')
|
| 246 |
+
#ax.set_ylabel('Amount')
|
| 247 |
+
#ax.set_xlabel('Date')
|
| 248 |
+
|
| 249 |
+
# Rotate x-axis labels for better readability
|
| 250 |
+
#plt.xticks(rotation=45)
|
| 251 |
+
|
| 252 |
+
# Show the plot
|
| 253 |
+
#st.pyplot(fig)
|
| 254 |
+
|
| 255 |
+
|
| 256 |
else:
|
| 257 |
# Message displayed when there is no data to visualise
|
| 258 |
st.write("No data to visualise.")
|