0% found this document useful (0 votes)
4 views

reserve code

This document is a Python Flask application that manages student enrollment using a SQLite database. It includes functionalities to add, edit, delete, and display students, along with a web interface styled with custom CSS. The application initializes a database and handles user interactions through various routes and templates.

Uploaded by

janinemylove08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

reserve code

This document is a Python Flask application that manages student enrollment using a SQLite database. It includes functionalities to add, edit, delete, and display students, along with a web interface styled with custom CSS. The application initializes a database and handles user interactions through various routes and templates.

Uploaded by

janinemylove08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

from flask import Flask, request, redirect, url_for, render_template_string, flash

import sqlite3
import os

app = Flask(__name__)
app.secret_key = 'pinedabayot'

DB_PATH = os.path.join(os.path.dirname(__file__), 'students.db')

def init_db():
try:
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
course TEXT NOT NULL,
year_level TEXT NOT NULL
)
''')
conn.commit()
conn.close()
except Exception as e:
print("Database init error:", e)

CUSTOM_CSS = '''
@import
url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap');

body {
font-family: 'Poppins', sans-serif;
background: #f0f4f8;
color: #2c3e50;
}

header {
background: linear-gradient(90deg, #20c997, #ff6b6b);
color: white;
padding: 2rem 0;
text-align: center;
border-radius: 0 0 15px 15px;
box-shadow: 0 4px 8px rgb(0 0 0 / 0.1);
}

h1, h2 {
font-weight: 600;
}
.container {
max-width: 900px;
}

/* Buttons */
.btn-primary {
background-color: #007bff;
border-color: #007bff;
border-radius: 30px;
font-weight: 600;
transition: background-color 0.3s ease;
}

.btn-primary:hover {
background-color: #0056b3;
border-color: #0056b3;
}

.btn-warning {
background-color: #fd7e14;
border-color: #fd7e14;
border-radius: 30px;
font-weight: 600;
color: white;
transition: background-color 0.3s ease;
}

.btn-warning:hover {
background-color: #e36b00;
border-color: #e36b00;
color: white;
}

.btn-danger {
background-color: #dc3545;
border-color: #dc3545;
border-radius: 30px;
font-weight: 600;
transition: background-color 0.3s ease;
}

.btn-danger:hover {
background-color: #a71d2a;
border-color: #a71d2a;
}

.btn-secondary {
background-color: #748ffc;
border-color: #748ffc;
color: white;
border-radius: 30px;
font-weight: 600;
transition: background-color 0.3s ease;
}

.btn-secondary:hover {
background-color: #5a66d9;
border-color: #5a66d9;
color: white;
}

/* Table */
table {
border-collapse: separate;
border-spacing: 0 0.75rem;
}

thead tr {
background-color: transparent !important;
color: #34495e;
font-weight: 600;
}

tbody tr {
background: white;
box-shadow: 0 4px 12px rgb(0 0 0 / 0.1);
border-radius: 10px;
transition: transform 0.2s ease;
}

tbody tr:hover {
transform: translateY(-3px);
}

td, th {
vertical-align: middle !important;
border: none !important;
}

/* Action buttons container */


.actions {
display: flex;
justify-content: flex-end;
gap: 0.5rem;
}
/* Position Add Student button top right */
.add-btn-container {
display: flex;
justify-content: flex-end;
margin-bottom: 1.5rem;
}

/* Alerts, form styles remain unchanged */


.alert {
border-radius: 12px;
font-weight: 600;
}

.form-control, .form-select {
border-radius: 12px;
box-shadow: none !important;
border: 1.8px solid #ced4da;
transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.form-control:focus, .form-select:focus {
border-color: #20c997;
box-shadow: 0 0 8px rgb(32 201 151 / 0.35);
outline: none;
}
'''

@app.route('/')
def index():
try:
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
c = conn.cursor()
c.execute('SELECT * FROM students')
students = c.fetchall()
conn.close()
except Exception as e:
flash("Error loading students: " + str(e), "danger")
students = []

return render_template_string('''
<!doctype html>
<html>
<head>
<title>Student Manager</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
<style>{{ css }}</style>
</head>
<body>
<header>
<h1>Student Course Enrollment</h1>
</header>
<div class="container my-5">
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
{% for category, message in messages %}
<div class="alert alert-{{ category }}">{{ message }}</div>
{% endfor %}
{% endif %}
{% endwith %}
<div class="add-btn-container">
<a href="{{ url_for('add_student') }}" class="btn btn-primary">Add Student</a>
</div>
<table class="table">
<thead>
<tr><th>Name</th><th>Course</th><th>Year</th><th>Actions</th></tr>
</thead>
<tbody>
{% for student in students %}
<tr>
<td>{{ student['name'] }}</td>
<td>{{ student['course'] }}</td>
<td>{{ student['year_level'] }}</td>
<td>
<div class="actions">
<a href="{{ url_for('edit_student', id=student['id']) }}" class="btn btn-warning
btn-sm">Edit</a>
<a href="{{ url_for('delete_student', id=student['id']) }}" class="btn
btn-danger btn-sm" onclick="return confirm('Are you sure to delete this
student?');">Delete</a>
</div>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</body>
</html>
''', students=students, css=CUSTOM_CSS)

@app.route('/add', methods=['GET', 'POST'])


def add_student():
if request.method == 'POST':
try:
name = request.form['name']
course = request.form['course']
year = request.form['year_level']
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('INSERT INTO students (name, course, year_level) VALUES (?, ?, ?)',
(name, course, year))
conn.commit()
conn.close()
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('''
<!doctype html>
<html>
<head><title>Add Student</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
<style>{{ css }}</style>
</head><body>
<header>
<h2 class="text-center py-4">Add New Student</h2>
</header>
<div class="container my-5">
<form method="POST" class="mx-auto" style="max-width:500px;">
<div class="mb-3">
<label class="form-label">Name:</label>
<input name="name" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">Course:</label>
<input name="course" class="form-control" required>
</div>
<div class="mb-3">
<label class="form-label">Year Level:</label>
<input name="year_level" class="form-control" required>
</div>
<button class="btn btn-primary">Add Student</button>
<a href="{{ url_for('index') }}" class="btn btn-secondary ms-2">Cancel</a>
</form>
</div>
</body></html>
''', css=CUSTOM_CSS)

@app.route('/edit/<int:id>', methods=['GET', 'POST'])


def edit_student(id):
try:
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
c = conn.cursor()
if request.method == 'POST':
name = request.form['name']
course = request.form['course']
year = request.form['year_level']
c.execute('UPDATE students SET name=?, course=?, year_level=? WHERE id=?',
(name, course, year, id))
conn.commit()
conn.close()
flash("Student updated successfully.", "success")
return redirect(url_for('index'))
else:
c.execute('SELECT * FROM students WHERE id=?', (id,))
student = c.fetchone()
conn.close()
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('''
<!doctype html>
<html>
<head><title>Edit Student</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet">
<style>{{ css }}</style>
</head><body>
<header>
<h2 class="text-center py-4">Edit Student</h2>
</header>
<div class="container my-5">
<form method="POST" class="mx-auto" style="max-width:500px;">
<div class="mb-3">
<label class="form-label">Name:</label>
<input name="name" class="form-control" value="{{ student['name'] }}" required>
</div>
<div class="mb-3">
<label class="form-label">Course:</label>
<input name="course" class="form-control" value="{{ student['course'] }}"
required>
</div>
<div class="mb-3">
<label class="form-label">Year Level:</label>
<input name="year_level" class="form-control" value="{{ student['year_level'] }}"
required>
</div>
<button class="btn btn-warning">Update</button>
<a href="{{ url_for('index') }}" class="btn btn-secondary ms-2">Cancel</a>
</form>
</div>
</body></html>
''', student=student, css=CUSTOM_CSS)

@app.route('/delete/<int:id>')
def delete_student(id):
try:
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
c.execute('DELETE FROM students WHERE id=?', (id,))
conn.commit()
conn.close()
flash("Student deleted.", "success")
except Exception as e:
flash("Error deleting student: " + str(e), "danger")
return redirect(url_for('index'))

if __name__ == '__main__':
init_db()
app.run(host='0.0.0.0', port=5000, debug=True)

You might also like