Skip to main content

a extension for flask which can auto register route rule

Project description

flask-controller

PyPI PyPI - Downloads PyPI - Python Version PyPI - License

简介

flask-controller 简化Flask开发,自动路由注册,以类的形式管理控制器

支持多层扫描

安装

pip install flask-controller

使用示例

项目结构

.
└── flask_app
    ├── controllers
    │   ├── __init__.py
    │   ├── admin
    │   │   ├── __init__.py
    │   │   └── login_controller.py
    │   ├── index_controller.py
    │   └── user_controller.py
    └── main.py

文件内容

flask_app/controllers/index_controller.py

# -*- coding: utf-8 -*-
from flask_controller import FlaskController


class IndexController(FlaskController):

    def index(self):
        return 'index'

flask_app/controllers/user_controller.py

# -*- coding: utf-8 -*-
from flask_controller import FlaskController


class UserController(FlaskController):

    def user_name(self, name, age: int = 23):
        return 'username'

flask_app/controllers/admin/login_controller.py

# -*- coding: utf-8 -*-
from flask_controller import FlaskController


class LoginController(FlaskController):
    def index(self):
        return 'Login index'

flask_app/main.py

# -*- coding: utf-8 -*-


from flask import Flask

from flask_controller import FlaskControllerRegister

app = Flask(__name__, static_folder=None)


# 自动注册路由
register = FlaskControllerRegister(app)

register.register_package('flask_app.controllers')

注册结果

Map([
 <Rule '/admin/login/index' (OPTIONS, GET, HEAD, POST) -> admin.login.index>,
 <Rule '/index/index' (OPTIONS, GET, HEAD, POST) -> index.index>,
 <Rule '/user/user_name/<name>/<age>' (OPTIONS, GET, HEAD, POST) -> user.user_name>
])

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

flask-controller-0.0.2.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distribution

flask_controller-0.0.2-py3-none-any.whl (5.9 kB view hashes)

Uploaded Python 3

Supported by

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