flask

A set of development rules for Flask, a Python framework for building web applications and APIs. It covers application structure, routes, configuration, errors, and database use.

In plain words
What is it for?
Use it when building or reviewing Flask apps, including application factories, blueprints, environment configuration, API errors, logging, and SQLAlchemy database sessions.
Why use it?
It gives Flask projects consistent patterns for organizing features, handling failures, protecting secrets, and returning predictable responses.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/nedcodes-ok/cursorrules-collection/flask
Clone the repo
git clone --depth 1 https://github.com/nedcodes-ok/cursorrules-collection
Per session 465 This file is loaded in full into every session.
When invoked 465 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00465 $0.00465
Opus 5 $0.00233 $0.00233
Sonnet 5 $0.00093 $0.00093
Haiku 4.5 $0.00047 $0.00047

Measured yesterday against content hash 3ea930bb0550, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

flask scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

Origin

Copies of this mod

1 near-identical copy found in the catalogue:

  • flask — 100% identical, 0 lines differ
rules-mdc/frameworks/flask.mdc · 45 lines

How it starts

The opening of the file, as written. The whole thing — 45 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Flask Cursor Rules

You are an expert Flask developer. Follow these rules:

Application Structure

  • Application factory pattern (create_app) for all projects. No module-level app instances
  • One Blueprint per feature domain. Register in create_app, not at import time
  • Config classes per environment: DevelopmentConfig, ProductionConfig, TestingConfig
  • Store secrets in environment variables, load via os.environ.get() with no defaults for required values

Blueprints & Routes

  • Prefix all blueprint URLs: bp = Blueprint('auth', name, url_prefix='/auth')
  • Use @bp.before_request for blueprint-scoped middleware (auth checks, rate limiting)
  • Return consistent JSON responses: {"data": ..., "error": null} or {"data": null, "error": {...}}
  • Use flask.abort() with custom error messages, never raise raw HTTP exceptions

Error Handling

  • Register @app.errorhandler for 400, 401, 403, 404, 422, 500 globally
  • Return JSON error responses with status code, message, and request_id
  • Log exceptions with app.logger, not print(). Configure structured logging
  • Never expose stack traces in production responses

Database (SQLAlchemy)

  • Use Flask-SQLAlchemy with scoped sessions. Call db.session.remove() in teardown
  • Models in app/models/, one file per domain entity
  • Always db.session.commit() in try/except with db.session.rollback() in except
  • Use Flask-Migrate for all schema changes, never db.create_all() in production

Request Handling

  • Validate request data with marshmallow or pydantic, not manual dict checks
  • Use @login_required decorator, never check session manually in route body
  • File uploads: validate content type and size before saving. Use secure_filename()
  • Set CSRF protection on all forms. Use Flask-WTF or manual token validation

Testing

  • Use app.test_client() with application context. Fixture: yield create_app('testing')
  • Separate test database. Use transactions for test isolation, rollback after each test
  • Test error handlers explicitly — don't assume Flask defaults are correct

Read the full file on GitHub · 45 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 45 lines · 465 tokens per session scan A 3ea930bb0550

Subscribe to this mod's changes

flask is a cursor rule published in the GitHub repository nedcodes-ok/cursorrules-collection (37 stars, last pushed 6mo ago), licensed MIT. It adds 465 tokens to every session, about $0.0023 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.