7-Day Comprehensive Flask Development Notes
Day 1: Introduction to Flask Framework & Setup
Goals:
- Familiarize with Flask and its features.
- Set up the development environment.
Topics Covered:
- What is Flask?
Flask is a micro web framework written in Python. It is lightweight, flexible, and easy to use for building web
applications and REST APIs.
- Features of Flask:
* Minimalistic and modular.
* Built-in development server and debugger.
* RESTful request dispatching.
* Uses Jinja2 templating engine.
* Secure cookies support.
- Environment Setup:
1. Install Python (https://www.python.org/downloads/)
2. Install Flask using pip:
pip install Flask
3. Create a project folder and a file app.py.
- Hello World Application:
from flask import Flask
app = Flask(__name__)
@app.route('/')
7-Day Comprehensive Flask Development Notes
def home():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
Resources:
- Flask Quickstart: https://flask.palletsprojects.com/en/latest/quickstart/
- Python Installation Guide: https://www.python.org/downloads/
Day 2: Routes & HTTP Requests
Goals:
- Understand routing and handling HTTP requests.
Topics Covered:
- Routing Basics:
Use @app.route decorator to define endpoints.
Dynamic routing: @app.route('/user/<name>')
- HTTP Methods:
Common methods: GET, POST, PUT, DELETE.
Use 'methods' parameter to handle multiple methods.
- Handling Forms & Requests:
Use flask.request to access form data.
Example:
from flask import request
7-Day Comprehensive Flask Development Notes
@app.route('/login', methods=['POST'])
def login():
username = request.form['username']
- Practical Exercises:
Create a login form, handle GET and POST separately.
Resources:
- Flask Routing: https://flask.palletsprojects.com/en/latest/quickstart/#routing
- HTTP Methods: https://flask.palletsprojects.com/en/latest/quickstart/#http-methods
- Flask-WTF: https://flask-wtf.readthedocs.io/en/stable/
Day 3: Introduction to SQLAlchemy & Database Setup
Goals:
- Learn ORM with SQLAlchemy and integrate a database.
Topics Covered:
- Installing SQLAlchemy:
pip install flask-sqlalchemy
- Configuration:
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
- Define Models:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
7-Day Comprehensive Flask Development Notes
- Database Migrations:
pip install flask-migrate
flask db init
flask db migrate -m "Initial"
flask db upgrade
Resources:
- Flask-SQLAlchemy: https://flasksqlalchemy.palletsprojects.com/en/latest/
- Flask-Migrate: https://flask-migrate.readthedocs.io/en/latest/
Day 4: CRUD Operations with SQLAlchemy
Goals:
- Learn and implement Create, Read, Update, Delete (CRUD).
Topics Covered:
- Create:
user = User(username='test')
db.session.add(user); db.session.commit()
- Read:
user = User.query.filter_by(username='test').first()
- Update:
user.username = 'newname'; db.session.commit()
- Delete:
db.session.delete(user); db.session.commit()
7-Day Comprehensive Flask Development Notes
- Routes & Views:
Create routes to handle CRUD operations through forms or APIs.
- Templating with Jinja2:
Use {{ variable }} and {% control structures %} in HTML files.
Resources:
- CRUD in Flask-SQLAlchemy: https://flasksqlalchemy.palletsprojects.com/en/latest/quickstart/
- Jinja2 Templating: https://flask.palletsprojects.com/en/latest/templating/
Day 5: Building RESTful APIs & Authentication
Goals:
- Create REST APIs and add authentication.
Topics Covered:
- Install flask-restful:
pip install flask-restful
- Create API Resource:
class Hello(Resource):
def get(self):
return {'message': 'hello'}
api.add_resource(Hello, '/api')
- Authentication with JWT:
pip install flask-jwt-extended
from flask_jwt_extended import JWTManager
7-Day Comprehensive Flask Development Notes
app.config['JWT_SECRET_KEY'] = 'secret'
jwt = JWTManager(app)
Resources:
- Flask-RESTful: https://flask-restful.readthedocs.io/en/latest/
- Flask-JWT-Extended: https://flask-jwt-extended.readthedocs.io/en/stable/
Day 6: Asynchronous Tasks with Celery
Goals:
- Learn how to run background tasks with Celery.
Topics Covered:
- Install Celery:
pip install celery
- Setup with Flask:
from celery import Celery
celery = Celery(app.name, broker='redis://localhost:6379/0')
- Define Task:
@celery.task
def add(x, y): return x + y
- Run Celery Worker:
celery -A app.celery worker --loglevel=info
Resources:
- Flask + Celery: https://docs.celeryproject.org/en/latest/getting-started/introduction.html
7-Day Comprehensive Flask Development Notes
Day 7: Flask Extensions & Project
Goals:
- Learn and integrate popular Flask extensions.
Topics Covered:
- Flask-Mail: send email
- Flask-Login: handle user sessions
- Flask-CORS: allow cross-origin requests
- Flask-Migrate: DB version control
- Final Project Ideas:
* Todo App with login/authentication
* Blog with post/comment API
* Notes app with background task for email notifications
Resources:
- Flask Extensions: https://flask.palletsprojects.com/en/latest/extensions/
- Flask-Mail: https://pythonhosted.org/Flask-Mail/
- Flask-JWT: https://pythonhosted.org/Flask-JWT/