Basic CRUD Python FLask API

Prerequisite

  1. Virtual Environment and Python Package Manager

  2. Install Python Flask

  3. .gitignore for virtual environment

Make an app.py file and add this code

#import flask LIBRARY
from flask import Flask, jsonify, request

# call the flask object
app = Flask(__name__)

# create list of dictionaries
# Sample data: list of students
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
    },
    {
        "name": "Michael Jordan",
        "section": "BSIT-4A",
        "id": 6,
        "email": "michael@gmail.com",
        "age": 19
    },
    {
        "name": "Andrew Jordan",
        "section": "BSIT-3A",
        "id": 7,
        "email": "andrew@gmail.com",
        "age": 20
    },
    {
        "name": "Jessa Boe",
        "section": "BSIT-2B",
        "id": 8,
        "email": "jessa@gmail.com",
        "age": 18
    },
    {
        "name": "Ted Talk",
        "section": "BSIT-3B",
        "id": 9,
        "email": "ted@gmail.com",
        "age": 19
    }
]

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

# READ: 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

@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

@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

# Dont forget this
if __name__ == '__main__':
    app.run(debug=True)

Run the app.py to run it use this command on your command prompt

python app.py

Go to your postman application and create a new collection and then add a new request and try to GET request the student data from your python flask application

Then add a new request and GET request the student data by id

Now add a new request and make a new student data then use POST request to new add student data

Then add a new request and update the data that you make and use PUT request to update the data

Lastly add a new request and choose the data that you make and use DELETE request to delete the data

And don’t forget to use this command on your python flask application before pushing your application in github repository

 pip freeze > requirements.txt
0
Subscribe to my newsletter

Read articles from Walter John Salibay directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Walter John Salibay
Walter John Salibay