当前位置: 首页 > 软件库 > Web应用开发 > Web框架 >

flask-restplus

授权协议 View license
开发语言 Python
所属分类 Web应用开发、 Web框架
软件类型 开源软件
地区 不详
投 递 者 景轶
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Flask RestPlus

IMPORTANT NOTICE:

This project has been forked to Flask-RESTXand will be maintained by by the python-restxorganization. Flask-RESTPlus should be considered unmaintained.

The community has decided to fork the project due to lack of response from theoriginal author @noirbizarre. We have been discussing this eventuality foralmost a year.

Things evolved a bit since that discussion and a few of us have been grantedmaintainers access to the github project, but only the original author hasaccess rights on the PyPi project. As such, we been unable to make any actualreleases. To prevent this project from dying out, we have forked it to continuedevelopment and to support our users.

Flask-RESTPlus is an extension for Flask that adds support for quickly building REST APIs.Flask-RESTPlus encourages best practices with minimal setup.If you are familiar with Flask, Flask-RESTPlus should be easy to pick up.It provides a coherent collection of decorators and tools to describe your APIand expose its documentation properly using Swagger.

Compatibility

Flask-RestPlus requires Python 2.7 or 3.4+.

Installation

You can install Flask-Restplus with pip:

$ pip install flask-restplus

or with easy_install:

$ easy_install flask-restplus

Quick start

With Flask-Restplus, you only import the api instance to route and document your endpoints.

from flask import Flask
from flask_restplus import Api, Resource, fields

app = Flask(__name__)
api = Api(app, version='1.0', title='TodoMVC API',
    description='A simple TodoMVC API',
)

ns = api.namespace('todos', description='TODO operations')

todo = api.model('Todo', {
    'id': fields.Integer(readOnly=True, description='The task unique identifier'),
    'task': fields.String(required=True, description='The task details')
})


class TodoDAO(object):
    def __init__(self):
        self.counter = 0
        self.todos = []

    def get(self, id):
        for todo in self.todos:
            if todo['id'] == id:
                return todo
        api.abort(404, "Todo {} doesn't exist".format(id))

    def create(self, data):
        todo = data
        todo['id'] = self.counter = self.counter + 1
        self.todos.append(todo)
        return todo

    def update(self, id, data):
        todo = self.get(id)
        todo.update(data)
        return todo

    def delete(self, id):
        todo = self.get(id)
        self.todos.remove(todo)


DAO = TodoDAO()
DAO.create({'task': 'Build an API'})
DAO.create({'task': '?????'})
DAO.create({'task': 'profit!'})


@ns.route('/')
class TodoList(Resource):
    '''Shows a list of all todos, and lets you POST to add new tasks'''
    @ns.doc('list_todos')
    @ns.marshal_list_with(todo)
    def get(self):
        '''List all tasks'''
        return DAO.todos

    @ns.doc('create_todo')
    @ns.expect(todo)
    @ns.marshal_with(todo, code=201)
    def post(self):
        '''Create a new task'''
        return DAO.create(api.payload), 201


@ns.route('/<int:id>')
@ns.response(404, 'Todo not found')
@ns.param('id', 'The task identifier')
class Todo(Resource):
    '''Show a single todo item and lets you delete them'''
    @ns.doc('get_todo')
    @ns.marshal_with(todo)
    def get(self, id):
        '''Fetch a given resource'''
        return DAO.get(id)

    @ns.doc('delete_todo')
    @ns.response(204, 'Todo deleted')
    def delete(self, id):
        '''Delete a task given its identifier'''
        DAO.delete(id)
        return '', 204

    @ns.expect(todo)
    @ns.marshal_with(todo)
    def put(self, id):
        '''Update a task given its identifier'''
        return DAO.update(id, api.payload)


if __name__ == '__main__':
    app.run(debug=True)

Contributors

Flask-RESTPlus is brought to you by @noirbizarre. Since early 2019 @SteadBytes,@a-luna, @j5awry, @ziirish volunteered to help @noirbizarre keep the project upand running.Of course everyone is welcome to contribute and we will be happy to review yourPR's or answer to your issues.

Documentation

The documentation is hosted on Read the Docs

Contribution

