Flask-style API layer running on FastAPI
Project description
foxar
A 100% Flask-compatible library built on top of FastAPI, providing the best of both worlds: Flask's simplicity and FastAPI's performance.
Features
一个兼容Flask语法的FastAPI高性能库,结合了Flask的简洁语法和FastAPI的高性能特性。
特性
- Flask兼容语法:支持Flask的路由装饰器、蓝图系统和应用结构
- FastAPI高性能:底层使用FastAPI,支持异步处理和类型提示
- 自动API文档:集成Swagger UI和ReDoc,自动生成API文档
- 类型安全:支持Pydantic模型和类型提示
- 易于迁移:允许现有Flask应用轻松迁移到高性能的FastAPI
安装
pip install foxar
基本使用
from foxar import Foxar, jsonify
app = Foxar(__name__)
@app.route('/')
def index():
return 'Hello, foxar!'
@app.route('/user/<int:user_id>')
def get_user(user_id):
return {'user_id': user_id, 'message': f'Hello user {user_id}'}
@app.route('/login', methods=['GET', 'POST'])
def login():
return jsonify({'status': 'success', 'message': 'Login successful'})
if __name__ == '__main__':
app.run(debug=True, port=5000)
蓝图使用
from foxar import Foxar, Blueprint
app = Foxar(__name__)
# 创建蓝图
api_bp = Blueprint('api', __name__, url_prefix='/api')
@api_bp.route('/items')
def get_items():
return {'items': ['item1', 'item2', 'item3']}
@api_bp.route('/items/<item_id>')
def get_item(item_id):
return {'item_id': item_id, 'name': f'Item {item_id}'}
# 注册蓝图
app.register_blueprint(api_bp)
if __name__ == '__main__':
app.run(debug=True, port=5000)
运行应用
使用内置run方法
app.run(debug=True, port=5000)
使用uvicorn
uvicorn your_app:app --reload --port 5000
访问API文档
- Swagger UI: http://localhost:5000/docs
- ReDoc: http://localhost:5000/redoc
主要组件
- Foxar:主应用类,继承自FastAPI,兼容Flask的初始化和路由语法
- Blueprint:蓝图类,封装了FastAPI的APIRouter
- request:请求对象,兼容Flask的request接口
- Response:响应类,兼容Flask的Response接口
- jsonify:JSON响应工具函数
- redirect:重定向工具函数
- Config:配置类,兼容Flask的Config接口
- url_for:URL生成工具函数
注意事项
- 虽然兼容Flask的基本语法,但某些Flask特有功能可能需要额外实现 (当然,你如果实现了那就是太棒了!欢迎提交PR)
- 支持异步和同步路由处理函数
- 保持了FastAPI的所有高性能特性和类型提示
- 自动生成OpenAPI文档,提供交互式API测试界面
性能优势
- 异步支持:底层使用FastAPI和Starlette,支持异步处理
- 类型提示:利用Python类型提示提高代码质量
- 自动验证:基于Pydantic的请求数据验证
- 高性能:比传统WSGI框架(如Flask)具有更高的并发处理能力
foxar为开发者提供了一种既熟悉又高效的Web框架选择,让你可以使用Flask的简洁语法,同时享受FastAPI的高性能特性。
Current Compatible Features
- Basic routing decorator
@app.route() - Blueprint support
- Request objects
requestandgobject - Response objects
Responsefamily - Session management
session - Message flashing
flash - Error handling
errorhandler - Hook functions
before_requestandafter_request - Configuration management
Config - URL generation
url_for - Template rendering
render_template - Static file serving
- URL mapping
url_mapproperty
Upcoming Features
- Core Context Management:
app_context(),request_context(),current_appproxy - Session Management Enhancements: Session persistence,
session.permanent,session.modified - Error Handling Improvements:
abort()function, enhanced error handling - Routing System Enhancements: Route aliases, route redirects, HTTP method support
- Request Object Improvements:
request.is_json,request.endpoint,request.view_args - Response Object Enhancements: Additional
Responsemethods, improvedmake_response() - Utility Functions:
safe_join(),send_file(),url_quote(),url_unquote(),escape() - Configuration System Enhancements: Environment variable support, configuration validation
- Test Client Improvements: Fully compatible Flask test client API
- Signal System Improvements: More Flask standard signals
- Command Line Interface: Flask CLI compatible interface
- Blueprint System Enhancements: Blueprint nesting, blueprint-level decorators
- Template System Enhancements: Template filters, global variables, context processors
- Static File Service Enhancements: Cache control, file compression
- Security Features: CSRF protection, XSS protection, CORS support
- Other Flask Features:
app.debug,app.testing, enhanced message flashing,gobject methods
Quick Start
from foxar import Foxar
app = Foxar(__name__)
@app.route('/')
def index():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
Installation
pip install foxar
Why foxar?
- 100% Flask Compatible: Write Flask code and run it on FastAPI
- Performance: Leverage FastAPI's asynchronous capabilities and automatic OpenAPI documentation
- Modern Python: Take advantage of FastAPI's type hints and modern Python features
- Easy Migration: Migrate existing Flask applications with minimal changes
- Extensible: Use all FastAPI features alongside Flask-compatible code
Documentation
For more information, please refer to the documentation.
Contributing
Contributions are welcome! Please see the contributing guidelines for more information.
License
foxar is licensed under the AGPLv3 License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file foxar-0.0.0.tar.gz.
File metadata
- Download URL: foxar-0.0.0.tar.gz
- Upload date:
- Size: 34.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8def4be420be43030e3e02d23d4df6aef21940c7d1f5401d5e3d494a67589035
|
|
| MD5 |
3f2a040e965db52ca2b037da3f9daaa1
|
|
| BLAKE2b-256 |
d0ffeee3bda1547c4e64ff9b08a1ccbb396aa3983c5337fc1dd54859906cfa66
|
Provenance
The following attestation bundles were made for foxar-0.0.0.tar.gz:
Publisher:
publish.yml on shunianssy/foxar
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
foxar-0.0.0.tar.gz -
Subject digest:
8def4be420be43030e3e02d23d4df6aef21940c7d1f5401d5e3d494a67589035 - Sigstore transparency entry: 926463503
- Sigstore integration time:
-
Permalink:
shunianssy/foxar@15c1717ce524b087e29f914ae7c5959f1cd17d9a -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/shunianssy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@15c1717ce524b087e29f914ae7c5959f1cd17d9a -
Trigger Event:
push
-
Statement type:
File details
Details for the file foxar-0.0.0-py3-none-any.whl.
File metadata
- Download URL: foxar-0.0.0-py3-none-any.whl
- Upload date:
- Size: 36.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
144f0ddd3faee810438cef3d1adec50385b11fc7ab0dd5ec92811a43145ef43e
|
|
| MD5 |
a35cae51e7414bc8d5d297089c58522d
|
|
| BLAKE2b-256 |
450cf57d58fb3f038a10e97626c176a64eba3c90ffdb8a41969577907e0b423e
|
Provenance
The following attestation bundles were made for foxar-0.0.0-py3-none-any.whl:
Publisher:
publish.yml on shunianssy/foxar
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
foxar-0.0.0-py3-none-any.whl -
Subject digest:
144f0ddd3faee810438cef3d1adec50385b11fc7ab0dd5ec92811a43145ef43e - Sigstore transparency entry: 926463555
- Sigstore integration time:
-
Permalink:
shunianssy/foxar@15c1717ce524b087e29f914ae7c5959f1cd17d9a -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/shunianssy
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@15c1717ce524b087e29f914ae7c5959f1cd17d9a -
Trigger Event:
push
-
Statement type: