Flask Training , study and exam guide
1 Introduction to Flask
1.1 What is Flask?
1.2 History and Evolution of Flask
1.3 Flask vs Django
1.4 Setting Up the Development Environment
2 Flask Basics
2.1 Installing Flask
2.2 Creating Your First Flask Application
2.3 Understanding the Flask Application Structure
2.4 Routing in Flask
2.5 Variable Rules in Routing
2.6 HTTP Methods (GET, POST, PUT, DELETE)
3 Templates and Static Files
3.1 Introduction to Jinja2 Templates
3.2 Rendering Templates
3.3 Template Inheritance
3.4 Static Files (CSS, JavaScript, Images)
3.5 Using Bootstrap with Flask
4 Forms and User Input
4.1 Introduction to Flask-WTF
4.2 Creating Forms with Flask-WTF
4.3 Validating User Input
4.4 Handling File Uploads
4.5 Flash Messages
5 Databases with Flask
5.1 Introduction to SQLAlchemy
5.2 Setting Up a Database
5.3 Defining Models
5.4 CRUD Operations with SQLAlchemy
5.5 Relationships in SQLAlchemy
5.6 Migrations with Flask-Migrate
6 Authentication and Authorization
6.1 Introduction to Flask-Login
6.2 User Authentication
6.3 Protecting Routes with Login Required
6.4 User Roles and Permissions
6.5 Password Hashing with Werkzeug
7 RESTful APIs with Flask
7.1 Introduction to RESTful APIs
7.2 Creating a RESTful API with Flask
7.3 Serializing and Deserializing Data
7.4 Handling API Errors
7.5 Authentication for APIs
8 Testing Flask Applications
8.1 Introduction to Unit Testing
8.2 Writing Tests with Flask-Testing
8.3 Testing Routes and Views
8.4 Testing Database Interactions
8.5 Continuous Integration with Flask
9 Deployment and Scaling
9.1 Introduction to Deployment
9.2 Deploying Flask Applications on Heroku
9.3 Deploying Flask Applications on AWS
9.4 Scaling Flask Applications
9.5 Load Balancing and Caching
10 Advanced Topics
10.1 Background Tasks with Celery
10.2 WebSockets with Flask-SocketIO
10.3 Internationalization and Localization
10.4 Custom Error Pages
10.5 Extending Flask with Blueprints
11 Exam Preparation
11.1 Review of Key Concepts
11.2 Practice Questions
11.3 Mock Exams
11.4 Tips for the Exam Day
Databases with Flask

Databases with Flask

Key Concepts

SQLite

SQLite is a lightweight, serverless database that is ideal for small to medium-sized applications. It stores the entire database in a single file, making it easy to manage and deploy. Flask integrates seamlessly with SQLite using the SQLAlchemy ORM.

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)

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

PostgreSQL

PostgreSQL is a powerful, open-source relational database system. It supports complex queries, transactions, and has a strong reputation for reliability and data integrity. Flask can connect to PostgreSQL using the psycopg2 adapter.

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://username:password@localhost/dbname'
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)

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

MySQL

MySQL is another popular open-source relational database management system. It is widely used in web applications due to its speed, reliability, and ease of use. Flask can connect to MySQL using the pymysql adapter.

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://username:password@localhost/dbname'
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)

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

MongoDB

MongoDB is a NoSQL database that stores data in JSON-like documents. It is highly scalable and flexible, making it suitable for applications that require high performance and dynamic schemas. Flask can connect to MongoDB using the Flask-PyMongo extension.

from flask import Flask
from flask_pymongo import PyMongo

app = Flask(__name__)
app.config['MONGO_URI'] = 'mongodb://localhost:27017/dbname'
mongo = PyMongo(app)

@app.route('/add')
def add():
    user = mongo.db.users.insert_one({'username': 'john_doe'})
    return 'User added!'

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

Redis

Redis is an in-memory data structure store, used as a database, cache, and message broker. It supports various data types such as strings, hashes, lists, sets, and sorted sets. Flask can connect to Redis using the Flask-Redis extension.

from flask import Flask
from flask_redis import FlaskRedis

app = Flask(__name__)
app.config['REDIS_URL'] = 'redis://localhost:6379/0'
redis_client = FlaskRedis(app)

@app.route('/set')
def set():
    redis_client.set('username', 'john_doe')
    return 'Username set!'

@app.route('/get')
def get():
    username = redis_client.get('username')
    return f'Username: {username.decode("utf-8")}'

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