Final Project: Python Flask API
GitHub Repositories:
Online Voting System
Online Job Platform
E-commerce
Travel Booking
Restaurant Reservation
Online Quiz Platform
Event Management System
Blogging Platform
Personal Finance Manager
Recipe Sharing Platform
Library Web App
Set Up the Project Environment
mkdir python-flask-api-project cd python-flask-api-project
Create Subdirectories for Each Application:
mkdir library-web-app online-voting-system online-job-platform e-commerce travel-booking restaurant-reservation online-quiz-platform event-management-system blogging-platform personal-finance-manager recipe-sharing-platform
Set Up Virtual Environment for Each Application:
# For library-web-app directory
cd library-web-app
python -m venv venv
Activate virtual environment:
venv\Scripts\activate
Install Dependencies (Flask, Flask-SQLAlchemy, etc.):
pip install Flask flask-mysqldb flask-cors flask-jwt-extended requests
Set Up MySQL Database (AIVEN):
app.config['MYSQL_HOST'] = 'your_aiven_host' app.config['MYSQL_USER'] = 'your_aiven_user' app.config['MYSQL_PASSWORD'] = 'your_aiven_password' app.config['MYSQL_DB'] = 'your_aiven_db_name'
Create Database Models and Structure
Create
models.py
in Each Subdirectory:# models.py (Library Web App) from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class User(db.Model): user_id = db.Column(db.String(100), primary_key=True) username = db.Column(db.String(50), nullable=False) email = db.Column(db.String(100), nullable=False) password = db.Column(db.String(100), nullable=False) role = db.Column(db.String(50)) created_at = db.Column(db.DateTime) updated_at = db.Column(db.DateTime) class Book(db.Model): book_id = db.Column(db.String(100), primary_key=True) title = db.Column(db.String(100)) author = db.Column(db.String(100)) genre = db.Column(db.String(50)) publication_date = db.Column(db.Date) ISBN = db.Column(db.String(50)) summary = db.Column(db.Text) created_at = db.Column(db.DateTime) updated_at = db.Column(db.DateTime)
Repeat for Other Applications
Set Up Flask Routes (API Endpoints)
# routes.py (Library Web App) from flask import Flask, request, jsonify from models import db, User, Book app = Flask(__name__) @app.route('/users', methods=['GET']) def get_users(): users = User.query.all() return jsonify([user.serialize() for user in users]) @app.route('/user', methods=['POST']) def add_user(): data = request.get_json() new_user = User(username=data['username'], email=data['email'], password=data['password']) db.session.add(new_user) db.session.commit() return jsonify(new_user.serialize()), 201
Create Endpoints for CRUD Operations:
GET: Retrieve records (e.g.,
GET /users
,GET /books
).POST: Create records (e.g.,
POST /user
,POST /book
).PUT/PATCH: Update records (e.g.,
PUT /user/{id}
,PATCH /book/{id}
).DELETE: Delete records (e.g.,
DELETE /user/{id}
,DELETE /book/{id}
).
Define Serializers for Models:
- Add methods to convert model instances to JSON format:
class User(db.Model):
# model fields...
def serialize(self):
return {
'user_id': self.user_id,
'username': self.username,
'email': self.email,
'role': self.role,
'created_at': self.created_at,
'updated_at': self.updated_at
}
Repeat for Other Applications
Testing with Postman
Create Postman Collections for Each Application:
Define Subfolders for Each Collection:
Test Each Endpoint:
online-job-platform:
POST:
GET:
PUT/PATCH:
DELETE:
restaurant-reservation:
POST:
GET:
PUT:
e-commerce:
POST:
GET
PUT/PATCH
library-web-app:
GET:
POST:
PUT/PATCH
DELETE:
travel-booking:
POST
GET
Set Up GitHub Repositories
Create a Separate GitHub Repository for Each Application
Initialize a Git repository inside each subdirectory:
git init git add . git commit -m "Initial commit" git remote add origin <your-repository-url> git push -u origin master