Back to Rules
Python

Flask Python Best Practices: Cursor Rules for Scalable API Development

Claude Directory November 29, 2025
0 copies 3 downloads

Unlock expert Cursor rules for Flask Python to build high-performance, modular APIs. Covering blueprints, error handling, JWT auth, testing, and deployment for production-ready apps.

Rule Content
- **Core Development Principles**
  - Deliver precise, code-focused replies with practical Python snippets.
  - Embrace functional and declarative styles; limit classes to Flask view handlers.
  - Promote reusability through loops and modules to eliminate repetition.
  - Choose clear names incorporating helper verbs like is_valid or can_access.
  - Adopt snake_case for folders and files, such as blueprints/auth_endpoints.py.
  - Export routes and helpers explicitly by name.
  - Apply Receive Object, Return Object (RORO) for clean data flow.

- **Python and Flask Coding Standards**
  - Define functions with def and add type annotations everywhere feasible.
  - Organize projects into app init, blueprints, models, helpers, and settings folders.
  - Skip braces in simple if blocks; use one-liners like if check: action().

- **Robust Error Management and Input Checks**
  - Tackle exceptions and boundaries first in methods.
  - Employ early exits for failures to flatten code structure.
  - Position success logic at the end for better flow.
  - Ditch redundant else; opt for if-then-return.
  - Leverage guard patterns for quick invalid state rejection.
  - Log issues thoroughly while sending clear user feedback.
  - Standardize errors via custom classes or factories.

- **Essential Libraries**
  - Flask for core framework.
  - Flask-RESTful for API endpoints.
  - Flask-SQLAlchemy for database ORM.
  - Flask-Migrate for schema updates.
  - Marshmallow for data conversion and checks.
  - Flask-JWT-Extended for token-based security.

- **Flask Framework Tips**
  - Build apps via factories for easier testing and scaling.
  - Group endpoints in blueprints for clean separation.
  - Craft REST APIs with Flask-RESTful class views.
  - Set global handlers for exceptions.
  - Hook into request phases with before_request, after_request, teardown_request.
  - Configure via app.config for env-specific setups (dev, test, prod).
  - Log with app.logger.
  - Secure routes using JWT extensions.

- **Speed and Efficiency Boosts**
  - Cache hot data with Flask-Caching.
  - Optimize DB queries: preload relations, add indexes.
  - Pool connections for DB reliability.
  - Manage sessions correctly to prevent leaks.
  - Offload heavy work to async queues like Celery.

- **Standard Practices**
  - Respect Flask contexts for app and requests.
  - Monitor API metrics: speed, delay, capacity.
  - Architect with blueprints, distinct layers (routes, logic, data), and env vars.

- **DB Operations**
  - Handle models and queries via Flask-SQLAlchemy.
  - Migrate schemas safely with Flask-Migrate.
  - Close sessions post-operation.

- **Data Handling**
  - Validate and serialize with Marshmallow schemas per model.

- **Security Features**
  - JWT auth powered by Flask-JWT-Extended.
  - Guard protected paths with auth decorators.

- **Quality Assurance**
  - Unit test with pytest.
  - Integration via Flask test client.
  - Setup reusable fixtures for DB and app.

- **API Specs**
  - Document with Flask-RESTX or Flasgger for OpenAPI.
  - Detail all paths with input/output models.

- **Production Rollout**
  - Serve via Gunicorn or uWSGI.
  - Enable logging and observability.
  - Secure secrets in environment variables.

Comments

More Rules

View all
AI/ML

GLM-4.7 Optimized Config & System Prompt Designer

Expert system prompt for designing high-performance configurations tailored to GLM-4.7's strengths in coding, reasoning, tool use, and multilingual tasks, backed by benchmarks like SWE-bench and τ²-Bench.

C
Community
AI/ML

GLM-4.7 Open-Source Coding Expert: Optimized System Prompt

Leverage GLM-4.7's top benchmarks in SWE-bench, LiveCodeBench, and more with this system prompt designed for generating clean, secure, open-source-ready code, stunning UIs, and agentic workflows.

C
Community
AI/ML

GLM-4.7 Optimized Coding Agent

This system prompt transforms an AI into GLM-4.7, a benchmark-leading coding agent excelling in agentic workflows, tool use, multilingual coding, and complex reasoning with verified best practices for production-ready open-source development.

C
Community
DevOps

Agentic Dev Loop: Autonomous Jira-Driven Coding Agent with GitHub CI Self-Healing

Ralph, a persistent autonomous AI agent, implements Jira tickets through an endless loop until 100% test success, with GitHub PRs, Jules AI reviews, and CI self-healing for reliable development workflows.

C
Claude Directory
AI/ML

Türk Hukuku Uzmanı AI Agent: Güvenilir Yasal Danışman System Prompt

Claude'u Türk hukuku alanında dünyanın en önde gelen uzmanı olarak yapılandıran, yapılandırılmış yanıtlar, zorunlu uyarılar ve etik sınırlarla donatılmış profesyonel AI agent promptu.

C
Community
Database

PostgreSQL Best Practices: Expert Subagent Guide

Expert subagent providing production-ready PostgreSQL guidance on schema design, query optimization, security, performance tuning, and administration with structured, actionable advice and official references.

C
Claude Directory