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 details)

Uploaded Source

Built Distribution

flask_auto_routers-0.1.2-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file flask-auto-routers-0.1.2.tar.gz.

File metadata

  • Download URL: flask-auto-routers-0.1.2.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for flask-auto-routers-0.1.2.tar.gz
Algorithm Hash digest
SHA256 95fa9be33851cd7f67402ffedb635e0ad9bb5d14afd2004adb873c7e3ee5bd33
MD5 9df4372e02bb3a716901df935b84d375
BLAKE2b-256 7147c855df24e1a27f51c56fbfa5087057517c01a609f202a1d3a216b8437e4d

See more details on using hashes here.

File details

Details for the file flask_auto_routers-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_auto_routers-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e0df8ea73b3f65a8edeceb3a5ee1e96fb00b71ee9b0d31f00014289bb18e1f01
MD5 aa74d061d4e5a9824d1506082b3cab73
BLAKE2b-256 07e09161f42f15275d844737130f09f1e62939dfcf70d3901b2bf541832e4339

See more details on using hashes here.

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