Flask Backend 标志

Flask Backend

社区
j4flmao
flask-backend

Use this skill when building Flask backend applications — lightweight, extensions, blueprints, Jinja2 templates, SQLAlchemy ORM. This skill enforces: blueprint-based modularization, proper application factory pattern, extension initialization, request context management. Do NOT use for: Django projects, FastAPI applications, Tornado async servers.

概览

发布者j4flmao
代码仓库agent-skills
技能名称flask-backend
星标数
41
复刻数
6
附带文件
8
许可协议MIT
链接
  • Markdown 格式的指令

    模型按需加载 SKILL.md 文件,因此只有在请求确实与该技能匹配时才会消耗 Token。

  • 兼容任何大语言模型

    AI 技能采用纯 Markdown,而非特定提供商的代码,因此可搭配 GPT、Claude、Gemini、Grok 或本地模型使用。

  • 8 个附带文件

    模型执行任务时可读取的脚本、模板和参考资料。这些文件为只读文件,绝不会被执行。

  • 开源

    由 j4flmao 发布于 GitHub。请在安装前查看技能的源文件。

安装

在 TypingMind 中安装 Flask Backend AI 技能,即可搭配任意大语言模型使用;也可以将其放入其他支持读取 SKILL.md 的智能体中。

1

安装到 TypingMind

TypingMind 可直接从 GitHub 文件夹安装技能:读取 SKILL.md,打包资源文件,并将结果保存在本地。

  1. 打开应用,前往“插件 → 技能”。
  2. 选择“从 GitHub 安装”。
  3. 粘贴下方的技能文件夹 URL 并确认。
    https://github.com/j4flmao/agent-skills/tree/main/skills/backend/python/flask
  4. 请在每个需要使用该技能的聊天中启用它。
依次选择“插件 → 技能 → 添加技能 → 从 GitHub URL 导入”,然后粘贴文件夹 URL 并点击“继续”。
2

安装到其他智能体

任何支持 Agent Skills 格式的智能体都可以使用此技能,只需将文件夹复制到该智能体的技能目录中。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/j4flmao/agent-skills.git /tmp/agent-skills
mkdir -p .claude/skills
cp -r /tmp/agent-skills/skills/backend/python/flask .claude/skills/flask-backend
复制后,请重启 Claude Code,以加载新技能。

在 TypingMind 中使用此技能

在任意 TypingMind 聊天中启用 Flask Backend,后续即可交由模型处理。技能的名称和描述会放在系统提示词中,一旦请求匹配,模型就会自动加载完整指令,无需您手动调用,且在实际使用前不会消耗 Token。

一旦请求与技能匹配,模型就会自动加载 Flask Backend。

兼容任何 AI 模型

AI 技能是纯 Markdown 指令,而非特定提供商的代码,因此 Flask Backend 不局限于最初适配的模型。只需在 TypingMind 中安装一次,即可搭配 GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 或您自行运行的本地模型使用,全程使用您自己的 API 密钥。

  • 仅在需要时加载

    系统提示词中仅包含技能名称和描述。首次遇到匹配的请求时才会加载完整指令,因此技能未使用时,这些指令不会消耗 Token。

  • 在聊天中切换模型

    技能是指令而非代码,因此切换模型不会导致技能失效,新模型会读取同一个 SKILL.md。

技能指令

这是模型会加载的 SKILL.md 内容。请在安装前阅读,因为技能就是模型将遵循的指令。

Flask Backend

Purpose

Define Flask backend application architecture: lightweight server setup, blueprint organization, extension integration, and WSGI deployment.

Agent Protocol

Trigger

User request includes: flask, flask backend, flask blueprint, flask app factory, flask sqlalchemy, flask extension, flask rest api, python flask.

Input Context

  • Python version (3.10+)
  • Flask version (3.x)
  • Database ORM (SQLAlchemy, Peewee)
  • API style (REST, Flask-RESTx)
  • Template engine (Jinja2, none for SPA)
  • Deployment (Gunicorn, uWSGI, serverless)

