-
Notifications
You must be signed in to change notification settings - Fork 1
/
Home.py
93 lines (75 loc) · 3.96 KB
/
Home.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# LIBRARIES ------------------------------
import streamlit as st
import streamlit_authenticator as stauth
from streamlit_gsheets import GSheetsConnection
from streamlit_option_menu import option_menu
import yaml
from yaml.loader import SafeLoader
import utils
# --- HOME PAGE ------------------------------
st.set_page_config(layout="wide")
st.title('Personal finance app')
st.text('Keep track of personal spending. Add transactions and visualize spending.\nBased on a private dataset in GoogleSheets.')
# Allow users to login
# Read and update data
# See monthly spending
# --- USER AUTHENTICATION --------------------
with open('config.yaml') as file:
config = yaml.load(file, Loader=SafeLoader)
authenticator = stauth.Authenticate(
config['credentials'],
config['cookie']['name'],
config['cookie']['key'],
config['cookie']['expiry_days'],
config['preauthorized']
)
name, authentication_status, username = authenticator.login()
st.session_state['auth_state'] = authentication_status
st.session_state['authenticator'] = authenticator
st.session_state['username'] = username
if authentication_status == False:
st.error('Username or password are incorrect')
elif authentication_status == None:
st.warning("Please enter your username and password.")
st.warning("If other use 'other' as username and 'abc123' as password to generate randomize data.")
elif authentication_status: # Successfull authentication
# --- INSIDE APP AFTER LOGIN -------------
# --- READ DATA FROM GOOGLE SHEETS ---------------------
# url = st.secrets["public_gsheets_url"] # Used if google sheets is public
conn = st.connection("gsheets", type=GSheetsConnection, ttl=1)
st.session_state['conn'] = conn # Save connection status to database in session state
# --- CHOOSE OPTION ------------------------------
selected = utils.sheet_menu()
gsheet, ncols, currency = utils.get_sheet_and_cols(selected)
if 'data' not in st.session_state: # In case the data was already read before
utils.read_data(st.session_state['conn'], st.session_state['username'], gsheet=gsheet, ncols=ncols) # Read the data from the selected sheet
if st.button('Reload data'): # Manually reading data
utils.read_data(st.session_state['conn'], st.session_state['username'], gsheet=gsheet, ncols=ncols)
st.success('Data loaded')
# --- MONTHLY SPENDING --------------------------------------
# See the spending of current and previous month
st.subheader('Monthly spending')
if username == 'other':
st.text('For user "other" the data is randomized')
col1, col2 = st.columns(2) # For filtering data to show
recurrent = col1.multiselect('Recurrent',[True,False], default=[True,False])
include = col2.multiselect('Include', [True,False], default=[True])
filtered = utils.monthly_total_spending(st.session_state['data'], currency, recurrent, include)
# Chart with total spending for all months
if filtered is not None: # filtered is None if there using ghseet "inversiones" -> no need for monthly plot
with st.expander('Monthly spending chart'):
utils.monthly_spending_plot(filtered,include,currency)
# --- SHOW RAW DATA --------------------------------------
with st.expander('Raw data'):
# Create copy of data, modify column types and sort to show first most recent transactions
edited_df = utils.show_raw_data(st.session_state['data'], st.session_state['gsheet'], currency)
if st.button('Update data'): # apply changes made in edited_df and update sessions_state df as well as gsheets database
if username=='other':
st.error('You are not authorized to update the data')
else:
with st.status('Updating data'):
utils.update_data(edited_df, st.session_state['gsheet'])
st.success('Data updated')
# --- SIDEBAR ---------------
st.sidebar.title(f'Welcome {name}')
authenticator.logout('Logout', 'sidebar')