from flask import Flask, request, redirect, url_for, render_template_string, flash
import sqlite3, os
from jinja2 import DictLoader
app = Flask(__name__)
# Secret key for session & flash messages
app.secret_key = [Link]('SECRET_KEY', 'secret123')
# Database file path
DB_PATH = [Link]([Link](__file__), 'students_enroll_list.db')
# Initialize DB and create students table if not exists
def init_db():
with [Link](DB_PATH) as conn:
[Link]('''CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL, course TEXT NOT NULL, year_level TEXT NOT NULL)''')
# Fetch all student records
def get_all_students():
with [Link](DB_PATH) as conn:
return [Link]('SELECT * FROM students').fetchall()
# Fetch single student by ID
def get_student(student_id):
with [Link](DB_PATH) as conn:
return [Link]('SELECT * FROM students WHERE id=?', (student_id,)).fetchone()
# Insert new student into DB
def add_student_db(name, course, year):
with [Link](DB_PATH) as conn:
[Link]('INSERT INTO students (name, course, year_level) VALUES (?, ?, ?)',
(name, course, year))
# Update student info by ID
def update_student_db(id, name, course, year):
with [Link](DB_PATH) as conn:
[Link]('UPDATE students SET name=?, course=?, year_level=? WHERE id=?',
(name, course, year, id))
# Delete student by ID
def delete_student_db(id):
with [Link](DB_PATH) as conn:
[Link]('DELETE FROM students WHERE id=?', (id,))
#TEMPLATES
base_template = '''<!doctype html><html><head>
<title>{% block title %}Student Manager{% endblock %}</title>
<link href="[Link]
rel="stylesheet">
<style>
body {
background: url('{{ url_for('static', filename='[Link]') }}') center/cover no-repeat
fixed;
min-height: 100vh;
}
.overlay { background: rgba(255,255,255,0.95); padding:2rem; border-radius:10px;
position:relative; }
#aboutText {
position:fixed; bottom:15px; right:15px; color:white; font-weight:600; cursor:pointer;
font-size:0.95rem; user-select:none; z-index:1080; transition:color 0.3s ease;
}
#aboutText:hover { color:#145c32; text-decoration:underline; }
#aboutModal table { width:100%; border-collapse:collapse; }
#aboutModal th, td { padding:0.5rem 1rem; text-align:left; border:1px solid #dee2e6; }
#aboutModal th { background:#198754; color:white; }
</style></head><body>
<div id="aboutText" data-bs-toggle="modal" data-bs-target="#aboutModal" role="button"
tabindex="0">About This Project</div>
<div class="container py-5">
<div class="overlay shadow-sm">
<h1 class="text-center mb-4">Student Course Enrollment</h1>
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
{{ message }}<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
</div>
{% endfor %}
{% endif %}
{% endwith %}
{% block content %}{% endblock %}
</div></div>
<div class="modal fade" id="aboutModal" tabindex="-1"><div class="modal-dialog
modal-dialog-centered modal-lg">
<div class="modal-content">
<div class="modal-header bg-success text-white"><h5 class="modal-title">About This
Project</h5>
<button type="button" class="btn-close btn-close-white"
data-bs-dismiss="modal"></button></div>
<div class="modal-body">
<section><h6>Project Name</h6><p>Student Course Enrollment Manager</p></section>
<section><h6>Created By:</h6><p>Salazar, Yugene C.</p></section>
<section><h6>Date Created</h6><p>May 19, 2025</p></section>
<section><h6>Purpose</h6><p>For project purposes only, a Flask web app managing
student enrollment info with CRUD functionalities.</p></section>
<section><h6>Technologies
Used</h6><ul><li>Python</li><li>Flask</li><li>SQLite</li><li>Bootstrap
5</li></ul></section>
<section><h6>Notes</h6><p>Designed with simplicity and usability in mind, supporting
year-level selection and basic validations.</p></section>
</div>
<div class="modal-footer"><button type="button" class="btn btn-secondary"
data-bs-dismiss="modal">Close</button></div>
</div></div></div>
<script
src="[Link]
</body></html>'''
index_template = '''{% extends "[Link]" %}
{% block title %}Home - Student Manager{% endblock %}
{% block content %}
<a href="{{ url_for('add_student') }}" class="btn btn-success mb-3">Add Student</a>
<div class="table-responsive">
<table class="table table-hover align-middle table-bordered bg-white">
<thead
class="table-dark"><tr><th>Name</th><th>Course</th><th>Year</th><th>Actions</th></tr>
</thead>
<tbody>
{% for s in students %}
<tr><td>{{ s[1] }}</td><td>{{ s[2] }}</td><td>{{ s[3] }}</td>
<td><a href="{{ url_for('edit_student', id=s[0]) }}" class="btn btn-sm btn-warning">Edit</a>
<a href="{{ url_for('delete_student', id=s[0]) }}" class="btn btn-sm btn-danger" onclick="return
confirm('Are you sure?');">Delete</a></td></tr>
{% else %}
<tr><td colspan="4" class="text-center">No students found.</td></tr>
{% endfor %}
</tbody></table></div>
{% endblock %}'''
add_template = '''{% extends "[Link]" %}
{% block title %}Add Student{% endblock %}
{% block content %}
<h2 class="mb-4">Add New Student</h2>
<form method="POST">
<div class="mb-3"><label>Name:</label><input name="name" class="form-control"
required></div>
<div class="mb-3"><label>Course:</label><input name="course" class="form-control"
required></div>
<div class="mb-3"><label>Year Level:</label>
<select name="year_level" class="form-control" required>
<option value="" disabled selected hidden>Select Year</option>
<option value="1st Year">1st Year</option><option value="2nd Year">2nd Year</option>
<option value="3rd Year">3rd Year</option><option value="4th Year">4th Year</option>
</select></div>
<button class="btn btn-success">Add Student</button>
<a href="{{ url_for('index') }}" class="btn btn-secondary">Cancel</a>
</form>
{% endblock %}'''
edit_template = '''{% extends "[Link]" %}
{% block title %}Edit Student{% endblock %}
{% block content %}
<h2 class="mb-4">Edit Student</h2>
<form method="POST">
<div class="mb-3"><label>Name:</label>
<input name="name" class="form-control" value="{{ student[1] }}" required></div>
<div class="mb-3"><label>Course:</label>
<input name="course" class="form-control" value="{{ student[2] }}" required></div>
<div class="mb-3"><label>Year Level:</label>
<select name="year_level" class="form-control" required>
<option value="" disabled hidden>Select Year</option>
<option value="1st Year" {% if student[3] == '1st Year' %}selected{% endif %}>1st
Year</option>
<option value="2nd Year" {% if student[3] == '2nd Year' %}selected{% endif %}>2nd
Year</option>
<option value="3rd Year" {% if student[3] == '3rd Year' %}selected{% endif %}>3rd
Year</option>
<option value="4th Year" {% if student[3] == '4th Year' %}selected{% endif %}>4th
Year</option>
</select></div>
<button class="btn btn-warning">Update</button>
<a href="{{ url_for('index') }}" class="btn btn-secondary">Cancel</a>
</form>
{% endblock %}'''
app.jinja_loader = DictLoader({
'[Link]': base_template, '[Link]': index_template,
'[Link]': add_template, '[Link]': edit_template
})
# Load all students and render index page
@[Link]('/')
def index():
try: students = get_all_students()
except Exception as e:
flash("Error loading students: " + str(e), "danger"); students = []
return render_template_string(index_template, students=students)
# Add new student on POST, show form on GET
@[Link]('/add', methods=['GET', 'POST'])
def add_student():
if [Link] == 'POST':
try:
add_student_db([Link]['name'], [Link]['course'],
[Link]['year_level'])
flash("Student added successfully.", "success")
return redirect(url_for('index'))
except Exception as e:
flash("Error adding student: " + str(e), "danger")
return render_template_string(add_template)
# Edit student data on POST, show current data on GET
@[Link]('/edit/<int:id>', methods=['GET', 'POST'])
def edit_student(id):
try:
if [Link] == 'POST':
update_student_db(id, [Link]['name'], [Link]['course'],
[Link]['year_level'])
flash("Student updated successfully.", "success")
return redirect(url_for('index'))
student = get_student(id)
if not student:
flash("Student not found.", "danger"); return redirect(url_for('index'))
except Exception as e:
flash("Error loading student: " + str(e), "danger")
return redirect(url_for('index'))
return render_template_string(edit_template, student=student)
# Delete student by ID and redirect
@[Link]('/delete/<int:id>')
def delete_student(id):
try:
delete_student_db(id); flash("Student deleted.", "success")
except Exception as e:
flash("Error deleting student: " + str(e), "danger")
return redirect(url_for('index'))
# Ensure DB is initialized before running app
if __name__ == '__main__':
init_db()
[Link](host='[Link]', port=5000, debug=True)