Skills2026年4月12日·1 分钟阅读

Flask — The Python Micro Web Framework

Flask is a lightweight WSGI web application framework for Python. Designed to make getting started quick and easy, with the ability to scale up to complex applications. The minimalist counterpart to Django, trusted by Netflix, LinkedIn, and Pinterest.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 64/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
step-1.md
先审查命令
npx -y tokrepo@latest install 65c0de18-3630-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

TL;DR
Flask is a lightweight Python web framework for building APIs and web apps. Quick to start, extensible to complex applications.
§01

What it is

Flask is a lightweight WSGI web application framework for Python. It provides routing, request handling, templating (Jinja2), and session management with minimal overhead. Flask follows a microframework philosophy: include only what you need, add extensions for everything else.

Flask is the minimalist counterpart to Django. Where Django provides an all-in-one solution with ORM, admin, and auth built in, Flask gives you a foundation and lets you choose your own database layer, authentication system, and project structure.

§02

How it saves time or tokens

Flask gets a web server running in under 10 lines of Python. No project scaffolding, no configuration files, no boilerplate. For prototyping, this speed-to-first-request is unmatched.

Flask is one of the most documented Python frameworks. AI coding assistants produce accurate Flask code reliably because the patterns are simple and well-represented in training data.

Additionally, the project's well-structured documentation and active community mean developers spend less time troubleshooting integration issues. When AI coding assistants generate code for this tool, they can reference established patterns from the documentation, producing correct implementations with fewer iterations and lower token costs.

§03

How to use

  1. Install Flask:
pip install flask
  1. Create a minimal application:
from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route('/api/hello')
def hello():
    name = request.args.get('name', 'World')
    return jsonify({'message': f'Hello, {name}'})

if __name__ == '__main__':
    app.run(debug=True)
  1. Add extensions as needed: Flask-SQLAlchemy for database, Flask-Login for authentication, Flask-CORS for cross-origin requests.
  1. Deploy with Gunicorn or uWSGI behind Nginx for production.
§04

Example

from flask import Flask, jsonify, request
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
db = SQLAlchemy(app)

class Task(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=False)
    done = db.Column(db.Boolean, default=False)

@app.route('/api/tasks', methods=['GET'])
def list_tasks():
    tasks = Task.query.all()
    return jsonify([{'id': t.id, 'title': t.title, 'done': t.done} for t in tasks])
§05

Related on TokRepo

§06

Common pitfalls

  • Using Flask's built-in development server in production. Always deploy with Gunicorn or uWSGI. The development server is single-threaded and not designed for production traffic.
  • Not using application factories for larger projects. As Flask apps grow, the factory pattern (create_app function) prevents circular imports and enables testing.
  • Ignoring Flask-Migrate for database schema changes. Without migration tracking, schema changes require manual SQL or database recreation.
  • Failing to review community discussions and changelogs before upgrading. Breaking changes in major versions can disrupt existing workflows. Pin versions in production and test upgrades in staging first.

常见问题

When should I choose Flask over Django?+

Choose Flask when you want full control over your stack, need a lightweight API server, or prefer choosing your own ORM and auth system. Choose Django when you want an all-in-one framework with admin panel, ORM, and auth built in. Flask is better for microservices and APIs; Django is better for full-stack web applications.

Is Flask suitable for production applications?+

Yes. Flask powers production applications at companies including Netflix, LinkedIn, and Pinterest. Deploy with Gunicorn or uWSGI behind Nginx, use a proper database, and add monitoring. Flask itself is production-ready; the development server is not.

How does Flask handle database access?+

Flask does not include a database layer. The most common extension is Flask-SQLAlchemy, which integrates SQLAlchemy ORM with Flask. You can also use raw SQL, PyMongo for MongoDB, or any other Python database library.

Does Flask support async/await?+

Flask 2.0+ supports async route handlers and views. You can use async def for route functions. However, Flask's WSGI foundation means true async performance requires an ASGI server. For heavy async workloads, consider FastAPI or Starlette.

Can Flask build REST APIs?+

Yes. Flask is widely used for REST APIs. Use flask.jsonify for JSON responses, request.get_json() for parsing request bodies, and Flask-RESTful or Flask-Smorest for structured API development with validation and documentation.

引用来源 (3)

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产