-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
155 lines (136 loc) · 4.78 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import streamlit as st
import sql_manager as sm
import pandas as pd
from streamlit_extras.bottom_container import bottom
st.set_page_config(
page_title="National Commission of Senior Citizens",
page_icon="./images/NCSC.png",
layout="wide",
# initial_sidebar_state="collapsed",
)
conn = sm.make_connection()
st.logo(image="./images/NCSC.png")
st.sidebar.header(
"National Commission of Senior Citizens", divider="rainbow", anchor=False
)
st.sidebar.page_link("pages/form.py", label="Registration Form", icon="📝")
table_name_mapping = {
"Senior": "senior",
"Education": "education",
"Health Concern": "healthconcern",
"Income": "income",
"Dependent": "dependent",
"School": "school",
}
def view_tables():
# Select a specific table
selected_table = st.sidebar.selectbox(
"Select a table",
table_name_mapping.keys(),
)
st.session_state.selected_table = selected_table
name_code_df = pd.read_sql_query("SELECT ReferenceCode, Name FROM senior", conn)
name_code_df["Display"] = (
name_code_df["Name"] + " (" + name_code_df["ReferenceCode"].astype(str) + ")"
)
senior_options = ["All"] + name_code_df[
"Display"
].tolist() # Prepend "all" to the list of options
selected_senior = st.sidebar.selectbox(
"Filter to show records for a specific senior",
senior_options,
index=0,
disabled=st.session_state.selected_table == "School",
)
if selected_senior != "All":
selected_code = name_code_df[name_code_df["Display"] == selected_senior][
"ReferenceCode"
].values[0]
st.session_state.selected_code = selected_code
else:
st.session_state.selected_code = None
# Display the table
if selected_table:
v_selected_table = table_name_mapping[selected_table]
st.write(f"### {selected_table}")
if st.session_state.selected_code and selected_table != "School":
df = pd.read_sql_query(
f"SELECT * FROM {v_selected_table} WHERE ReferenceCode = '{st.session_state.selected_code}'",
conn,
)
else:
df = pd.read_sql_query(f"SELECT * FROM {v_selected_table}", conn)
# Remove , from keys
if selected_table in ["Senior", "School"]:
df.iloc[:, 0] = df.iloc[:, 0].astype(str)
else:
df.iloc[:, :2] = df.iloc[:, :2].astype(str)
if selected_table == "Dependent":
colconfig = {
"DepIsChild": st.column_config.CheckboxColumn(
"Dependent is children of SC"
),
"DepIsWorking": st.column_config.CheckboxColumn("Dependent is working"),
}
else:
colconfig = {}
event = st.dataframe(
df,
use_container_width=True,
hide_index=True,
selection_mode="single-row",
on_select="rerun",
column_config=colconfig,
)
# st.markdown("### Selected Record")
sel = event.selection.rows
selected_row_df = df.iloc[sel]
if not selected_row_df.empty:
# For debugging
# st.dataframe(selected_row_df,
# use_container_width=True,
# hide_index=True,
# )
st.session_state.referencecode = (
selected_row_df["ReferenceCode"].values[0]
if selected_table != "School"
else selected_row_df["SchoolID"].values[0]
)
st.session_state.table_pk = selected_row_df.iloc[0].values[0]
# Navigation
st.sidebar.divider()
st.sidebar.markdown("### Operations")
st.sidebar.page_link(
"pages/add.py",
label="Create record on existing senior",
icon="➕",
)
if selected_table == "Senior":
st.sidebar.page_link(
"pages/read.py",
label="Read selected record",
icon="🔎",
disabled=selected_row_df.empty,
)
elif selected_table == "School":
st.sidebar.page_link(
"pages/add_school.py", label="Add new school", icon="🏫"
)
st.sidebar.page_link(
"pages/update.py",
label="Update selected record",
icon="✍️",
disabled=selected_row_df.empty,
)
st.sidebar.page_link(
"pages/delete.py",
label="Delete selected record",
icon="🗑️",
disabled=selected_row_df.empty,
)
st.sidebar.divider()
st.sidebar.page_link("pages/problems.py", label="SQL Query Problems", icon="🧪")
st.sidebar.page_link("pages/about.py", label="About", icon="❓")
with bottom():
st.info("This website is intended solely for academic purposes.", icon="📙")
view_tables()