import streamlit as st
import pandas as pd
# --- PyCafe / Pyodide: Install fpdf safely ---
try:
from fpdf import FPDF
except Exception:
import micropip, asyncio
async def install_fpdf():
await micropip.install("fpdf")
global FPDF
from fpdf import FPDF
asyncio.get_event_loop().create_task(install_fpdf())
# ================= SET PAGE CONFIG ==================
st.set_page_config(page_title="SK Youth Portal", layout="wide")
# ================= SESSION STATE ==================
if "user_points" not in st.session_state:
st.session_state.user_points = 0 # starting points
if "user_name" not in st.session_state:
st.session_state.user_name = "" # store current user name
# ================= PAGE STYLE ==================
st.markdown("""
<style>
.main { background-color: #0e1117; color: white; }
.stButton>button { border-radius: 20px; background-color: #00d4ff; color: black; font-weight: bold; }
</style>
""", unsafe_allow_html=True)
# ================= CERTIFICATE FUNCTION ==================
def create_certificate(user_name, activity_name, points):
pdf = FPDF(orientation='L', unit='mm', format='A4')
pdf.add_page()
# Certificate Background (your Canva template)
pdf.image("cert.jpeg", x=0, y=0, w=297, h=210) # <-- updated file name
# Text Overlay
pdf.set_font("Arial", 'B', 36)
pdf.set_text_color(0, 0, 0)
pdf.cell(0, 110, user_name, ln=True, align='C')
pdf.set_font("Arial", '', 20)
pdf.cell(0, -40, f"For participating in {activity_name}", ln=True, align='C')
pdf.cell(0, 60, f"And earning {points} Leadership Points", ln=True, align='C')
return pdf.output(dest="S").encode("latin-1")
# ================= SIDEBAR ==================
with st.sidebar:
st.title("ποΈ SK Portal")
page = st.radio("Navigate to:",
["Dashboard", "Activity Feed", "Evaluation & Rewards", "Certificates"])
st.info("Log in to track your points!")
# ================= DASHBOARD PAGE ==================
if page == "Dashboard":
st.title("π Youth Impact Leaderboard")
st.write("Your contribution stats!")
col1, col2 = st.columns([1,2])
with col1:
st.subheader("Your Stats")
st.metric("Your Current Points",
f"{st.session_state.user_points} pts")
st.progress(st.session_state.user_points / 200) # assuming 200 pts max
st.caption("Next Rank: Silver Badge at 100 pts")
with col2:
# Example leaderboard (top contributors, you dynamically get points from session)
data = {
"Rank": [1,2,3,4],
"Name": ["Juan Dela Cruz","Maria Clara","You","Kiko Matsing"],
"Points":[150,125,st.session_state.user_points,50]
}
st.table(pd.DataFrame(data))
# ================= ACTIVITY FEED PAGE ==================
elif page == "Activity Feed":
st.header("π’ Recent & Upcoming Activities")
col_a, col_b = st.columns(2)
with col_a:
st.image("https://images.unsplash.com/photo-1544928147-79a2dbc1f389?w=500",
caption="Coastal Cleanup")
st.write("**Date:** Dec 15, 2025")
if st.button("Complete Activity", key="act1"):
st.session_state.user_points += 15
st.success(f"Activity completed! Total points: {st.session_state.user_points}")
with col_b:
st.image("https://images.unsplash.com/photo-1511632765486-a01980e01a18?w=500",
caption="Youth Night")
st.write("**Date:** Jan 5, 2026")
if st.button("Complete Activity", key="act2"):
st.session_state.user_points += 20
st.success(f"Activity completed! Total points: {st.session_state.user_points}")
# ================= EVALUATION & REWARDS PAGE ==================
elif page == "Evaluation & Rewards":
st.header("π Post-Activity Evaluation")
with st.form("eval_form"):
# Get user's name once and store it in session
if not st.session_state.user_name:
st.session_state.user_name = st.text_input("Full Name for Certificate")
act = st.selectbox("Which activity did you attend?",
["Coastal Cleanup","Tech Seminar"])
rating = st.slider("Rate the activity", 1, 5)
feedback = st.text_area("What can we improve?")
submitted = st.form_submit_button("Submit & Claim Reward")
if submitted:
if st.session_state.user_name:
st.session_state.user_points += 15
st.success(f"Form submitted! Total points: {st.session_state.user_points}")
st.balloons()
cert = create_certificate(st.session_state.user_name, act, 15)
st.download_button(
"π Download My Certificate",
data=cert,
file_name="SK_Certificate.pdf",
mime="application/pdf"
)
else:
st.warning("Please enter your name to generate the certificate.")
# ================= CERTIFICATE PAGE ==================
elif page == "Certificates":
st.header("π SK Digital Certificate Portal")
name_input = st.text_input("Enter your Full Name:", st.session_state.user_name)
if name_input:
st.session_state.user_name = name_input
activity = st.selectbox("Select Activity:",
["Coastal Cleanup","Basketball League","Tech Seminar"])
if st.button("Generate My Certificate"):
if st.session_state.user_name:
pdf_bytes = create_certificate(st.session_state.user_name, activity, st.session_state.user_points)
st.success("Certificate Ready!")
st.balloons()
st.download_button(
"β¬οΈ Download Certificate (PDF)",
data=pdf_bytes,
file_name=f"SK_Certificate_{st.session_state.user_name}.pdf",
mime="application/pdf"
)
else:
st.error("Please enter your name first!")