Activity 34: Basic Crud Python FLask API

1: Install Python (If Not Installed Yet)

python.org.

2: Create a Python Virtual Environment

  1. Open CMD

  2. Navigate to your folder/project directory

     cd path/to/your/project/folder
    
  3. Create a virtual environment by running:

     python -m venv venv
    
  4. Activate the virtual environment:

    • Windows:

        venv\Scripts\activate
      

3: Install Flask

pip install Flask

4: Create Your Flask Application File (app.py)

Open your text editor (VS Code) and create a file .py in your project folder.

  1. Import the Library

  2. Initialize Flask app

  3. Create a list of students

  4. Create API Routes

  • GET all students

  • GET a specific student by ID

  • POST: Add a new student

  • PUT: Update a student by ID

  • DELETE: Remove a student by ID

  1. Run the Flask app
# Import the necessary library

from flask import Flask, jsonify, request



# Initialize Flask app

app = Flask(__name__)



# Create a list of students (Sample data)
students = [
    {"name": "Juan Carlos", "section": "BSIT-4B", "id": 1, "email": "juan@gmail.com", "age": 22},
    {"name": "Jose Rizal", "section": "BSIT-2A", "id": 2, "email": "jose@gmail.com", "age": 21},
    {"name": "Juan Luna", "section": "BSIT-3A", "id": 3, "email": "juan@gmail.com", "age": 20},
    {"name": "Andres Bonifacio", "section": "BSIT-3A", "id": 4, "email": "andres@gmail.com", "age": 20},
    {"name": "Justin Bieber", "section": "BSIT-2A", "id": 5, "email": "justin@gmail.com", "age": 21}
]



# Create API Routes
# GET all students

@app.route('/students', methods=['GET'])
def get_students():
    return jsonify(students)



# GET a specific student by ID

@app.route('/students/<int:student_id>', methods=['GET'])
def get_student(student_id):
    student = next((student for student in students if student["id"] == student_id), None)
    if student:
        return jsonify(student)
    else:
        return jsonify({"message": "Student not found"}), 404



# POST: Add a new student

@app.route('/students', methods=['POST'])
def add_student():
    new_student = request.get_json()
    new_student['id'] = max(student['id'] for student in students) + 1  # Generate new ID
    students.append(new_student)
    return jsonify(new_student), 201



# PUT: Update a student by ID

@app.route('/students/<int:student_id>', methods=['PUT'])
def update_student(student_id):
    student = next((student for student in students if student["id"] == student_id), None)
    if student:
        data = request.get_json()
        student.update(data)
        return jsonify(student)
    else:
        return jsonify({"message": "Student not found"}), 404



# DELETE: Remove a student by ID

@app.route('/students/<int:student_id>', methods=['DELETE'])
def delete_student(student_id):
    student = next((student for student in students if student["id"] == student_id), None)
    if student:
        students.remove(student)
        return jsonify({"message": "Student deleted"})
    else:
        return jsonify({"message": "Student not found"}), 404



# Run the Flask app

if __name__ == '__main__':
    app.run(debug=True)

5: Run Your Flask Application

Use python “name of your app (.py)“

python app.py

6: Test the API Using Postman

1.6: Open Postman

  1. If you haven't installed Postman, download it here: https://www.postman.com/downloads/

  2. After installation, open Postman.

2.6: Test the API with Postman

Get All Students (GET request)

  1. Open Postman.

  2. Create a new request

  3. Set the method to GET from the dropdown menu

  4. http://127.0.0.1:5000/students , copy the http link that you get by running your app

  1. Click Send

Result

[
    {
        "age": 22,
        "email": "juan@gmail.com",
        "id": 1,
        "name": "Juan Carlos",
        "section": "BSIT-4B"
    },
    {
        "age": 21,
        "email": "jose@gmail.com",
        "id": 2,
        "name": "Jose Rizal",
        "section": "BSIT-2A"
    },
    {
        "age": 20,
        "email": "juan@gmail.com",
        "id": 3,
        "name": "Juan Luna",
        "section": "BSIT-3A"
    },
    {
        "age": 20,
        "email": "andres@gmail.com",
        "id": 4,
        "name": "Andres Bonifacio",
        "section": "BSIT-3A"
    },
    {
        "age": 21,
        "email": "justin@gmail.com",
        "id": 5,
        "name": "Justin Bieber",
        "section": "BSIT-2A"
    }
]

Get a Student by ID (GET request)

In Postman, set the method to GET

http://127.0.0.1:5000/students/1

You can replace 1 with the any student ID you want to retrieve

Result

{
    "age": 22,
    "email": "juan@gmail.com",
    "id": 1,
    "name": "Juan Carlos",
    "section": "BSIT-4B"
}

Add a New Student (POST request)

set the method to POST

http://127.0.0.1:5000/students

Go to Body
Select raw radio button
Add new student you want to add

Then enter the student data in JSON format like this:

{
    "name": "New Student",
    "section": "BSIT-1A",
    "email": "newstudent@gmail.com",
    "age": 20
}

Click Send

Result

{
    "age": 20,
    "email": "newstudent@gmail.com",
    "id": 6,
    "name": "New Student",
    "section": "BSIT-1A"
}

Update a Student (PUT request)

set the method to PUT

http://127.0.0.1:5000/students/1

You can replace 1 with the student ID you want to update

Go to Body
Select raw radio button

Enter the data you want to update

{
  "name": "Updated Student"
}

Click Send

Result

{
    "age": 22,
    "email": "juan@gmail.com",
    "id": 1,
    "name": "Updated Student",
    "section": "BSIT-4B"
}

Delete a student (DELETE request)

set the method to DELETE.

http://127.0.0.1:5000/students/1

You can change the 1 with any student ID you want to delete

Click Send

Result

{
  "message": "Student deleted"
}

GitHub Repository: https://github.com/MonetForProgrammingPurposes/flask-crud-api