# Flasgger
## Easy Swagger UI for your Flask API
[![Build Status](https://travis-ci.com/flasgger/flasgger.svg?branch=master)](https://travis-ci.com/flasgger/flasgger)
[![Code Health](https://landscape.io/github/rochacbruno/flasgger/master/landscape.svg?style=flat)](https://landscape.io/github/rochacbruno/flasgger/master)
[![Coverage Status](https://coveralls.io/repos/github/rochacbruno/flasgger/badge.svg?branch=master)](https://coveralls.io/github/rochacbruno/flasgger?branch=master)
[![PyPI](https://img.shields.io/pypi/v/flasgger.svg)](https://pypi.python.org/pypi/flasgger)
<a target="_blank" href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=rochacbruno%40gmail%2ecom&lc=BR&item_name=Flasgger&no_note=0&currency_code=USD&bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHostedGuest"><img alt='Donate with Paypal' src='http://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif' /></a>
![flasgger](docs/flasgger.png)
Flasgger is a Flask extension to **extract [OpenAPI-Specification](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#operation-object)** from all Flask views registered in your API.
Flasgger also comes with **[SwaggerUI](http://swagger.io/swagger-ui/) embedded** so you can access [http://localhost:5000/apidocs](localhost:5000/apidocs) and visualize and interact with your API resources.
Flasgger also **provides validation** of the incoming data, using the same specification it can validates if the data received as as a POST, PUT, PATCH is valid against the schema defined using **YAML**, **Python dictionaries** or **Marshmallow Schemas**.
Flasgger can work with simple function views or MethodViews using docstring as specification, or using `@swag_from` decorator to get specification from **YAML** or **dict** and also provides **SwaggerView** which can use **Marshmallow Schemas** as specification.
Flasgger is compatible with `Flask-RESTful` so you can use `Resources` and `swag` specifications together, take a look at [restful example.](examples/restful.py)
Flasgger also supports `Marshmallow APISpec` as base template for specification, if you are using APISPec from Marshmallow take a look at [apispec example.](examples/apispec_example.py)
Table of Contents
=================
* [Top Contributors](#top-contributors)
* [Examples and demo app](#examples-and-demo-app)
* [Docker](#docker)
* [Installation](#installation)
* [Getting started](#getting-started)
* [Using docstrings as specification](#using-docstrings-as-specification)
* [Using external YAML files](#using-external-yaml-files)
* [Using dictionaries as raw specs](#using-dictionaries-as-raw-specs)
* [Using Marshmallow Schemas](#using-marshmallow-schemas)
* [Using <strong>Flask RESTful</strong> Resources](#using-flask-restful-resources)
* [Auto-parsing external YAML docs and MethodViews](#auto-parsing-external-yaml-docs-and-methodviews)
* [Handling multiple http methods and routes for a single function](#handling-multiple-http-methods-and-routes-for-a-single-function)
* [Use the same data to validate your API POST body.](#use-the-same-data-to-validate-your-api-post-body)
* [Custom validation](#custom-validation)
* [Validation Error handling](#validation-error-handling)
* [Get defined schemas as python dictionaries](#get-defined-schemas-as-python-dictionaries)
* [HTML sanitizer](#html-sanitizer)
* [Swagger UI and templates](#swagger-ui-and-templates)
* [OpenAPI 3.0 Support](#openapi-30-support)
* [Externally loading Swagger UI and jQuery JS/CSS](#externally-loading-swagger-ui-and-jquery-jscss)
* [Initializing Flasgger with default data.](#initializing-flasgger-with-default-data)
* [Getting default data at runtime](#getting-default-data-at-runtime)
* [Behind a reverse proxy](#behind-a-reverse-proxy)
* [Customize default configurations](#customize-default-configurations)
* [Extracting Definitions](#extracting-definitions)
* [Python2 Compatibility](#python2-compatibility)
Created by [gh-md-toc](https://github.com/ekalinin/github-markdown-toc)
# Top Contributors
[![](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/images/0)](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/links/0)[![](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/images/1)](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/links/1)[![](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/images/2)](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/links/2)[![](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/images/3)](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/links/3)[![](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/images/4)](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/links/4)[![](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/images/5)](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/links/5)[![](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/images/6)](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/links/6)[![](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/images/7)](https://sourcerer.io/fame/rochacbruno/rochacbruno/flasgger/links/7)
# Examples and demo app
There are some [example applications](examples/) and you can also play with examples in [Flasgger demo app](http://flasgger.pythonanywhere.com/)
> NOTE: all the examples apps are also test cases and run automatically in Travis CI to ensure quality and coverage.
## Docker
The examples and demo app can also be built and run as a Docker image/container:
```
docker build -t flasgger .
docker run -it --rm -p 5000:5000 --name flasgger flasgger
```
Then access the Flasgger demo app at http://localhost:5000 .
# Installation
> under your virtualenv do:
Ensure you have latest setuptools
```
pip install -U setuptools
```
then
```
pip install flasgger
```
or (dev version)
```
pip install https://github.com/rochacbruno/flasgger/tarball/master
```
> NOTE: If you want to use **Marshmallow Schemas** you also need to run `pip install marshmallow apispec`
# Getting started
## Using docstrings as specification
Create a file called for example `colors.py`
```python
from flask import Flask, jsonify
from flasgger import Swagger
app = Flask(__name__)
swagger = Swagger(app)
@app.route('/colors/<palette>/')
def colors(palette):
"""Example endpoint returning a list of colors by palette
This is using docstrings for specifications.
---
parameters:
- name: palette
in: path
type: string
enum: ['all', 'rgb', 'cmyk']
required: true
default: all
definitions:
Palette:
type: object
properties:
palette_name:
type: array
items:
$ref: '#/definitions/Color'
Color:
type: string
responses:
200:
description: A list of colors (may be filtered by palette)
schema:
$ref: '#/definitions/Palette'
examples:
rgb: ['red', 'green', 'blue']
"""
all_colors = {
'cmyk': ['cian', 'magenta', 'yellow', 'black'],
'rgb': ['red', 'green', 'blue']
}
if palette == 'all':
result = all_colors
else:
result = {palette: all_colors.get(palette)}
return jsonify(result)
app.run(debug=True)
```
Now run:
```
python colors.py
```
And go to: [http://localhost:5000/apidocs/](http://localhost:5000/apidocs/)
You should get:
![colors](docs/colors.png)
## Using external YAML files
Save a new file `colors.yml`
```yaml
Example endpoint returning a list of colors by palette
In this example the specification is taken from external YAML file
---
parameters:
- name: palette
in: path
type: string
enum: ['all', 'rgb', 'cmyk']
required: true
default: all
definitions:
Palette:
type: object
properties:
palette_name:
type: array
items:
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
弗拉斯格 Flask API的Easy Swagger UI Flasgger是Flask扩展,可从您API中注册的所有Flask视图中提取。 Flasgger还内置了因此您可以访问http:// localhost:5000 / apidocs并可视化API资源并与之交互。 Flasgger还提供所述输入数据的验证,使用它可以在相同的规格只会验证如果收到作为POST,PUT的数据,PATCH是针对使用YAML,Python字典或棉花糖架构所定义的模式是有效的。 Flasgger可以使用简单的函数视图或方法视图(使用docstring作为规范),或使用@swag_from装饰器从YAML或dict获取规范,还提供可以使用棉花糖模式作为规范的SwaggerView 。 Flasgger与Flask-RESTful兼容,因此您可以同时使用Resources和swag规范,并查看一个 Flasgger还支持Marshmallow APISpec作为规范的基础模板,如果您使用棉花糖的APISPec,请查看 目录 由创建 杰出贡献者 示例和演示应用 这里有一些,您也可以在使用示例 注
资源详情
资源评论
资源推荐
收起资源包目录
flasgger:Flask API的简单OpenAPI规范和Swagger UI (197个子文件)
setup.cfg 27B
.coveragerc 261B
swagger-ui.css 139KB
screen.css 43KB
print.css 41KB
style.css 3KB
reset.css 773B
typography.css 0B
Dockerfile 350B
.dockerignore 16B
droid-sans-v6-latin-700.eot 22KB
droid-sans-v6-latin-regular.eot 21KB
throbber.gif 9KB
expand.gif 73B
collapse.gif 69B
.gitattributes 80B
.gitignore 731B
HISTORY 8KB
index.html 7KB
index_old.html 6KB
flasgger.html 5KB
index.html 4KB
oauth2-redirect.html 2KB
swagger.html 2KB
index.html 1KB
head.html 659B
o2c.html 479B
o2c.html 479B
footer.html 384B
body_scripts.html 176B
top.html 168B
custom_head.html 108B
index.html 97B
favicon.ico 5KB
MANIFEST.in 1023B
MANIFEST.in 37B
tox.ini 850B
swagger-ui.js 2.58MB
swagger-ui-bundle.js 979KB
swagger-ui.min.js 443KB
swagger-ui.js 390KB
swagger-ui-standalone-preset.js 303KB
sanitize-html.min.js 127KB
jsoneditor.min.js 127KB
jquery-1.8.0.min.js 90KB
jquery.min.js 84KB
handlebars-4.0.5.js 70KB
lodash.min.js 51KB
js-yaml.min.js 42KB
es5-shim.js 22KB
backbone-min.js 19KB
marked.js 15KB
highlight.9.1.0.pack.js 11KB
swagger-oauth.js 7KB
geo.js 4KB
jquery.ba-bbq.min.js 3KB
el.js 3KB
ru.js 3KB
ja.js 3KB
fr.js 2KB
es.js 2KB
it.js 2KB
ca.js 2KB
en.js 2KB
ko-kr.js 2KB
pt.js 2KB
pl.js 2KB
tr.js 2KB
zh-cn.js 2KB
translator.js 1KB
jquery.wiggle.min.js 536B
jquery.slideto.min.js 365B
object-assign-pollyfill.js 349B
highlight.9.1.0.pack_extended.js 310B
colors_template.json 2KB
LICENSE 1KB
Makefile 783B
swagger-ui-bundle.js.map 4.08MB
swagger-ui-standalone-preset.js.map 1.32MB
swagger-ui.js.map 1.26MB
swagger-ui.css.map 274KB
jquery.min.map 127KB
README.md 27KB
README.zh.md 23KB
CODE_OF_CONDUCT.md 3KB
README.md 1KB
README.md 213B
README.md 1B
colors.png 149KB
flasgger_example.png 124KB
flasgger.png 39KB
explorer_icons.png 5KB
favicon-32x32.png 1KB
wordnik_api.png 670B
favicon-16x16.png 665B
pet_store_api.png 631B
favicon-32x32.png 628B
logo_small.png 455B
favicon-16x16.png 445B
utils.py 33KB
共 197 条
- 1
- 2
13338383381
- 粉丝: 15
- 资源: 4648
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1