ECDDemo / app.py
Nagaraj81's picture
Update app.py
65bedf1 verified
import os
import pandas as pd
import numpy as np
import plotly.express as px
import plotly.graph_objects as go
import folium
import gradio as gr
from folium.plugins import MarkerCluster
def generate_nursery_data(num=5500):
np.random.seed(42)
governorates = ["Cairo", "Alexandria", "Giza", "Aswan", "Luxor", "Dakahlia", "Sharqia", "Qalyubia",
"Kafr El Sheikh", "Gharbia", "Menoufia", "Beheira", "Ismailia", "Suez", "Port Said", "Damietta",
"Fayoum", "Beni Suef", "Minya", "Assiut", "Sohag", "Qena", "Red Sea", "New Valley", "Matruh",
"North Sinai", "South Sinai"]
df = pd.DataFrame()
df["Governorate"] = np.random.choice(governorates, num)
df["Licensed"] = np.random.choice(["Licensed", "Unlicensed"], num, p=[0.62, 0.38])
df["Unlicensed_Status"] = np.where(df["Licensed"] == "Unlicensed",
np.random.choice(["No Application", "Licensing in Progress", "License Rejected"], num), "Not Applicable")
df["Rejection_Reason"] = np.where(df["Unlicensed_Status"] == "License Rejected",
np.random.choice(["Missing Documents", "Non-compliant Facility", "Safety Violations", "Staff Unqualified", "Others"], num), "Not Applicable")
df["Ownership"] = np.random.choice(["Rent", "Endowment", "Owned", "Usufruct", "Others"], num, p=[0.3,0.1,0.4,0.15,0.05])
df["Affiliation"] = np.random.choice(["Governmental", "Private", "CBO", "Individual"], num, p=[0.25,0.35,0.2,0.2])
df["Total_Classes"] = np.random.randint(1, 10, num)
df["Avg_Class_Area_sqm"] = np.random.normal(25, 5, num).round(1)
df["Avg_Children_Per_Class"] = np.random.randint(10, 35, num)
df["Avg_Occupancy_Rate"] = np.random.uniform(0.6, 1.0, num).round(2)
df["Total_Employees"] = np.random.randint(5, 25, num)
df["Edu_Staff_to_Children"] = np.random.uniform(0.05, 0.2, num).round(2)
df["Employee_Support_%"] = np.random.uniform(10, 30, num).round(1)
df["Employee_Edu_%"] = np.random.uniform(50, 80, num).round(1)
df["Employee_Admin_%"] = (100 - (df["Employee_Support_%"] + df["Employee_Edu_%"]).clip(upper=100)).round(1)
for col in ["Academic_Activities", "Languages", "Motor_Sensory", "Music_Theater", "Religious_Education", "Arts_Crafts", "Other_Activities"]:
df[col] = np.random.choice([True, False], num)
df["Meal_Subscription"] = np.random.choice(["Yes", "No"], num, p=[0.6, 0.4])
bins = ["0-50", "50-250", "250-500", "500-1000", "1000-2500", "2500+"]
df["Meal_Subscription_Value"] = np.where(df["Meal_Subscription"] == "Yes",
np.random.choice(bins, num, p=[0.05, 0.4, 0.3, 0.15, 0.08, 0.02]), "Not Applicable")
return df
# === Full Dashboard Launcher ===
def launch_dashboard():
df = generate_nursery_data()
gender_fig = px.pie(names=["Male", "Female"], values=[43, 57], title="Gender Distribution")
enrollment_fig = px.bar(
x=["Enrollment Rate", "Licensed %", "Unlicensed %"],
y=[10, (df['Licensed'] == 'Licensed').mean()*100, (df['Licensed'] == 'Unlicensed').mean()*100],
title="Enrollment & Licensing Overview",
labels={"x": "Metric", "y": "Percentage"},
text_auto=True
)
counts_fig = px.bar(
x=["Total Nurseries", "Children Enrolled"],
y=[5500, 55000],
title="Total Counts",
text_auto=True
)
ownership_fig = px.pie(df, names="Ownership", title="Ownership Distribution")
affiliation_fig = px.pie(df, names="Affiliation", title="Affiliation Distribution")
activity_cols = ["Academic_Activities", "Languages", "Motor_Sensory", "Music_Theater", "Religious_Education", "Arts_Crafts", "Other_Activities"]
activity_counts = df[activity_cols].apply(pd.Series.value_counts).T.rename(columns={True: "Offered", False: "Not Offered"})
activity_counts["Activity"] = activity_counts.index
activity_fig = px.bar(activity_counts, x="Activity", y="Offered", title="Activities Offered")
meal_fig = px.histogram(df[df["Meal_Subscription"] == "Yes"], x="Meal_Subscription_Value", title="Meal Subscription Value Distribution")
classroom_fig = px.histogram(df, x="Avg_Class_Area_sqm", nbins=30, title="Classroom Area Distribution")
emp_stack = go.Figure()
for role in ["Employee_Support_%", "Employee_Edu_%", "Employee_Admin_%"]:
emp_stack.add_trace(go.Box(y=df[role], name=role))
emp_stack.update_layout(title="Employee Role Distribution")
gov_summary = df.groupby("Governorate").agg(
Count=("Governorate", "count"),
Licensed_Rate=("Licensed", lambda x: (x=="Licensed").mean()*100),
Activity_Score=("Academic_Activities", lambda x: (x.sum() / len(x)) * 100),
Rejection_Score=("Rejection_Reason", lambda x: (x != 'Not Applicable').mean() * 100)
).reset_index()
gov_summary["Compliance_Tier"] = np.where(
gov_summary["Licensed_Rate"] < 50, "Low",
np.where(gov_summary["Licensed_Rate"] < 75, "Medium", "High")
)
gov_bar = px.bar(gov_summary, x="Governorate", y="Licensed_Rate", title="Licensing % by Governorate")
reject_fig = px.histogram(df[df['Rejection_Reason'] != 'Not Applicable'], x="Governorate", color="Rejection_Reason", title="Rejection Reasons by Governorate")
lang_fig = px.histogram(df[df['Languages']], x="Governorate", title="Language Activities Offered")
music_fig = px.histogram(df[df['Music_Theater']], x="Governorate", title="Music & Theater Offered")
tier_fig = px.bar(gov_summary, x="Governorate", y="Licensed_Rate", color="Compliance_Tier", title="Compliance Score by Governorate")
gov_coords = {g: [26.8 + np.random.rand()/2, 30.8 + np.random.rand()/2] for g in df["Governorate"].unique()}
maps = {}
for label, col, colors in [
("Licensing Coverage", "Licensed", {"Licensed": "green", "Unlicensed": "red"}),
("Ownership Type", "Ownership", {}),
("Affiliation Type", "Affiliation", {})
]:
fmap = folium.Map(location=[26.8, 30.8], zoom_start=6, control_scale=True, tiles='CartoDB positron')
mcluster = MarkerCluster().add_to(fmap)
for _, row in df.iterrows():
gov = row['Governorate']
coords = gov_coords.get(gov, [26.8, 30.8])
value = row[col]
color = colors.get(value, "blue")
popup = folium.Popup(f"<b>{gov}</b><br>{label}: {value}", max_width=300)
folium.Marker(location=coords, popup=popup, icon=folium.Icon(color=color)).add_to(mcluster)
file = f"map_{col}.html"
fmap.save(file)
maps[label] = file
insights = """
### Insights & Strategic Interventions
- **Licensing Focus**: Target Cairo, Giza, and Sharqia with <50% licensed nurseries
- **Facility Improvement**: Prioritize Governorates with smaller average classroom area (<22 sqm)
- **Rejection Reduction**: Most frequent reasons: 'Safety Violations' and 'Staff Unqualified'
- **Activity Gaps**: Arts, Music, and Sensory activities need boosting in rural areas
- **Staffing Ratios**: Recommend raising educational staff-to-child ratios above 0.12 minimum
### Priority Interventions by Tier:
- **Urgent** (Low Compliance): Cairo, Qalyubia — fast-track licensing, safety training
- **Strategic** (Medium Compliance): Beheira, Minya — curriculum and activity upgrade
- **Maintain** (High Compliance): Luxor, Red Sea — monitoring and recognition
### Expected Impact:
- Increase licensing compliance by 20% in 6 months
- Improve readiness of 1,500 nurseries via safety/teacher interventions
- Boost developmental outcomes by expanding educational offerings
"""
with gr.Blocks() as demo:
gr.Markdown("# 🇪🇬 Egypt Nursery Dashboard – July 2025")
charts = [counts_fig, enrollment_fig, gender_fig, ownership_fig, affiliation_fig, activity_fig,
meal_fig, classroom_fig, emp_stack, gov_bar, reject_fig, lang_fig, music_fig, tier_fig]
titles = ["Total Counts", "Enrollment & Licensing Overview", "Gender Distribution", "Ownership", "Affiliation",
"Activities Offered", "Meal Pricing", "Classroom Area", "Employee Roles", "Licensing by Governorate",
"Rejection Reasons", "Language Activities", "Music & Theater", "Compliance Tier"]
for title, fig in zip(titles, charts):
gr.Markdown(f"## {title}")
gr.Plot(fig)
gr.Markdown("## Interactive Maps")
for label, file in maps.items():
gr.Markdown(f"### {label}")
with open(file, 'r', encoding='utf-8') as f:
html = f.read().replace("'", "&#39;").replace("\n", " ")
gr.HTML(f"<iframe srcdoc='{html}' width='100%' height='500' style='border:none;'></iframe>")
gr.Markdown("## 🧠 Insights & Recommendations")
gr.Markdown(insights)
demo.launch()
launch_dashboard()