# Flask API
Browsable web APIs for Flask.
[![Unix Build Status](https://img.shields.io/travis/flask-api/flask-api.svg)](https://travis-ci.org/flask-api/flask-api)
[![Coverage Status](https://img.shields.io/coveralls/flask-api/flask-api.svg)](https://coveralls.io/r/flask-api/flask-api)
[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/flask-api/flask-api.svg)](https://scrutinizer-ci.com/g/flask-api/flask-api/)
[![PyPI Version](https://img.shields.io/pypi/v/Flask-API.svg)](https://pypi.org/project/Flask-API/)
**Status**: This project is in maintenance mode. The original author ([Tom Christie](https://twitter.com/_tomchristie)) has shifted his focus to [API Star](https://github.com/encode/apistar). Passing PRs will still be considered for releases by the maintainer ([Jace Browning](https://twitter.com/jacebrowning)).
## Overview
Flask API is a drop-in replacement for Flask that provides an implementation of browsable APIs similar to what [Django REST framework](http://www.django-rest-framework.org) offers. It gives you properly content-negotiated responses and smart request parsing:
![Screenshot](docs/screenshot.png)
## Installation
Requirements:
* Python 2.7+ or 3.4+
* Flask 0.12.3+
Install using `pip`:
```shell
$ pip install Flask-API
```
Import and initialize your application:
```python
from flask_api import FlaskAPI
app = FlaskAPI(__name__)
```
## Responses
Return any valid response object as normal, or return a `list` or `dict`.
```python
@app.route('/example/')
def example():
return {'hello': 'world'}
```
A renderer for the response data will be selected using content negotiation based on the client 'Accept' header. If you're making the API request from a regular client, this will default to a JSON response. If you're viewing the API in a browser, it'll default to the browsable API HTML.
## Requests
Access the parsed request data using `request.data`. This will handle JSON or form data by default.
```python
@app.route('/example/')
def example():
return {'request data': request.data}
```
## Example
The following example demonstrates a simple API for creating, listing, updating and deleting notes.
```python
from flask import request, url_for
from flask_api import FlaskAPI, status, exceptions
app = FlaskAPI(__name__)
notes = {
0: 'do the shopping',
1: 'build the codez',
2: 'paint the door',
}
def note_repr(key):
return {
'url': request.host_url.rstrip('/') + url_for('notes_detail', key=key),
'text': notes[key]
}
@app.route("/", methods=['GET', 'POST'])
def notes_list():
"""
List or create notes.
"""
if request.method == 'POST':
note = str(request.data.get('text', ''))
idx = max(notes.keys()) + 1
notes[idx] = note
return note_repr(idx), status.HTTP_201_CREATED
# request.method == 'GET'
return [note_repr(idx) for idx in sorted(notes.keys())]
@app.route("/<int:key>/", methods=['GET', 'PUT', 'DELETE'])
def notes_detail(key):
"""
Retrieve, update or delete note instances.
"""
if request.method == 'PUT':
note = str(request.data.get('text', ''))
notes[key] = note
return note_repr(key)
elif request.method == 'DELETE':
notes.pop(key, None)
return '', status.HTTP_204_NO_CONTENT
# request.method == 'GET'
if key not in notes:
raise exceptions.NotFound()
return note_repr(key)
if __name__ == "__main__":
app.run(debug=True)
```
Now run the webapp:
```shell
$ python ./example.py
* Running on http://127.0.0.1:5000/
* Restarting with reloader
```
You can now open a new tab and interact with the API from the command line:
```shell
$ curl -X GET http://127.0.0.1:5000/
[{"url": "http://127.0.0.1:5000/0/", "text": "do the shopping"},
{"url": "http://127.0.0.1:5000/1/", "text": "build the codez"},
{"url": "http://127.0.0.1:5000/2/", "text": "paint the door"}]
$ curl -X GET http://127.0.0.1:5000/1/
{"url": "http://127.0.0.1:5000/1/", "text": "build the codez"}
$ curl -X PUT http://127.0.0.1:5000/1/ -d text="flask api is teh awesomez"
{"url": "http://127.0.0.1:5000/1/", "text": "flask api is teh awesomez"}
```
You can also work on the API directly in your browser, by opening <http://127.0.0.1:5000/>. You can then navigate between notes, and make `GET`, `PUT`, `POST` and `DELETE` API requests.
没有合适的资源?快使用搜索试试~ 我知道了~
Python-Flask的可浏览WebAPI一个强大的FlaskRESTful框架
共90个文件
png:31个
py:26个
md:11个
需积分: 47 15 下载量 77 浏览量
2019-08-10
04:45:59
上传
评论 1
收藏 257KB ZIP 举报
温馨提示
Flask的可浏览Web API,一个强大的Flask RESTful框架
资源推荐
资源详情
资源评论
收起资源包目录
Python-Flask的可浏览WebAPI一个强大的FlaskRESTful框架.zip (90个子文件)
flask-api-flask-api-ae70313
.scrutinizer.yml 273B
flask_api
settings.py 2KB
exceptions.py 2KB
compat.py 739B
request.py 7KB
renderers.py 4KB
status.py 2KB
app.py 5KB
mediatypes.py 4KB
decorators.py 900B
response.py 1KB
__init__.py 56B
negotiation.py 2KB
tests
test_app.py 7KB
test_mediatypes.py 5KB
test_request.py 2KB
test_negotiation.py 4KB
test_renderers.py 6KB
test_status.py 1KB
__init__.py 433B
test_parsers.py 7KB
test_settings.py 780B
test_exceptions.py 830B
templates
base.html 9KB
parsers.py 2KB
static
img
glyphicons-halflings.png 12KB
glyphicons-halflings-white.png 9KB
grid.png 1KB
js
prettify-min.js 13KB
default.js 2KB
jquery-1.8.1-min.js 91KB
bootstrap.min.js 25KB
css
default.css 1KB
bootstrap.min.css 115KB
prettify.css 817B
bootstrap-tweaks.css 3KB
Pipfile.lock 22KB
CONTRIBUTING.md 852B
.travis.yml 530B
mkdocs.yml 611B
scent.py 2KB
LICENSE.md 1KB
setup.py 2KB
README.md 4KB
Pipfile 434B
Makefile 4KB
docs
about
release-notes.md 2KB
license.md 1KB
index.md 4KB
logo.draw.io.xml 961B
img
android-chrome-36x36.png 974B
android-chrome-72x72.png 1KB
apple-touch-icon-114x114.png 1KB
manifest.json 1KB
apple-touch-icon-152x152.png 1KB
safari-pinned-tab.svg 2KB
favicon.ico 7KB
android-chrome-96x96.png 2KB
android-chrome-384x384.png 6KB
android-chrome-512x512.png 9KB
apple-touch-icon-180x180.png 1KB
apple-touch-icon-144x144.png 1KB
apple-touch-icon-76x76.png 1KB
apple-touch-icon-120x120.png 1KB
android-chrome-192x192.png 2KB
mstile-310x150.png 2KB
android-chrome-256x256.png 4KB
apple-touch-icon.png 1KB
favicon-194x194.png 2KB
favicon-32x32.png 812B
mstile-150x150.png 2KB
mstile-70x70.png 1KB
mstile-310x310.png 3KB
apple-touch-icon-57x57.png 934B
android-chrome-48x48.png 1KB
mstile-144x144.png 2KB
android-chrome-144x144.png 2KB
apple-touch-icon-60x60.png 939B
favicon-16x16.png 612B
browserconfig.xml 437B
apple-touch-icon-72x72.png 1KB
CNAME 17B
screenshot.png 58KB
api-guide
renderers.md 5KB
parsers.md 4KB
templates.md 740B
exceptions.md 4KB
status-codes.md 4KB
.gitignore 545B
example.py 1KB
共 90 条
- 1
资源评论
weixin_39841848
- 粉丝: 509
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- MQTT协议发温湿度到阿里云平台支持下发控制LED灯与继电器对接阿里云APP
- STM32F103ZET6+OV2640+TF卡存储
- 操作系统考试要点最新版本.doc
- 操作系统试题B卷.doc
- 移动机器人自主路径规划之RRT算法MATLAB实现代码
- Python使用 LSTM循环神经网络预测风力发电厂中风机产生的功率项目源码+数据集.zip
- 深入探究文件I/O-I.MX6U嵌入式Linux C应用编程学习笔记基于正点原子阿尔法开发板
- MQTT协议发温湿度电压数据到ONENET支持下发控制LED灯与继电器(新平台)
- 平抑风电波动的电-氢混合储能容量优化配置(注释完全,可直接运行)(文档加Matlab源码)
- Gigabyte.RX560.4g 1750mhz bios GAMING OC
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功