Output Artifact

A markdown document containing:

  • Project structure
  • Application factory pattern
  • Blueprint organization
  • Extension initialization pattern
  • Error handling (app.errorhandler)
  • Configuration management
  • Testing (pytest, app.test_client)
  • CLI commands (click integration)

Response Format

Produce the artifact directly. No preamble, no postamble, no explanations. No filler, no hedging. Compress output.

Completion Criteria

  • Application factory creates configurable app instances
  • Blueprints group related routes and templates
  • Extensions initialized via init_app pattern
  • Error handlers registered at app level
  • Tests use app.test_client fixture

Max Response Length

4096 tokens

Workflow

Step 1: Project Setup

bash
pip install flask flask-sqlalchemy flask-migrate pydantic
pip install pytest pytest-cov  # dev
pip install gunicorn  # production
pip install flask-cors flask-limiter redis  # optional

Step 2: Project Structure

project/
+-- app/
|   +-- __init__.py
|   +-- extensions.py
|   +-- config.py
|   +-- models/
|   |   +-- __init__.py
|   |   +-- order.py
|   |   +-- user.py
|   +-- blueprints/
|   |   +-- orders/
|   |   |   +-- __init__.py
|   |   |   +-- routes.py
|   |   |   +-- schemas.py
|   |   |   +-- service.py
|   |   +-- products/
|   |   |   +-- __init__.py
|   |   |   +-- routes.py
|   |   |   +-- service.py
|   |   +-- auth/
|   |   |   +-- __init__.py
|   |   |   +-- routes.py
|   |   |   +-- service.py
|   |   +-- health/
|   |       +-- __init__.py
|   |       +-- routes.py
|   +-- services/
|   |   +-- __init__.py
|   |   +-- order_service.py
|   |   +-- payment_service.py
|   +-- utils/
|   |   +-- __init__.py
|   |   +-- errors.py
|   |   +-- pagination.py
|   |   +-- decorators.py
|   +-- templates/
|       +-- base.html
|       +-- orders/
|           +-- list.html
|           +-- detail.html
+-- migrations/
+-- tests/
|   +-- conftest.py
|   +-- test_orders.py
|   +-- test_health.py
|   +-- factories.py
+-- .env
+-- .flaskenv
+-- requirements.txt
+-- wsgi.py
+-- Dockerfile

Step 3: Application Factory

python
# app/__init__.py
from flask import Flask

def create_app(config_object="app.config.Config") -> Flask:
    app = Flask(__name__)
    app.config.from_object(config_object)
    app.config.from_prefixed_env()

    register_extensions(app)
    register_blueprints(app)
    register_error_handlers(app)
    register_cli_commands(app)

    return app

def register_extensions(app: Flask) -> None:
    from app.extensions import db, migrate
    db.init_app(app)
    migrate.init_app(app, db)
    if app.config.get("SQLALCHEMY_DATABASE_URI"):
        import flask_cors
        flask_cors.CORS(app)

def register_blueprints(app: Flask) -> None:
    from app.blueprints.health.routes import health_bp
    from app.blueprints.orders.routes import orders_bp
    from app.blueprints.auth.routes import auth_bp

    app.register_blueprint(health_bp)
    app.register_blueprint(orders_bp, url_prefix="/api/orders")
    app.register_blueprint(auth_bp, url_prefix="/api/auth")

def register_error_handlers(app: Flask) -> None:
    from app.utils.errors import register_error_handlers as _register
    _register(app)

def register_cli_commands(app: Flask) -> None:
    import click

    @app.cli.command("seed-db")
    def seed_db():
        """Seed database with sample data."""
        click.echo("Seeding database...")

Step 4: Configuration Classes

python
# app/config.py
import os
from datetime import timedelta

basedir = os.path.abspath(os.path.dirname(__file__))

