pip install flask_restful
# 1. 导包
from flask_restful import Api, Resource
# 2. 创建API对象并接管app
api = Api(app)
# 3. 定义类视图
class IndexResource(Resource):
def get(self):
return ‘index‘
# 4. 使用api对象添加路由
api.add_resource(IndexResource, ‘/‘)
from flask import Flask
# 1. 导包
from flask_restful import Api, Resource
app = Flask(__name__)
# 2. 创建API对象并接管app
api = Api(app)
# 3. 定义类视图
class IndexResource(Resource):
def get(self):
return ‘index‘
# 4. 使用api对象添加路由
api.add_resource(IndexResource, ‘/‘)
if __name__ == ‘__main__‘:
app.run()
# 1. 导包 from flask_restful import Api, Resource from flask.blueprints import Blueprint
# 2. 创建蓝图对象,并使用API对象接管蓝图 bule_prin = Blueprint(‘flask_blueprint‘, __name__) api = Api(bule_prin)
# 3. 定义试图,并使用API对象添加路由 class IndexResource(Resource): def get(self): return ‘index‘ api.add_resource(IndexResource, ‘/‘)
# 4.将蓝图注册到app中 app.register_blueprint(bule_prin)
from flask import Flask # 1. 导包 from flask_restful import Api, Resource from flask.blueprints import Blueprint app = Flask(__name__) # 2. 创建蓝图对象,并使用API对象接管蓝图 bule_prin = Blueprint(‘flask_blueprint‘, __name__) api = Api(bule_prin) # 3. 定义试图,并使用API对象添加路由 class IndexResource(Resource): def get(self): return ‘index‘ api.add_resource(IndexResource, ‘/‘) # 4.将蓝图注册到app中 app.register_blueprint(bule_prin) if __name__ == ‘__main__‘: app.run()
from flask_restful import Api, Resource
api = Api(app)
# 2. 定义装饰器 def outter(func): def inner(*args, **kwargs): ret = func(*args, **kwargs) return ‘{} decorators....‘.format(ret) return inner
# 1. 定义类视图,并设置路由 class IndexResource(Resource): # 为所有请求方法都添加装饰器 # method_decorators = [outter] # 为指定方法添加装饰器 method_decorators = { ‘get‘: [outter] } def get(self): return ‘get ...‘ def post(self): return ‘post ...‘ api.add_resource(IndexResource, ‘/‘)
# 为所有请求方法都添加装饰器 method_decorators = [outter]
# 为指定方法添加装饰器 method_decorators = { ‘get‘: [outter] }
from flask import Flask from flask_restful import Api, Resource app = Flask(__name__) api = Api(app) # 2. 定义装饰器 def outter(func): def inner(*args, **kwargs): ret = func(*args, **kwargs) return ‘{} decorators....‘.format(ret) return inner # 1. 定义类视图,并设置路由 class IndexResource(Resource): # 为所有请求方法都添加装饰器 # method_decorators = [outter] # 为指定方法添加装饰器 method_decorators = { ‘get‘: [outter] } def get(self): return ‘get ...‘ def post(self): return ‘post ...‘ api.add_resource(IndexResource, ‘/‘) if __name__ == ‘__main__‘: app.run()
原文:https://www.cnblogs.com/chao666/p/12403229.html