Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhanced error handling, improved argument parsing, and optimized code readability #61

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
103 changes: 41 additions & 62 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,55 @@
import pandas as pd
import streamlit as st

#Page Setup
# 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",
page_title="MOEST Exam Center Calculator",
page_icon=":school:",
layout="wide",
initial_sidebar_state="expanded",
)

#Sidebar
# Sidebar
with st.sidebar:
st.title("Random Center Calculator")
schools_file = st.file_uploader("Upload School/College file", type="tsv")
centers_file = st.file_uploader("Upload Centers file", type="tsv")
prefs_file = st.file_uploader("Upload Preferences file", type="tsv")
num_centers = st.number_input("Number of Centers", min_value=1, value=5, step=1)

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)
calculate = st.button("Calculate Centers", help="Calculate the exam centers")

# 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="ℹ️")
tabs = st.columns(5)

if centers_file:
df = pd.read_csv(centers_file, sep="\t")
tab4.dataframe(df)
else:
tab4.info("Upload data to view it.", icon="ℹ️")
with tabs[0]:
st.subheader("School Center")

if prefs_file:
df = pd.read_csv(prefs_file, sep="\t")
tab5.dataframe(df)
else:
tab5.info("Upload data to view it.", icon="ℹ️")
with tabs[1]:
st.subheader("School Center Distance")

with tabs[2]:
st.subheader("School Data")

with tabs[3]:
st.subheader("Center Data")

with tabs[4]:
st.subheader("Pref Data")

# 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}"
def run_center_randomizer(schools_tsv, centers_tsv, prefs_tsv, num_centers):
cmd = f"python school_center.py --schools {schools_tsv} --centers {centers_tsv} --prefs {prefs_tsv} --num_centers {num_centers}"
subprocess.run(cmd, shell=True)

# Display uploaded data
def display_uploaded_data(file, tab):
if file:
df = pd.read_csv(file, sep="\t")
tab.dataframe(df)
else:
tab.info("Upload data to view it.", icon="ℹ️")

# Run logic after the button is clicked
if calculate:

Expand All @@ -81,7 +69,7 @@ def save_file_to_temp(file_obj):
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)
run_center_randomizer(schools_path, centers_path, prefs_path, num_centers)

# Set the paths for the output files
school_center_file = "results/school-center.tsv"
Expand All @@ -93,22 +81,13 @@ def save_file_to_temp(file_obj):
os.unlink(prefs_path)

# Display data in the specified tabs
if school_center_file:
df = pd.read_csv(school_center_file, sep="\t")
tab1.dataframe(df)
else:
tab1.error("School Center file not found.")

if school_center_distance_file:
df = pd.read_csv(school_center_distance_file, sep="\t")
tab2.dataframe(df)
else:
tab2.error("School Center Distance file not found.")
display_uploaded_data(school_center_file, tabs[0])
display_uploaded_data(school_center_distance_file, tabs[1])

st.toast("Calculation successful!", icon="🎉")
st.sidebar.success("Calculation successful!")

else:
st.sidebar.error("Please upload all required files.", icon="🚨")
else:
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="ℹ️")
tabs[0].info("Results will be shown only after the calculation is completed.", icon="ℹ️")
tabs[1].info("Results will be shown only after the calculation is completed.", icon="ℹ️")