class Config:
    SECRET_KEY = os.environ.get("SECRET_KEY", "change-me-in-production")
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    JSON_SORT_KEYS = False
    MAX_CONTENT_LENGTH = 16 * 1024 * 1024  # 16MB

class DevelopmentConfig(Config):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = os.environ.get(
        "DATABASE_URL", "sqlite:///" + os.path.join(basedir, "..", "dev.db")
    )

class TestingConfig(Config):
    TESTING = True
    SQLALCHEMY_DATABASE_URI = "sqlite:///:memory:"

class ProductionConfig(Config):
    DEBUG = False
    SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL")
    SESSION_COOKIE_SECURE = True
    SESSION_COOKIE_HTTPONLY = True
    SESSION_COOKIE_SAMESITE = "Lax"

config_map = {
    "development": DevelopmentConfig,
    "testing": TestingConfig,
    "production": ProductionConfig,
    "default": DevelopmentConfig,
}

Step 5: Blueprint Pattern with Service Layer

python
# app/blueprints/orders/routes.py
from flask import Blueprint, request, jsonify
from pydantic import ValidationError
from app.services.order_service import OrderService
from app.blueprints.orders.schemas import CreateOrderSchema, OrderResponseSchema
from app.utils.decorators import require_auth

orders_bp = Blueprint("orders", __name__)
order_service = OrderService()

@orders_bp.route("", methods=["POST"])
@require_auth
def create_order():
    data = request.get_json()
    try:
        schema = CreateOrderSchema(**data)
    except ValidationError as e:
        return jsonify(error=e.errors()), 400

    order = order_service.create(schema.model_dump())
    response = OrderResponseSchema.model_validate(order).model_dump()
    return jsonify(response), 201

@orders_bp.route("", methods=["GET"])
@require_auth
def list_orders():
    page = request.args.get("page", 1, type=int)
    per_page = request.args.get("per_page", 20, type=int)
    orders, total = order_service.list_paginated(page=page, per_page=per_page)
    items = [OrderResponseSchema.model_validate(o).model_dump() for o in orders]
    return jsonify({"items": items, "total": total, "page": page, "per_page": per_page})

@orders_bp.route("/<uuid:order_id>", methods=["GET"])
@require_auth
def get_order(order_id):
    order = order_service.get_by_id(str(order_id))
    if not order:
        return jsonify(error="Order not found"), 404
    response = OrderResponseSchema.model_validate(order).model_dump()
    return jsonify(response)

@orders_bp.route("/<uuid:order_id>", methods=["DELETE"])
@require_auth
def delete_order(order_id):
    order_service.delete(str(order_id))
    return "", 204

Step 6: Service Layer

python
# app/services/order_service.py
from app.extensions import db
from app.models.order import Order
from typing import Optional

class OrderService:
    def create(self, data: dict) -> Order:
        order = Order(**data)
        db.session.add(order)
        db.session.commit()
        return order

    def get_by_id(self, order_id: str) -> Optional[Order]:
        return Order.query.get(order_id)

    def list_paginated(self, page: int = 1, per_page: int = 20):
        pagination = Order.query.order_by(Order.created_at.desc()).paginate(
            page=page, per_page=per_page, error_out=False
        )
        return pagination.items, pagination.total

    def update(self, order_id: str, data: dict) -> Optional[Order]:
        order = self.get_by_id(order_id)
        if not order:
            return None
        for key, value in data.items():
            setattr(order, key, value)
        db.session.commit()
        return order

    def delete(self, order_id: str) -> bool:
        order = self.get_by_id(order_id)
        if not order:
            return False
        db.session.delete(order)
        db.session.commit()
        return True

Step 7: Pydantic Schemas

python
# app/blueprints/orders/schemas.py
from pydantic import BaseModel, Field, UUID4
from typing import List, Optional
from datetime import datetime

class OrderItemSchema(BaseModel):
    product_id: str
    quantity: int = Field(ge=1)
    unit_price: float = Field(gt=0)

