烧瓶

其他 社区

技能解读

当前原文的自动摘译,尚未人工审校。

项目结构

使用应用程序工厂模式(create_app())来避免全局状态并启用测试。将功能组织到蓝图中;在工厂注册。将配置保存在具有 DevelopmentConfig、ProductionConfig 等类的 config.py 中。

路由和视图

与通用的 @app.route 和 methods=[...] 相比,更喜欢显式 HTTP 方法装饰器(@app.get、@app.post)。尽早验证请求数据;在处理之前,对于格式错误的输入返回 400 错误。使用 flask.abort() 和适当的 HTTP 代码,而不是手动返回错误响应。

扩展

在工厂内使用 ext.init_app(app) 延迟初始化扩展,而不是在模块级别。常见扩展:Flask-SQLAlchemy、Flask-Migrate、Flask-Login、Flask-WTF、Flask-CORS。

测试

使用 app.test_client() 进行 HTTP 级别测试,使用 app.test_request_context() 进行单元测试。使用 pytest 装置创建应用程序和客户端;适当的范围(session对于应用程序,function对于客户端)。设置TESTING=True并使用单独的测试数据库。

陷阱

切勿在生产中使用开发服务器(app.run())——使用 Gunicorn 或 uWSGI。避免在 app 对象上存储可变状态;对请求范围的数据使用g。永远不要硬编码SECRET_KEY——从环境变量加载。

流狐档案 运行与权限由流狐静态检查推断,不代表作者声明或实际测试结果。
领域
通用
作者声明的智能体
未找到明确声明;不据此推断已兼容或已测试
静态检查
94 / 100 · 启发式扫描,不代表运行安全
运行环境证据
未执行;当前没有隔离运行测试证据
作者 / 版本 / 许可证
@microsoft · NOASSERTION
上下文用量评级
低消耗
接入复杂程度
需简单配置
是否需要外部接口密钥
需要 · Vendor-specific
兼容的系统
未声明(默认跨平台)
运行环境要求
未检出明确要求
检测到的文件与系统行为
  • 只读
  • 允许写入 / 修改
  • 读取环境变量
网络行为
允许外网请求
安装命令数
无(仅作为资料)

需要注意: 未声明工具权限范围;实际权限由运行环境决定。

作者说明文件(原文) SKILL.md

# Skill: Flask

Best practices for Flask web development including routing, blueprints, and testing.

## When to Use

Apply this skill when building Flask web applications or APIs — routing, blueprints, extensions, and testing.

## Project Structure

-   Use the application factory pattern (`create_app()`) to avoid global state and enable testing.
-   Organize features into Blueprints; register them in the factory.
-   Keep configuration in a `config.py` with classes like `DevelopmentConfig`, `ProductionConfig`.

## Routing and Views

-   Prefer explicit HTTP method decorators (`@app.get`, `@app.post`) over generic `@app.route` with `methods=[...]`.
-   Validate request data early; return 400 errors for malformed input before processing.
-   Use `flask.abort()` with appropriate HTTP codes rather than returning error responses manually.

## Extensions

-   Initialize extensions lazily with `ext.init_app(app)` inside the factory, not at module level.
-   Common extensions: Flask-SQLAlchemy, Flask-Migrate, Flask-Login, Flask-WTF, Flask-CORS.

## Testing

-   Use `app.test_client()` for HTTP-level tests and `app.test_request_context()` for unit tests.
-   Use pytest fixtures to create the app and client; scope appropriately (`session` for the app, `function` for the client).
-   Set `TESTING=True` and use a separate test database.

## Pitfalls

-   Never use the development server (`app.run()`) in production — use Gunicorn or uWSGI.
-   Avoid storing mutable state on the `app` object; use `g` for request-scoped data.
-   Never hardcode `SECRET_KEY` — load from environment variables.

讨论

基于 GitHub Discussions。登录 GitHub 即可参与讨论、点赞、订阅更新。