首先下载pip install flask-restful
使用步骤:
导入Rrsource ,Api
创建api对象,并关联
api = Api(app)
定义视图类
class Hello(Rrsource):
def get(self):
return {‘data’:‘hello’}
使用api对象绑定路由和视图
api.add_resouce(Hello, ‘/’)
在蓝图中使用Flask-restful扩展
可以在蓝图中使用步骤:
创建蓝图对象
user_bp = Blueprint(‘user’,name)
创建api对象并关联到蓝图对象
api = Api(user_bp)
定义视图类
class Hello(Resource):
def get(self):
return {‘data’:‘hell world’}
使用api绑定视图类和路由关系
api.add_resource(Hello, ‘/’)
把蓝图对象注册到app中
app.register_bulepint(user_bp)