class CreateOrderSchema(BaseModel):
    customer_id: str = Field(min_length=1)
    items: List[OrderItemSchema] = Field(min_length=1)
    coupon_code: Optional[str] = None

class OrderResponseSchema(BaseModel):
    id: str
    customer_id: str
    status: str
    total_amount: float
    created_at: datetime

    model_config = {"from_attributes": True}

Step 8: Error Handling

python
# app/utils/errors.py
from flask import jsonify

class AppError(Exception):
    def __init__(self, message: str, status_code: int = 400, code: str = "APP_ERROR"):
        self.message = message
        self.status_code = status_code
        self.code = code

def register_error_handlers(app):
    @app.errorhandler(AppError)
    def handle_app_error(error):
        return jsonify(code=error.code, message=error.message), error.status_code

    @app.errorhandler(400)
    def bad_request(error):
        return jsonify(code="BAD_REQUEST", message="Invalid request"), 400

    @app.errorhandler(401)
    def unauthorized(error):
        return jsonify(code="UNAUTHORIZED", message="Authentication required"), 401

    @app.errorhandler(403)
    def forbidden(error):
        return jsonify(code="FORBIDDEN", message="Access denied"), 403

    @app.errorhandler(404)
    def not_found(error):
        return jsonify(code="NOT_FOUND", message="Resource not found"), 404

    @app.errorhandler(405)
    def method_not_allowed(error):
        return jsonify(code="METHOD_NOT_ALLOWED", message="Method not allowed"), 405

    @app.errorhandler(422)
    def unprocessable(error):
        return jsonify(code="UNPROCESSABLE", message="Unprocessable entity"), 422

    @app.errorhandler(429)
    def too_many_requests(error):
        return jsonify(code="RATE_LIMITED", message="Too many requests"), 429

    @app.errorhandler(500)
    def internal_error(error):
        return jsonify(code="INTERNAL", message="Unexpected error"), 500

Step 9: Testing

python
# tests/conftest.py
import pytest
from app import create_app
from app.config import TestingConfig

@pytest.fixture
def app():
    app = create_app(TestingConfig)
    with app.app_context():
        from app.extensions import db
        db.create_all()
        yield app
        db.drop_all()

@pytest.fixture
def client(app):
    return app.test_client()

@pytest.fixture
def auth_headers(client):
    # Create user and get token
    response = client.post("/api/auth/login", json={
        "email": "test@example.com",
        "password": "password123"
    })
    data = response.get_json()
    return {"Authorization": f"Bearer {data['access_token']}"}

# tests/test_orders.py
def test_create_order(client, auth_headers):
    response = client.post("/api/orders", json={
        "customer_id": "cust-1",
        "items": [{"product_id": "prod-1", "quantity": 2, "unit_price": 19.99}]
    }, headers=auth_headers)
    assert response.status_code == 201
    data = response.get_json()
    assert data["customer_id"] == "cust-1"
    assert data["total_amount"] == 39.98

def test_get_nonexistent_order(client, auth_headers):
    response = client.get("/api/orders/nonexistent-id", headers=auth_headers)
    assert response.status_code == 404

Architecture Decision Trees

Blueprint Organization

Domain features > 5?
  +-- Yes -> One blueprint per bounded context (orders, users, payments)
  +-- No  -> One blueprint for all routes, but still use blueprint() for testability

Database ORM

Need async performance?
  +-- Yes -> SQLAlchemy 2.0 async with asyncio. Flask async support experimental.
  +-- No  -> Need migration support?
      +-- Yes -> SQLAlchemy + Alembic (flask-migrate)
      +-- No  -> Peewee (simpler, less boilerplate)

Template vs SPA

Server-rendered HTML?
  +-- Yes -> Jinja2 templates with layout inheritance, macros
  +-- No  -> Flask as pure JSON API, SPA handles rendering

