# Basic CRUD Python FLask API

### **Prerequisite**

1. Virtual Environment and Python Package Manager
    
2. Install Python Flask
    
3. .gitignore for virtual environment
    

### [Make](http://app.py/) an [**app.py**](http://app.py) [**file a**](http://app.py/)**nd add this code**

```plaintext
#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**](http://app.py) **to run it use this command on your command prompt**

```plaintext
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**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729582046521/8dfd8aae-15ce-4116-a91e-5ff3344b80d9.png?auto=compress,format&format=webp align="left")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729582213144/88887d92-21d2-43b4-b44e-68495f04a510.png?auto=compress,format&format=webp align="left")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729582416944/552d8c3a-4b76-44a5-9f74-8b2257f77b73.png?auto=compress,format&format=webp align="left")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729582558718/d87939ce-b84b-4364-9d9d-bbab2c42a3d7.png?auto=compress,format&format=webp align="left")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1729582663336/0da74609-76ba-4636-b659-82032fb66160.png?auto=compress,format&format=webp align="left")

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

```plaintext
 pip freeze > requirements.txt
```