Want to contribute! That's awesome! Check out CONTRIBUTING.rst!

  • 背景 在使用flask+flask-restplus时,业务正常时接口函数返回一个可以json序列化的对象 @ns.route('/hello') class Hello(Resource): def get(self): return ['hello', 'hi'] 接口返回内容如下: [ "hello", "hi" ] 当业务异常(比如检测到参数错误

  • 1. 说明  先来看一个应用场景:  我写了一段功能性的程序(可能是Java的,也可能是Python的),供他人调用(调我程序可能是其它编程语言,或者直接运行,如果调用者对我使用的工具不熟悉,直接调用可能很麻烦),这个程序需要传入多个参数,需要结构化的输出,我以什么方式提供给比较好呢?  我们可能会选择BS的结构,建立一个Web-Server,然后把功能性的程序放在Web-Server上并向外暴露

  • 使用FLASK-RESTPLUS的RequestParser模块时返回400 BAD REQUEST错误Input payload validation failed : object is not callable { "errors": { "number": "number 'str' object is not callable" }, "message": "Inpu

  • 问题: 书写 仪表盘的图表配置信息保存 操作的接口时,想通过querystring 的方式来获取id值,因此定义 的接口 路由是 /api/dashboard/chart/handle/?id=int:id 这样的 路由地址,其中 int:id 这里是必需要传递的一个value 值,通过这样的方式来获取到id值,但在使用过程中,请求该接口,一直返回404. 解决: 使用flask-restplus

  • https://www.cnblogs.com/leejack/tag/Flask-RESTPlus/

 相关资料
  • 问题内容: 在官方的快速入门中,建议在使用单个 模块 时使用: 2. …如果您使用的是单个模块(如本例所示),则应使用,因为取决于它是作为应用程序启动还是作为模块导入,其名称将有所不同(与实际导入名称不同)。… 但是,在他们的API文档中,当我的应用程序为 软件包 时,建议进行硬编码: 因此,您在此处提供的内容很重要。如果使用单个模块,则始终为正确的值。但是,如果您使用的是包,通常建议在其中硬编码

  • 在前面,我们介绍了 REST Web 服务,并使用 Flask 提供服务。这里,我们使用第三方库 Flask-RESTful,它使得在 Flask 中提供 REST 服务变得更加简单。 安装 使用 pip 安装: $ pip install flask-restful 使用 下面我们主要使用官方文档的例子进行说明。 Hello World 我们先来看一个简单的例子。 # -*- coding: u

  • Bootstrap 是 Twitter 开源的一个 CSS/HTML 框架,它让 Web 开发变得更加迅速,简单。要想在我们的 Flask 应用中使用 Boostrap,有两种方案可供选择: 第 1 种,在我们的 Jinja 模板中直接引入 Bootstrap 层叠样式表 (CSS) 和 JavaScript 文件,比如 bootstrap.min.css,bootstrap.min.js; 第

  • 在 Web 应用中,我们经常需要保护我们的 api,以避免非法访问。比如,只允许登录成功的用户发表评论等。Flask-HTTPAuth 扩展可以很好地对 HTTP 的请求进行认证,不依赖于 Cookie 和 Session。本文主要介绍两种认证的方式:基于密码和基于令牌 (token)。 安装 使用 pip 安装: $ pip install Flask-HTTPAuth 基于密码的认证 为了简化

  • 假设你的 Web 服务对于某些请求比较耗时,而该请求的返回结果在较短的时间内(比如 5 分钟内)都是足够有效的,这时你能想到什么方法去改善这种状况呢?缓存?对,至少这是一种提高性能的最简单的方法。 Flask 本身不提供缓存功能,但是作为 Flask 核心的 Werkzeug 框架则提供了一个简单的缓存对象 SimpleCache,它将缓存项存放在 Python 解释器的内存中。使用 Simple

  • MongoDB 是一个文档型数据库,是 NoSQL (not only SQL) 的一种,具有灵活、易扩展等诸多优点,受到许多开发者的青睐。MongoEngine 是一个用来操作 MongoDB 的 ORM 框架,如果你不知道什么是 ORM,可以参考 Flask-SQLAlchemy 一节。在 Flask 中,我们可以直接使用 MongoEngine,也可使用 Flask-MongoEngine