Skip to main content

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文档

主要组件

  1. Foxar:主应用类,继承自FastAPI,兼容Flask的初始化和路由语法
  2. Blueprint:蓝图类,封装了FastAPI的APIRouter
  3. request:请求对象,兼容Flask的request接口
  4. Response:响应类,兼容Flask的Response接口
  5. jsonify:JSON响应工具函数
  6. redirect:重定向工具函数
  7. Config:配置类,兼容Flask的Config接口
  8. 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 request and g object
  • Response objects Response family
  • Session management session
  • Message flashing flash
  • Error handling errorhandler
  • Hook functions before_request and after_request
  • Configuration management Config
  • URL generation url_for
  • Template rendering render_template
  • Static file serving
  • URL mapping url_map property

Upcoming Features

  • Core Context Management: app_context(), request_context(), current_app proxy
  • 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 Response methods, improved make_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, g object 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?

  1. 100% Flask Compatible: Write Flask code and run it on FastAPI
  2. Performance: Leverage FastAPI's asynchronous capabilities and automatic OpenAPI documentation
  3. Modern Python: Take advantage of FastAPI's type hints and modern Python features
  4. Easy Migration: Migrate existing Flask applications with minimal changes
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

foxar-0.0.0.tar.gz (34.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

foxar-0.0.0-py3-none-any.whl (36.9 kB view details)

Uploaded Python 3

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

Hashes for foxar-0.0.0.tar.gz
Algorithm Hash digest
SHA256 8def4be420be43030e3e02d23d4df6aef21940c7d1f5401d5e3d494a67589035
MD5 3f2a040e965db52ca2b037da3f9daaa1
BLAKE2b-256 d0ffeee3bda1547c4e64ff9b08a1ccbb396aa3983c5337fc1dd54859906cfa66

See more details on using hashes here.

Provenance

The following attestation bundles were made for foxar-0.0.0.tar.gz:

Publisher: publish.yml on shunianssy/foxar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for foxar-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 144f0ddd3faee810438cef3d1adec50385b11fc7ab0dd5ec92811a43145ef43e
MD5 a35cae51e7414bc8d5d297089c58522d
BLAKE2b-256 450cf57d58fb3f038a10e97626c176a64eba3c90ffdb8a41969577907e0b423e

See more details on using hashes here.

Provenance

The following attestation bundles were made for foxar-0.0.0-py3-none-any.whl:

Publisher: publish.yml on shunianssy/foxar

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page