Common Pitfalls

  1. Global flask app instance: Creating app = Flask(__name__) at module level prevents multiple app instances for testing. Always use application factory.

  2. Circular imports with db and models: Defining db in models and importing models in extensions creates cycles. Solution: extensions.py for db, models import db.

  3. Request context outside of request: Accessing request, g, current_app outside of request context raises RuntimeError. Push context explicitly for background tasks.

  4. Session per request not configured correctly: Flask-SQLAlchemy session scoped to request by default. Manual session management needed for background thread work.

  5. Not closing database connections: Flask-SQLAlchemy handles this, but raw connections from psycopg2 or direct engine usage must be closed.

  6. Storing secrets in config files: Environment variables for secrets. Use .env with python-dotenv. Never commit secrets.

  7. No CORS configuration for API: API consumed by browser-based SPA needs flask-cors with proper origin whitelist.

  8. Overusing before_request: Complex logic in @app.before_request makes testing difficult. Extract into middleware or decorators.

  9. Blueprint with duplicate endpoint names: Endpoint names must be unique across blueprints. Use blueprint name prefix.

  10. JSON serialization errors: Custom types (UUID, datetime) not JSON serializable. Override json.JSONEncoder or use model_dump() with Pydantic.

Best Practices

  1. Application factory for every project — never create Flask() at module level.
  2. Blueprints for all route grouping — never free-floating @app.route.
  3. Extensions initialized via init_app pattern for testability.
  4. Config from class hierarchy (Config, DevConfig, ProdConfig) + env override.
  5. Pydantic for request validation — never manual dict parsing.
  6. pytest fixtures for app and client — never global app instance.
  7. Service layer between routes and models for business logic isolation.
  8. Type hints on all function signatures for IDE support and static analysis.
  9. Alembic migrations for all schema changes, never Flask.create_all() in production.
  10. Before/after request hooks for request-scoped concerns (DB session, auth context).

Compared With

FeatureFlaskFastAPIDjango
Async supportLimitedNativeLimited (3.0+)
Performance~5k req/s~25k req/s~4k req/s
Built-in ORMNo (SQLAlchemy)No (SQLAlchemy)Yes (Django ORM)
Admin interfaceNo (Flask-Admin)NoBuilt-in
ValidationPydantic/ManualPydantic (built-in)Django Forms/DRF
API documentationNoOpenAPI (auto)DRF (manual)
Template engineJinja2 (built-in)Jinja2Django Templates
Learning curveLowLowModerate
DeploymentWSGI (Gunicorn)ASGI (Uvicorn)WSGI/ASGI
Extensions1000+GrowingBatteries-included

Performance

  • Flask handles ~5,000 req/s on basic hardware (single worker). Scale with Gunicorn workers (2-4 x CPU cores).
  • SQLAlchemy connection pooling: configure SQLALCHEMY_ENGINE_OPTIONS with pool_size and max_overflow.
  • Jinja2 template caching: app.jinja_env.auto_reload = False in production.
  • Response compression: Add Flask-Compress for gzip/brotli.
  • Redis caching with Flask-Caching for expensive queries.
  • Database query optimization: eager loading with joinedload(), indexing on foreign keys.
  • Gunicorn with gevent workers for I/O-bound workloads.

Tooling

ToolPurpose
Flask-SQLAlchemyORM integration
Flask-Migrate (Alembic)Database migrations
PydanticRequest/response validation
Flask-CORSCross-origin requests
Flask-LimiterRate limiting
PytestTesting framework
BlackCode formatting
RuffLinting (replaces Flake8)
MypyType checking
GunicornProduction WSGI server
DockerContainerization
Poetry / pip-toolsDependency management
Flask-DebugToolbarDevelopment debugging

Rules

  • Application factory for every project — never create Flask() at module level.
  • Blueprints for all route grouping — never free-floating @app.route.
  • Extensions initialized via init_app pattern for testability.
  • Config from class hierarchy (Config, DevConfig, ProdConfig) + env override.
  • Pydantic for request validation — never manual dict parsing.
  • pytest fixtures for app and client — never global app instance.
  • Alembic migrations for schema changes — never create_all in production.
  • Service layer isolates business logic from route handlers.
  • Error handlers registered at app level for consistent error responses.
  • Type hints required on all public function signatures.
  • CORS configured explicitly per environment, never wide open.
  • Rate limiting on authentication endpoints to prevent brute force.

References

  • references/flask-application-factory.md — Flask Application Factory Pattern
  • references/flask-restful-api-design.md — Flask RESTful API Design
  • references/flask-blueprints-factories.md — Flask Blueprints and Application Factories
  • references/flask-deployment.md — Flask Deployment
  • references/flask-extensions.md — Flask Extensions
  • references/flask-security.md — Flask Security Reference
  • references/flask-setup.md — Flask Setup Guide
  • references/flask-testing.md — Flask Testing Reference

Handoff

Hand off to backend/universal/api-response/SKILL.md for API response standards.

附带文件

技能加载后,模型会按需读取这些文件。它们仅作为可读文件提供,绝不会被执行。

常见问题

Flask Backend AI 技能有什么作用?

Use this skill when building Flask backend applications — lightweight, extensions, blueprints, Jinja2 templates, SQLAlchemy ORM. This skill enforces: blueprint-based modularization, proper application factory pattern, extension initialization, request context management. Do NOT use for: Django projects, FastAPI applications, Tornado async servers.

为什么在 TypingMind 中使用 Flask Backend?

因为您只需安装一次,就能搭配任何模型使用。Flask Backend 采用纯 Markdown 格式,而非特定提供商专用的代码,因此同一技能可用于 GPT-5、Claude、Gemini、Grok 或本地模型。您甚至可以在聊天中途切换模型,而不会影响技能的正常使用。TypingMind 使用您自己的 API 密钥,因此您直接向提供商付费,而不是按席位订阅;您的技能和聊天记录也都保留在您自己的存储空间中。

如何在 TypingMind 中安装 Flask Backend?

在 TypingMind 中打开“插件 → 技能 → 从 GitHub 安装”,然后粘贴 https://github.com/j4flmao/agent-skills/tree/main/skills/backend/python/flask。TypingMind 会读取其中的 SKILL.md,打包相关文件,并将其安装为技能,供您在各个聊天中按需启用。

哪些 AI 模型可以使用 Flask Backend?

您在 TypingMind 中连接的任何模型都可以。AI 技能采用纯 Markdown 指令,而非特定提供商专用的代码,因此 GPT、Claude、Gemini、Grok 和本地模型都能在请求匹配时加载此技能。

Flask Backend 可以搭配多少个 AI 模型使用?

数量不限。只要模型支持技能,您就可以将它与 Flask Backend 搭配使用。GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 等模型都可以通过您自己的 API 密钥接入 TypingMind。

Flask Backend AI 技能免费吗?

是的。j4flmao 已在 GitHub 上根据 MIT 许可证发布了该技能。您只需向自己的 AI 提供商支付所用 Token 的费用。

什么是 AI 技能?

AI 技能是一组可复用的指令,用于教会 AI 模型完成某项特定任务。它遵循开放的 Agent Skills 格式:包含名称和描述的 SKILL.md 文件,以及模型可能需要的脚本、模板或参考文件。模型仅在您的请求与技能匹配时才会读取这些指令,因此已安装的技能在使用前不会产生任何费用。

AI 技能与插件或 MCP 服务器有何不同?

插件或 MCP 服务器为模型提供可调用的新工具,也就是在某处运行并返回结果的代码。AI 技能则为模型提供知识和流程:如何处理任务、遵循哪些步骤,以及怎样的输出才算优质。技能采用纯 Markdown 格式,因此不需要服务器、API 密钥或运行时环境,并且适用于任何模型。

查看全部

立即创建您自己的 AI 工作区

订阅我们的电子简报,获取新功能和后续赠奖活动的通知 👇