Wednesday, 22 October 2025

#12 Flask fundas

Python Flask Masterclass: 10 Modules

Python Flask Masterclass: Learn by Building

This tutorial introduces Flask fundamentals through 10 progressively complex modules—from setup to deployment.

Module 1: Introduction to Flask

Flask is a lightweight WSGI web framework for Python. Let’s start by installing and running a simple app.

pip install Flask
# app.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Flask!"
    
if __name__ == '__main__':
    app.run(debug=True)

Module 2: Routing and URL Parameters

Explore dynamic URL routes and parameter passing.

from flask import Flask
app = Flask(__name__)

@app.route('/user/<username>')
def show_user(username):
    return f"Welcome {username}!"

Module 3: Templates with Jinja2

Use Flask with HTML templates stored in a templates/ folder.

from flask import render_template

@app.route('/greet/<name>')
def greet(name):
    return render_template('greet.html', name=name)
<!DOCTYPE html>
<html>
  <body>
    <h2>Hello, {{ name }}!</h2>
  </body>
</html>

Module 4: Static Files (CSS & JS)

Serve CSS and JS from a static/ folder.

@app.route('/profile')
def profile():
    return render_template('profile.html')
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
<h1>My Profile</h1>

Module 5: Request Handling and Forms

Learn to use GET and POST methods.

from flask import request

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        user = request.form['username']
        return f"Welcome {user}"
    return '''
    <form method="post">
        <input name="username">
        <input type="submit">
    </form>'''

Module 6: Flask Blueprints (Modular Apps)

Organize large apps using Blueprints.

# blog/routes.py
from flask import Blueprint

blog = Blueprint('blog', __name__)

@blog.route('/posts')
def posts():
    return "Blog posts list"
# main.py
from flask import Flask
from blog.routes import blog

app = Flask(__name__)
app.register_blueprint(blog)

Module 7: Working with Databases (SQLite + SQLAlchemy)

Integrate Flask with SQLAlchemy ORM.

pip install flask_sqlalchemy
from flask_sqlalchemy import SQLAlchemy

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)

Module 8: REST APIs with Flask

Build APIs using Flask and jsonify.

from flask import jsonify

@app.route('/api/data')
def get_data():
    data = {'message': 'Hello API'}
    return jsonify(data)

Module 9: Authentication (Flask-Login)

Handle user sessions and login management.

pip install flask-login
from flask_login import LoginManager, UserMixin, login_user

login_manager = LoginManager(app)

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(user_id)

Module 10: Deployment on Render or Heroku

Production setup with gunicorn and requirements.txt.

pip install gunicorn
web: gunicorn app:app

Deploy using Git or Render web dashboard.

No comments:

Post a Comment

#21a Dunder Method - Samples

Python Dunder (Magic) Methods – Complete Guide with Demos Python Dunder (Magic) Methods – Complete Guide with Demos ...