Skip to content

Commit ddb6fc4

Browse files
authored
Add streamlit app for UI (#44)
1 parent 143490f commit ddb6fc4

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

app.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import os
2+
import tempfile
3+
import subprocess
4+
import pandas as pd
5+
import streamlit as st
6+
7+
#Page Setup
8+
st.set_page_config(
9+
page_title="MOEST Exam Center Calculator",
10+
page_icon=":school:",
11+
# page_icon="https://avatars.githubusercontent.com/u/167545222?s=200&v=4", # official logo
12+
layout="wide",
13+
initial_sidebar_state="expanded",
14+
)
15+
16+
#Sidebar
17+
with st.sidebar:
18+
19+
add_side_header = st.sidebar.title(
20+
"Random Center Calculator"
21+
)
22+
schools_file = st.sidebar.file_uploader("Upload School/College file", type="tsv")
23+
centers_file = st.sidebar.file_uploader("Upload Centers file", type="tsv")
24+
prefs_file = st.sidebar.file_uploader("Upload Preferences file", type="tsv")
25+
26+
calculate = st.sidebar.button("Calculate Centers", type="primary", use_container_width=True)
27+
28+
# Tabs
29+
tab1, tab2, tab3, tab4, tab5 = st.tabs([
30+
"School Center",
31+
"School Center Distance",
32+
"View School Data",
33+
"View Centers Data",
34+
"View Pref Data"
35+
])
36+
37+
tab1.subheader("School Center")
38+
tab2.subheader("School Center Distance")
39+
tab3.subheader("School Data")
40+
tab4.subheader("Center Data")
41+
tab5.subheader("Pref Data")
42+
43+
# Show data in Tabs as soon as the files are uploaded
44+
if schools_file:
45+
df = pd.read_csv(schools_file, sep="\t")
46+
tab3.dataframe(df)
47+
else:
48+
tab3.info("Upload data to view it.", icon="ℹ️")
49+
50+
if centers_file:
51+
df = pd.read_csv(centers_file, sep="\t")
52+
tab4.dataframe(df)
53+
else:
54+
tab4.info("Upload data to view it.", icon="ℹ️")
55+
56+
if prefs_file:
57+
df = pd.read_csv(prefs_file, sep="\t")
58+
tab5.dataframe(df)
59+
else:
60+
tab5.info("Upload data to view it.", icon="ℹ️")
61+
62+
63+
# Function to run the center randomizer program
64+
def run_center_randomizer(schools_tsv, centers_tsv, prefs_tsv):
65+
cmd = f"python school_center.py {schools_tsv} {centers_tsv} {prefs_tsv}"
66+
subprocess.run(cmd, shell=True)
67+
68+
# Run logic after the button is clicked
69+
if calculate:
70+
71+
def save_file_to_temp(file_obj):
72+
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
73+
file_obj.seek(0) # Go to the start of the file
74+
temp_file.write(file_obj.read())
75+
return temp_file.name
76+
77+
# Ensure all files are uploaded
78+
if schools_file and centers_file and prefs_file:
79+
schools_path = save_file_to_temp(schools_file)
80+
centers_path = save_file_to_temp(centers_file)
81+
prefs_path = save_file_to_temp(prefs_file)
82+
83+
# Run the program with the temporary file paths
84+
run_center_randomizer(schools_path, centers_path, prefs_path)
85+
86+
# Set the paths for the output files
87+
school_center_file = "results/school-center.tsv"
88+
school_center_distance_file = "results/school-center-distance.tsv"
89+
90+
# Delete the temporary files after use
91+
os.unlink(schools_path)
92+
os.unlink(centers_path)
93+
os.unlink(prefs_path)
94+
95+
# Display data in the specified tabs
96+
if school_center_file:
97+
df = pd.read_csv(school_center_file, sep="\t")
98+
tab1.dataframe(df)
99+
else:
100+
tab1.error("School Center file not found.")
101+
102+
if school_center_distance_file:
103+
df = pd.read_csv(school_center_distance_file, sep="\t")
104+
tab2.dataframe(df)
105+
else:
106+
tab2.error("School Center Distance file not found.")
107+
108+
st.toast("Calculation successful!", icon="🎉")
109+
110+
else:
111+
st.sidebar.error("Please upload all required files.", icon="🚨")
112+
else:
113+
tab1_msg = tab1.info("Results will be shown only after the calculation is completed.", icon="ℹ️")
114+
tab2_msg = tab2.info("Results will be shown only after the calculation is completed.", icon="ℹ️")

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ pytest-clarity==1.0.1
44
pytest-sugar==1.0.0
55
pre-commit==3.6.0
66
pytest-cov==5.0.0
7+
streamlit==1.33.0

0 commit comments

Comments
 (0)