forked from moest-np/center-randomize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
181 lines (152 loc) · 6.88 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import os
import folium
import tempfile
import subprocess
import pandas as pd
import streamlit as st
from streamlit_folium import st_folium
#Page Setup
st.set_page_config(
page_title="MOEST Exam Center Calculator",
page_icon=":school:",
# page_icon="https://avatars.githubusercontent.com/u/167545222?s=200&v=4", # official logo
layout="wide",
initial_sidebar_state="expanded",
)
# Session setup
if 'calculate_clicked' not in st.session_state:
st.session_state.calculate_clicked = False
if 'calculation_completed' not in st.session_state:
st.session_state.calculation_completed = False
if 'calculated_data' not in st.session_state:
st.session_state.calculated_data = {}
if 'filter_type' not in st.session_state:
st.session_state.filter_type = None
if 'filter_value' not in st.session_state:
st.session_state.filter_value = None
#Maps setup
m = folium.Map(location=[27.7007, 85.3001], zoom_start=12, )
fg = folium.FeatureGroup(name="Allocated Centers")
#Sidebar
with st.sidebar:
add_side_header = st.sidebar.title("Random Center Calculator")
schools_file = st.sidebar.file_uploader("Upload School/College file", type="tsv")
centers_file = st.sidebar.file_uploader("Upload Centers file", type="tsv")
prefs_file = st.sidebar.file_uploader("Upload Preferences file", type="tsv")
calculate = st.sidebar.button("Calculate Centers", type="primary", use_container_width=True)
# Tabs
tab1, tab2, tab3, tab4, tab5 = st.tabs([
"School Center",
"School Center Distance",
"View School Data",
"View Centers Data",
"View Pref Data"
])
tab1.subheader("School Center")
tab2.subheader("School Center Distance")
tab3.subheader("School Data")
tab4.subheader("Center Data")
tab5.subheader("Pref Data")
# Show data in Tabs as soon as the files are uploaded
if schools_file:
df = pd.read_csv(schools_file, sep="\t")
tab3.dataframe(df)
else:
tab3.info("Upload data to view it.", icon="ℹ️")
if centers_file:
df = pd.read_csv(centers_file, sep="\t")
tab4.dataframe(df)
else:
tab4.info("Upload data to view it.", icon="ℹ️")
if prefs_file:
df = pd.read_csv(prefs_file, sep="\t")
tab5.dataframe(df)
else:
tab5.info("Upload data to view it.", icon="ℹ️")
# Function to run the center randomizer program
def run_center_randomizer(schools_tsv, centers_tsv, prefs_tsv):
cmd = f"python school_center.py {schools_tsv} {centers_tsv} {prefs_tsv}"
subprocess.run(cmd, shell=True)
#Function to filter the data
def filter_data(df, filter_type, filter_value):
if filter_type in df.columns:
df = df[df[filter_type] == filter_value]
return df
else:
st.error(f"{filter_type} is not a valid column in the DataFrame.")
return pd.DataFrame()
# Run logic after the button is clicked
if calculate:
st.session_state.calculate_clicked = True
def save_file_to_temp(file_obj):
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
file_obj.seek(0) # Go to the start of the file
temp_file.write(file_obj.read())
return temp_file.name
if st.session_state.calculate_clicked:
# Ensure all files are uploaded
if schools_file and centers_file and prefs_file:
schools_path = save_file_to_temp(schools_file)
centers_path = save_file_to_temp(centers_file)
prefs_path = save_file_to_temp(prefs_file)
# Run the program with the temporary file paths
run_center_randomizer(schools_path, centers_path, prefs_path)
st.toast("Calculation successful!", icon="🎉")
st.session_state.calculation_completed = True
# Delete the temporary files after use
os.unlink(schools_path)
os.unlink(centers_path)
os.unlink(prefs_path)
# Set the paths for the output files[UPDATE LATER TO BE FLEXIBLE]
school_center_file = "results/school-center.tsv"
school_center_distance_file = "results/school-center-distance.tsv"
# Store calculated data in session state
if school_center_file and school_center_distance_file:
st.session_state.calculated_data['school_center'] = school_center_file
st.session_state.calculated_data['school_center_distance'] = school_center_distance_file
else:
tab1.error("Calculated data not found", icon="🚨")
else:
st.sidebar.error("Please upload all required files.", icon="🚨")
elif not st.session_state.calculate_clicked:
tab1_msg = tab1.info("Results will be shown only after the calculation is completed.", icon="ℹ️")
tab2_msg = tab2.info("Results will be shown only after the calculation is completed.", icon="ℹ️")
if st.session_state.calculate_clicked and st.session_state.calculation_completed:
# Display data from session state
if 'school_center' in st.session_state.calculated_data:
df_school_center = pd.read_csv(st.session_state.calculated_data['school_center'], sep="\t")
allowed_filter_types = ['scode', 'school', 'cscode', 'center']
st.session_state.filter_type = tab1.radio("Choose a filter type:", allowed_filter_types)
# Display an input field based on the selected filter type
if st.session_state.filter_type:
st.session_state.filter_value = tab1.selectbox(f"Select a value for {st.session_state.filter_type}:", df_school_center[st.session_state.filter_type].unique())
# Filter the DataFrame based on the selected filter type and value
if st.session_state.filter_value:
filtered_df = filter_data(df_school_center, st.session_state.filter_type, st.session_state.filter_value)
tab1.dataframe(filtered_df)
tab1.subheader('Map')
tab1.divider()
for index, center in filtered_df.iterrows():
fg.add_child(
folium.Marker(
location=[center.center_lat, center.center_long],
popup=f"{(center.center).title()}\nAllocation: {center.allocation}",
tooltip=f"{center.center}",
icon=folium.Icon(color="red")
)
)
m.add_child(fg)
with tab1:
st_folium( m, width=1200, height=400 )
tab1.divider()
tab1.subheader('All Data')
tab1.dataframe(df_school_center)
else:
tab1.info("No calculated data available.", icon="ℹ️")
if 'school_center_distance' in st.session_state.calculated_data:
df = pd.read_csv(st.session_state.calculated_data['school_center_distance'], sep="\t")
tab2.dataframe(df)
else:
tab2.error("School Center Distance file not found.")
elif st.session_state.calculate_clicked and not st.session_state.calculated_data:
tab1.error("School Center data not found in session state.")