-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
73 lines (61 loc) · 2.24 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import streamlit as st
import pandas as pd
import os
# Define the name of the CSV file to store the data
CSV_FILE_NAME = "spend_data.csv"
# Define the columns for the CSV file
CSV_COLUMNS = ["Item", "Amount"]
# Check if the CSV file exists, if not create it
if not os.path.isfile(CSV_FILE_NAME):
with open(CSV_FILE_NAME, "w") as file:
file.write(",".join(CSV_COLUMNS) + "\n")
# Define a function to append data to the CSV file
def append_data_to_csv_file(item, amount):
with open(CSV_FILE_NAME, "a") as file:
file.write(item + "," + str(amount) + "\n")
# Define a function to read the CSV file and return a DataFrame
def read_csv_file():
return pd.read_csv(CSV_FILE_NAME)
def clear_csv_file():
if os.path.isfile(CSV_FILE_NAME):
os.remove(CSV_FILE_NAME)
with open(CSV_FILE_NAME, "w") as file:
file.write(",".join(CSV_COLUMNS) + "\n")
# Define the Streamlit app
def app():
# Set the title of the app
st.title("Spend Analyzer")
st.markdown(
f"""
<style>
.stApp {{
background-image: url("https://www.pixelstalk.net/wp-content/uploads/images6/Dark-Blue-Background-Desktop.jpg");
background-attachment: fixed;
background-size: cover
}}
</style>
""",
unsafe_allow_html=True
)
# Create a form for user input
form = st.form(key="spend_form")
# Add form fields for item and amount
item = form.text_input("Item")
amount = form.number_input("Amount", min_value=0.01, step=0.01)
# Add a submit button to the form
submitted = form.form_submit_button("Submit")
# If the form is submitted, append the data to the CSV file
if submitted:
append_data_to_csv_file(item, amount)
# Read the CSV file and display the spend data as a table
spend_data = read_csv_file()
st.table(spend_data)
# Display a spend chart based on the spend data
spend_chart_data = spend_data.groupby("Item")["Amount"].sum()
st.bar_chart(spend_chart_data)
# Add a button to clear all contents of the CSV file
if st.button("Clear All"):
clear_csv_file()
st.success("All contents of the CSV file have been cleared.")
if __name__=="__main__":
app()