From dde868789ca219fb41cf19469078118d2b2cde54 Mon Sep 17 00:00:00 2001 From: Khaled Mahfouz Date: Mon, 9 Jun 2025 19:52:42 +0300 Subject: [PATCH] - version : 0.1 - description : initialize the app --- .gitignore | 1 + app.py | 71 ++++++++++++++++++++++++++++++++ templates/index.html | 96 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 168 insertions(+) create mode 100644 .gitignore create mode 100644 app.py create mode 100644 templates/index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bee8a64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__ diff --git a/app.py b/app.py new file mode 100644 index 0000000..04a7ec0 --- /dev/null +++ b/app.py @@ -0,0 +1,71 @@ +import sqlite3 +from flask import Flask, render_template, request, redirect, url_for + +# Create a Flask application instance +app = Flask(__name__) + +# --- Database Functions --- + +def get_db_connection(): + """Creates and returns a connection to the SQLite database.""" + conn = sqlite3.connect('students.db') + # Return rows as dictionaries, so we can access columns by name + conn.row_factory = sqlite3.Row + return conn + +def init_db(): + """Initializes the database and creates the 'students' table if it doesn't exist.""" + conn = get_db_connection() + # Use a 'with' statement to automatically handle closing the connection + with conn: + conn.execute(''' + CREATE TABLE IF NOT EXISTS students ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + parent_name TEXT NOT NULL, + phone_number TEXT NOT NULL + ) + ''') + print("Database initialized and 'students' table created.") + +# Command to initialize the database from the command line: flask init-db +@app.cli.command('init-db') +def init_db_command(): + """Clear existing data and create new tables.""" + init_db() + + +# --- App Routes --- + +@app.route('/') +def index(): + """Renders the main page with the list of students.""" + conn = get_db_connection() + students = conn.execute('SELECT * FROM students').fetchall() + conn.close() + # The 'students' variable is passed to the HTML template + return render_template('index.html', students=students) + +@app.route('/add', methods=['POST']) +def add_student(): + """Handles the form submission for adding a new student.""" + # Get data from the form submitted in index.html + name = request.form['name'] + parent_name = request.form['parent_name'] + phone_number = request.form['phone_number'] + + # Insert the new student record into the database + conn = get_db_connection() + conn.execute('INSERT INTO students (name, parent_name, phone_number) VALUES (?, ?, ?)', + (name, parent_name, phone_number)) + conn.commit() + conn.close() + + # Redirect back to the main page to see the updated list + return redirect(url_for('index')) + +if __name__ == '__main__': + # Initialize the database when the script is run directly + init_db() + # Run the Flask development server + app.run(debug=True) diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..425f91e --- /dev/null +++ b/templates/index.html @@ -0,0 +1,96 @@ + + + + + + Student Information + + + + + + + + + + +
+ + +
+

Student Management System

+

Enter and view student details below.

+
+ + +
+

Add New Student

+
+
+ +
+ + +
+ +
+ + +
+ +
+ + +
+
+
+ +
+
+
+ + +
+

Registered Students

+
+ + + + + + + + + + + {% for student in students %} + + + + + + {% else %} + + + + + {% endfor %} + +
Student NameParent's NamePhone Number
{{ student['name'] }}{{ student['parent_name'] }}{{ student['phone_number'] }}
No students have been added yet.
+
+
+ +
+ +