Creating an expense tracker application can be an excellent beginner project for those new to Python and Streamlit. In this blog, I will walk you through the process of building an Expense Tracker App using Python and Streamlit. This app will allow you to add, save, load, and visualize your expenses.
Prerequisites
Before we begin, ensure you have the following installed:
- Python 3.x
- Streamlit
- Pandas
- Matplotlib
- Seaborn
You can install the required libraries using pip:
pip install streamlit pandas matplotlib seaborn
Project Overview
Our Expense Tracker App will have the following features:
Add Expenses: Users can add new expenses.
Save Expenses: Users can save the list of expenses to a CSV file.
Load Expenses: Users can load expenses from a CSV file.
Visualize Expenses: Users can visualize expenses using a bar chart.
Step-by-Step Implementation
Let’s dive into the code step by step.
Step 1: Importing Required Libraries
First, we need to import the necessary libraries:
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
Step 2: Initializing Session State
We will use Streamlit’s session state to store the expenses:
if 'expenses' not in st.session_state:
st.session_state.expenses = pd.DataFrame(columns=['Date', 'Category', 'Amount', 'Description'])
Step 3: Defining Functions
Next, we define the functions to add, load, save, and visualize expenses:
def add_expense(date, category, amount, description):
new_expense = pd.DataFrame([[date, category, amount, description]], columns=st.session_state.expenses.columns)
st.session_state.expenses = pd.concat([st.session_state.expenses, new_expense], ignore_index=True)
def load_expenses():
uploaded_file = st.file_uploader("Choose a file", type=['csv'])
if uploaded_file is not None:
st.session_state.expenses = pd.read_csv(uploaded_file)
def save_expenses():
st.session_state.expenses.to_csv('expenses.csv', index=False)
st.success("Expenses saved successfully")
def visualize_expenses():
if not st.session_state.expenses.empty:
fig, ax = plt.subplots()
sns.barplot(data=st.session_state.expenses, x='Category', y="Amount", ax=ax)
plt.xticks(rotation=45)
st.pyplot(fig)
else:
st.warning("No Expenses to Visualize!")
Step 4: Building the Streamlit Interface
Now, let’s create the Streamlit interface:
st.title("DevDuniya Expense Tracker")
with st.sidebar:
st.header('Add Expense')
date = st.date_input("Date")
category = st.selectbox('Category', ['Food', 'Transport', 'Entertainment', 'Utilities', 'Other'])
amount = st.number_input('Amount', min_value=0.0, format="%.2f")
description = st.text_input('Description')
if st.button('Add'):
add_expense(date, category, amount, description)
st.success("Expense Added!")
st.header('File Operations')
if st.button('Save Expenses'):
save_expenses()
if st.button('Load Expenses'):
load_expenses()
st.header('Expenses')
st.write(st.session_state.expenses)
st.header('Visualization')
if st.button('Visualize Expenses'):
visualize_expenses()
Complete Code:
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Initialize the expense dataframe
if 'expenses' not in st.session_state:
st.session_state.expenses = pd.DataFrame(columns=['Date', 'Category', 'Amount', 'Description'])
def add_expense(date, category, amount, description):
new_expense = pd.DataFrame([[date, category, amount, description]], columns=st.session_state.expenses.columns)
st.session_state.expenses = pd.concat([st.session_state.expenses, new_expense], ignore_index=True)
def load_expenses():
uploaded_file = st.file_uploader("Choose a file", type=['csv'])
if uploaded_file is not None:
st.session_state.expenses = pd.read_csv(uploaded_file)
def save_expenses():
st.session_state.expenses.to_csv('expenses.csv', index=False)
st.success("Expenses saved successfully!")
def visualize_expenses():
if not st.session_state.expenses.empty:
fig, ax = plt.subplots()
sns.barplot(data=st.session_state.expenses, x='Category', y='Amount', ax=ax)
plt.xticks(rotation=45)
st.pyplot(fig)
else:
st.warning("No expenses to visualize!")
st.title('DevDuniya Expense Tracker')
with st.sidebar:
st.header('Add Expense')
date = st.date_input('Date')
category = st.selectbox('Category', ['Food', 'Transport', 'Entertainment', 'Utilities', 'Other'])
amount = st.number_input('Amount', min_value=0.0, format="%.2f")
description = st.text_input('Description')
if st.button('Add'):
add_expense(date, category, amount, description)
st.success('Expense added!')
st.header('File Operations')
if st.button('Save Expenses'):
save_expenses()
if st.button('Load Expenses'):
load_expenses()
st.header('Expenses')
st.write(st.session_state.expenses)
st.header('Visualization')
if st.button('Visualize Expenses'):
visualize_expenses()
Conclusion:
In this blog, we created a simple Expense Tracker App using Python and Streamlit. This app allows you to add, save, load, and visualize your expenses easily. You can further enhance this app by.
If you have any queries related to this article, then you can ask in the comment section, we will contact you soon, and Thank you for reading this article.
Follow me to receive more useful content:
Instagram | Twitter | Linkedin | Youtube
Thank you