domdomingo commited on
Commit
0919209
1 Parent(s): f3cbe6f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import calendar # Core Python Module
2
+ from datetime import datetime # Core Python Module
3
+
4
+ import plotly.graph_objects as go # pip install plotly
5
+ import streamlit as st # pip install streamlit
6
+ from streamlit_option_menu import option_menu # pip install streamlit-option-menu
7
+
8
+ import database as db # local import
9
+
10
+ # # -------------- SETTINGS --------------
11
+ incomes = ["Salary", "Blog", "Other Income"]
12
+ expenses = ["Rent", "Utilities", "Groceries", "Car", "Other Expenses", "Saving"]
13
+ currency = "USD"
14
+ page_title = "Income and Expense Tracker"
15
+ page_icon = ":money_with_wings:" # emojis: https://www.webfx.com/tools/emoji-cheat-sheet/
16
+ layout = "centered"
17
+ # # --------------------------------------
18
+
19
+ st.set_page_config(page_title=page_title, page_icon=page_icon, layout=layout)
20
+ st.title(page_title + " " + page_icon)
21
+
22
+ # # --- DROP DOWN VALUES FOR SELECTING THE PERIOD ---
23
+ years = [datetime.today().year, datetime.today().year + 1]
24
+ months = list(calendar.month_name[1:])
25
+
26
+
27
+ # # # --- DATABASE INTERFACE --- Data Key: a0yqzckztju_L4gxuEg2cSQWAsaE8digGs8pgfhNTAdn
28
+ def get_all_periods():
29
+ items = db.fetch_all_periods()
30
+ periods = [item["key"] for item in items]
31
+ return periods
32
+
33
+
34
+ # # # --- HIDE STREAMLIT STYLE ---
35
+ hide_st_style = """
36
+ <style>
37
+ #MainMenu {visibility: hidden;}
38
+ footer {visibility: hidden;}
39
+ header {visibility: hidden;}
40
+ </style>
41
+ """
42
+ st.markdown(hide_st_style, unsafe_allow_html=True)
43
+
44
+ # # # --- NAVIGATION MENU ---
45
+ selected = option_menu(
46
+ menu_title=None,
47
+ options=["Data Entry", "Data Visualization"],
48
+ icons=["pencil-fill", "bar-chart-fill"], # https://icons.getbootstrap.com/
49
+ orientation="horizontal",
50
+ )
51
+
52
+ # # # --- INPUT & SAVE PERIODS ---
53
+ if selected == "Data Entry":
54
+ st.header(f"Data Entry in {currency}")
55
+ with st.form("entry_form", clear_on_submit=True):
56
+ col1, col2 = st.columns(2)
57
+ col1.selectbox("Select Month:", months, key="month")
58
+ col2.selectbox("Select Year:", years, key="year")
59
+
60
+ "---"
61
+ with st.expander("Income"):
62
+ for income in incomes:
63
+ st.number_input(f"{income}:", min_value=0, format="%i", step=10, key=income)
64
+ with st.expander("Expenses"):
65
+ for expense in expenses:
66
+ st.number_input(f"{expense}:", min_value=0, format="%i", step=10, key=expense)
67
+ with st.expander("Comment"):
68
+ comment = st.text_area("", placeholder="Enter a comment here ...")
69
+
70
+ "---"
71
+ submitted = st.form_submit_button("Save Data")
72
+ if submitted:
73
+ period = str(st.session_state["year"]) + "_" + str(st.session_state["month"])
74
+ incomes = {income: st.session_state[income] for income in incomes}
75
+ expenses = {expense: st.session_state[expense] for expense in expenses}
76
+ db.insert_period(period, incomes, expenses, comment)
77
+ st.success("Data saved!")
78
+
79
+
80
+ # # --- PLOT PERIODS ---
81
+ if selected == "Data Visualization":
82
+ st.header("Data Visualization")
83
+ with st.form("saved_periods"):
84
+ period = st.selectbox("Select Period:", get_all_periods())
85
+ submitted = st.form_submit_button("Plot Period")
86
+ if submitted:
87
+ # Get data from database
88
+ period_data = db.get_period(period)
89
+ comment = period_data.get("comment")
90
+ expenses = period_data.get("expenses")
91
+ incomes = period_data.get("incomes")
92
+
93
+ # Create metrics
94
+ total_income = sum(incomes.values())
95
+ total_expense = sum(expenses.values())
96
+ remaining_budget = total_income - total_expense
97
+ col1, col2, col3 = st.columns(3)
98
+ col1.metric("Total Income", f"{total_income} {currency}")
99
+ col2.metric("Total Expense", f"{total_expense} {currency}")
100
+ col3.metric("Remaining Budget", f"{remaining_budget} {currency}")
101
+ st.text(f"Comment: {comment}")
102
+
103
+ # # Create sankey chart
104
+ label = list(incomes.keys()) + ["Total Income"] + list(expenses.keys())
105
+ source = list(range(len(incomes))) + [len(incomes)] * len(expenses)
106
+ target = [len(incomes)] * len(incomes) + [label.index(expense) for expense in expenses.keys()]
107
+ value = list(incomes.values()) + list(expenses.values())
108
+
109
+ # # Data to dict, dict to sankey
110
+ link = dict(source=source, target=target, value=value)
111
+ node = dict(label=label, pad=20, thickness=30, color="#198AAF")
112
+ data = go.Sankey(link=link, node=node)
113
+
114
+ # # Plot it!
115
+ fig = go.Figure(data)
116
+ fig.update_layout(margin=dict(l=0, r=0, t=5, b=5))
117
+ st.plotly_chart(fig, use_container_width=True)