CodeAlchemy

Jotting one man's journey through software development, programming, and technology


Project maintained by pablogarciaprado Hosted on GitHub Pages — Theme by mattgraham

◀️ Home

Flask

Basic concepts

1) What Flask is (and is not)

In a simple project’s app.py:


2) Tiny web request diagram

Browser (JS/HTML)
   |   HTTP request (GET /, POST /submit)
   v
Flask route in app.py
   |   calls business logic (ConfigStore/modules)
   v
Response (HTML / JSON / Redirect)
   |
   v
Browser renders UI or handles JSON

Mental model:

  1. Client sends request.
  2. Flask picks matching route + method.
  3. Python code runs.
  4. Flask returns response.

3) JavaScript basics (only what you need for Flask)


4) Basic Flask app structure

Typical shape:

  1. Imports
  2. App creation and config
  3. Routes/endpoints
  4. Local run block (if __name__ == "__main__":)

In a simple app.py:


5) Routing and endpoints

Example patterns for app.py:


6) Requests and responses

Common request sources:

Common response types:

Session example in this app:


7) Templates (Jinja2)

Rule of thumb:


8) Project structure cheat sheet

Example repo:

Why this matters:


9) Glossary


Blueprint

A Flask Blueprint is a way to organize and structure a Flask application by grouping related routes, views, and other logic. It helps break a large application into smaller, modular components, making it easier to manage and maintain.

from flask import Blueprint

# Create a Blueprint instance
auth_bp = Blueprint('auth', __name__)

@auth_bp.route('/login')
def login():
    return "Login Page"

@auth_bp.route('/logout')
def logout():
    return "Logout Page"

# Register the Blueprint in the main app
from flask import Flask
app = Flask(__name__)
app.register_blueprint(auth_bp, url_prefix='/auth')

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