Skip to main content

flask restful auto router

Project description

Flask Auto Router(自动注册路由)


This is an auto-register routing plugin based on the 'Flask' framework, developed according to the 'flask_restful' specification, you only need to configure the view file, you can automatically register the routing system, if you encounter Debug or problems, please contact me through the above contact information

这是一个基于 Flask框架的自动注册路由插件,根据flask_restful 规范开发的,你只需要配置视图文件,就可以自动注册路由系统,如果你在使用中遇到Debug或者问题,请通过上面的联系方式联系我

Required plugins 必要的插件

pip install -U flask
pip install -U flask-restful

settings(配置)

BASE_DIR: Sets the root path for the search

设置搜索的根路径

from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent

AUTO_ROUTER_DIR: A file filter that allows AutoRouter to search only a specified range of directories

文件过滤器,可以使AutoRouter 只搜索指定范围的目录

AUTO_ROUTER_DIR = ['view', 'view2'] 

Simple configuration is all that's needed, and you never have to configure a cumbersome routing system again

只需要简单的配置,从此再也不用配置繁琐的路由系统 ^.^

from flask import Flask
from flask_auto_router import AutoRouter


# 1.flask restful view object Auto register router plugins
# 1.flask restful 自动注册视图类路由插件
auto_router = AutoRouter()

# 2. File filter, which allows searching only a specified range of file directories
#  Add this configuration to app.config
# 2.文件过滤器,只允许搜索指定范围的文件目录
#  将此配置加入到 app.config中即可
# Configuration is only allowed to specify the directory view class
# 只允许配置指定目录视图类
#   AUTO_ROUTER_DIR = ['view', 'view2'] 

# 3. 配置需要自动路由的.py文件(注意带有相对路径)
auto_router.add_routers("home","/","view.home")
auto_router.add_routers("user","/user","view.user")

app = Flask(__name__)
auto_router.init_app(app)

if __name__ in "__main__":
    app.run()

View Object(视图类)

__url__: Custom Router 自定义子路由

__endpoint__: Jinja2 template reflection names Jinja2 模板反射名称

view.home

from flask_restful import Resource

class Home(Resource):
    __url__ = '/'
    __endpoint__ = 'home'

    def get(self, **kwargs):
        return "Hello World!"

访问地址: http://localhost:5000

view.user

from flask_restful import Resource

class User(Resource):
    __url__ = '/'
    __endpoint__ = 'user'

    def get(self, **kwargs):
        return "Hello User!"
    
# If the route method is not stated, the class name will be used as the route  
# 如果没有申明路由方式,将会以类名作为路由
class Login(Resource):

    def get(self, **kwargs):
        return "Login in!"

访问地址:

user: http://localhost:5000/user

user: http://localhost:5000/login

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-auto-routers-0.1.2.tar.gz (4.1 kB view hashes)

Uploaded Source

Built Distribution

flask_auto_routers-0.1.2-py3-none-any.whl (3.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