Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +165 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,167 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import base64
|
| 6 |
+
|
| 7 |
+
# PAGE CONFIGURATION
|
| 8 |
+
|
| 9 |
+
st.set_page_config(page_title="Telangana Minorities Residential Educational Institutions Society", layout="wide")
|
| 10 |
+
|
| 11 |
+
# Custom CSS for UI
|
| 12 |
+
st.markdown("""
|
| 13 |
+
<style>
|
| 14 |
+
.big-font {font-size:20px !important; font-weight:600;}
|
| 15 |
+
.kpi-box {background-color:#f9f9f9; padding:15px; border-radius:10px; text-align:center; border: 1px solid #ddd;}
|
| 16 |
+
</style>
|
| 17 |
+
""", unsafe_allow_html=True)
|
| 18 |
+
|
| 19 |
+
# DATA UPLOAD
|
| 20 |
+
|
| 21 |
+
st.title("Telangana Minorities Residential Educational Institutions Society Analysis Dashboard")
|
| 22 |
+
uploaded_file = st.file_uploader("Upload your dataset (Excel or CSV)", type=["xlsx", "csv"])
|
| 23 |
+
|
| 24 |
+
if uploaded_file:
|
| 25 |
+
if uploaded_file.name.endswith(".xlsx"):
|
| 26 |
+
df = pd.read_excel(uploaded_file)
|
| 27 |
+
else:
|
| 28 |
+
df = pd.read_csv(uploaded_file)
|
| 29 |
+
|
| 30 |
+
st.success("Dataset uploaded successfully!")
|
| 31 |
+
|
| 32 |
+
# Standardize column names
|
| 33 |
+
df.columns = df.columns.str.strip().str.lower().str.replace(" ", "_")
|
| 34 |
+
df.fillna(0, inplace=True)
|
| 35 |
+
|
| 36 |
+
# DYNAMIC COLUMN DETECTION
|
| 37 |
+
|
| 38 |
+
def find_column(keyword):
|
| 39 |
+
for col in df.columns:
|
| 40 |
+
if keyword in col:
|
| 41 |
+
return col
|
| 42 |
+
return None
|
| 43 |
+
|
| 44 |
+
columns = {
|
| 45 |
+
"v_minority": {
|
| 46 |
+
"sanctioned": find_column("vth_class_minority_sanction"),
|
| 47 |
+
"admitted": find_column("vth_class_minority_admitted"),
|
| 48 |
+
"vacancy": find_column("vth_class_minority_vacancies"),
|
| 49 |
+
"attendance": find_column("total_school_attendance"),
|
| 50 |
+
},
|
| 51 |
+
"v_non_minority": {
|
| 52 |
+
"sanctioned": find_column("vth_class_non_minority_sanction"),
|
| 53 |
+
"admitted": find_column("vth_class_non_minority_admitted"),
|
| 54 |
+
"vacancy": find_column("vth_class_non_minority_vacancies"),
|
| 55 |
+
"attendance": find_column("total_school_attendance"),
|
| 56 |
+
},
|
| 57 |
+
"inter_minority": {
|
| 58 |
+
"sanctioned": find_column("1st_year_minority_sanction"),
|
| 59 |
+
"admitted": find_column("1st_year_minority_admitted"),
|
| 60 |
+
"vacancy": find_column("1st_year_minority_vacancies"),
|
| 61 |
+
"attendance": find_column("total_intermediate_attendance"),
|
| 62 |
+
},
|
| 63 |
+
"inter_non_minority": {
|
| 64 |
+
"sanctioned": find_column("1st_year_non_minority_sanction"),
|
| 65 |
+
"admitted": find_column("1st_year_non_minority_admitted"),
|
| 66 |
+
"vacancy": find_column("1st_year_non_minority_vacancies"),
|
| 67 |
+
"attendance": find_column("total_intermediate_attendance"),
|
| 68 |
+
},
|
| 69 |
+
"absentees": find_column("total_absentees"),
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
# FILTERS
|
| 73 |
+
|
| 74 |
+
st.sidebar.header("Filters")
|
| 75 |
+
level_filter = st.sidebar.radio("Select Level", ["V (School)", "Inter 1st Year"])
|
| 76 |
+
category_filter = st.sidebar.radio("Select Category", ["Minority", "Non-Minority"])
|
| 77 |
+
districts = st.sidebar.multiselect("Select District(s)", options=df["district"].unique(), default=df["district"].unique())
|
| 78 |
+
search_college = st.sidebar.text_input("Search College Name (Optional)")
|
| 79 |
+
|
| 80 |
+
df = df[df["district"].isin(districts)]
|
| 81 |
+
if search_college:
|
| 82 |
+
df = df[df["college_name"].str.contains(search_college, case=False, na=False)]
|
| 83 |
+
|
| 84 |
+
# Map columns dynamically
|
| 85 |
+
if level_filter == "V (School)":
|
| 86 |
+
key = "v_minority" if category_filter == "Minority" else "v_non_minority"
|
| 87 |
+
else:
|
| 88 |
+
key = "inter_minority" if category_filter == "Minority" else "inter_non_minority"
|
| 89 |
+
|
| 90 |
+
sanctioned_col = columns[key]["sanctioned"]
|
| 91 |
+
admitted_col = columns[key]["admitted"]
|
| 92 |
+
vacant_col = columns[key]["vacancy"]
|
| 93 |
+
attendance_col = columns[key]["attendance"]
|
| 94 |
+
absentees_col = columns["absentees"]
|
| 95 |
+
|
| 96 |
+
# KPI CALCULATIONS
|
| 97 |
+
|
| 98 |
+
total_sanctioned = df[sanctioned_col].sum()
|
| 99 |
+
total_admitted = df[admitted_col].sum()
|
| 100 |
+
total_vacant = df[vacant_col].sum()
|
| 101 |
+
total_attendance = df[attendance_col].sum()
|
| 102 |
+
total_absentees = df[absentees_col].sum()
|
| 103 |
+
attendance_pct = round((total_attendance / (total_attendance + total_absentees)) * 100, 2) if (total_attendance + total_absentees) > 0 else 0
|
| 104 |
+
|
| 105 |
+
# KPI DISPLAY
|
| 106 |
+
col1, col2, col3, col4, col5, col6 = st.columns(6)
|
| 107 |
+
col1.markdown(f"<div class='kpi-box'>π« <br><span class='big-font'>{total_sanctioned:,}</span><br>Total Sanctioned</div>", unsafe_allow_html=True)
|
| 108 |
+
col2.markdown(f"<div class='kpi-box'>π <br><span class='big-font'>{total_admitted:,}</span><br>Total Admitted</div>", unsafe_allow_html=True)
|
| 109 |
+
col3.markdown(f"<div class='kpi-box'>π <br><span class='big-font'>{total_vacant:,}</span><br>Total Vacant</div>", unsafe_allow_html=True)
|
| 110 |
+
col4.markdown(f"<div class='kpi-box'>β
<br><span class='big-font'>{total_attendance:,}</span><br>Total Attendance</div>", unsafe_allow_html=True)
|
| 111 |
+
col5.markdown(f"<div class='kpi-box'>β <br><span class='big-font'>{total_absentees:,}</span><br>Total Absentees</div>", unsafe_allow_html=True)
|
| 112 |
+
col6.markdown(f"<div class='kpi-box'>π <br><span class='big-font'>{attendance_pct}%</span><br>Attendance %</div>", unsafe_allow_html=True)
|
| 113 |
+
|
| 114 |
+
st.markdown("---")
|
| 115 |
+
|
| 116 |
+
# VISUALIZATIONS
|
| 117 |
+
df["admission_rate"] = (df[admitted_col] / df[sanctioned_col]) * 100
|
| 118 |
+
df["vacancy_rate"] = (df[vacant_col] / df[sanctioned_col]) * 100
|
| 119 |
+
df["attendance_%"] = (df[attendance_col] / (df[attendance_col] + df[absentees_col])) * 100
|
| 120 |
+
|
| 121 |
+
# 1. Vacancy Rate vs Admission Rate (Bubble)
|
| 122 |
+
st.plotly_chart(px.scatter(df, x="admission_rate", y="vacancy_rate", size="admission_rate",
|
| 123 |
+
color=attendance_col, hover_name="college_name",
|
| 124 |
+
title="Vacancy Rate vs Admission Rate"), use_container_width=True)
|
| 125 |
+
|
| 126 |
+
# 2. Attendance Impact on Vacancies
|
| 127 |
+
st.plotly_chart(px.scatter(df, x=attendance_col, y="vacancy_rate", title="Attendance Impact on Vacancy Rate", trendline="ols"), use_container_width=True)
|
| 128 |
+
|
| 129 |
+
# 3. Top 10 Districts by Absenteeism
|
| 130 |
+
absentee_summary = df.groupby("district")[absentees_col].sum().reset_index().sort_values(absentees_col, ascending=False).head(10)
|
| 131 |
+
st.plotly_chart(px.bar(absentee_summary, x="district", y=absentees_col, title="Top 10 Districts by Absenteeism"), use_container_width=True)
|
| 132 |
+
|
| 133 |
+
# 4. Vacancy Rate Distribution
|
| 134 |
+
st.plotly_chart(px.box(df, y="vacancy_rate", x="district", title="Vacancy Rate Distribution by District"), use_container_width=True)
|
| 135 |
+
|
| 136 |
+
# 5. Sanction vs Admission
|
| 137 |
+
stacked = df.groupby("district")[[sanctioned_col, admitted_col]].sum().reset_index()
|
| 138 |
+
stacked = stacked.melt(id_vars="district", value_vars=[sanctioned_col, admitted_col], var_name="Type", value_name="Seats")
|
| 139 |
+
st.plotly_chart(px.bar(stacked, x="district", y="Seats", color="Type", title="Sanctioned vs Admitted Seats"), use_container_width=True)
|
| 140 |
+
|
| 141 |
+
# 6. Top & Bottom 5 Institutes
|
| 142 |
+
top_5 = df.nlargest(5, "attendance_%")[["college_name", "attendance_%"]]
|
| 143 |
+
bottom_5 = df.nsmallest(5, "attendance_%")[["college_name", "attendance_%"]]
|
| 144 |
+
col1, col2 = st.columns(2)
|
| 145 |
+
with col1:
|
| 146 |
+
st.subheader("Top 5 Institutes (Attendance %)")
|
| 147 |
+
st.dataframe(top_5)
|
| 148 |
+
with col2:
|
| 149 |
+
st.subheader("Bottom 5 Institutes (Attendance %)")
|
| 150 |
+
st.dataframe(bottom_5)
|
| 151 |
+
|
| 152 |
+
# DRILL-DOWN: SELECT DISTRICT
|
| 153 |
+
selected_district = st.selectbox("Search for a District for Detailed Institute View", df["district"].unique())
|
| 154 |
+
drill_df = df[df["district"] == selected_district][["college_name", sanctioned_col, admitted_col, vacant_col, attendance_col, absentees_col, "attendance_%"]]
|
| 155 |
+
st.dataframe(drill_df)
|
| 156 |
+
|
| 157 |
+
# DOWNLOAD DATA
|
| 158 |
+
def download_excel(dataframe):
|
| 159 |
+
output = BytesIO()
|
| 160 |
+
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
|
| 161 |
+
dataframe.to_excel(writer, index=False, sheet_name="Report")
|
| 162 |
+
return output.getvalue()
|
| 163 |
+
|
| 164 |
+
st.download_button("π₯ Download Excel Report", data=download_excel(df), file_name="education_dashboard_report.xlsx")
|
| 165 |
|
| 166 |
+
else:
|
| 167 |
+
st.warning("Upload the merged dataset to start the analysis.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|