Skip to main content

Flask wrapper for programmatically composing openapi specifications

Project description

flaskdoc

PyPi downloads Python versions build Documentation Status license Coverage Code Quality

FlaskDoc allows developers to programmatically compose openapi specifications for flask endpoints as a part of code without needing to write a separate yaml file, and it comes with SwaggerUI embedded. Its main focus is on documentation which frees developers to focus on getting their services coded.

Why flaskdoc

  • Focus only on documentation and not introduce some fancy new way of using flask.

  • Easily add to existing code without needing to refactor of change the way the code has been written

  • Little or no learning curve, as long as a developer is comforatble using flask developers, they can use flaskdoc. to learn quickly and not distract So developers focus on writing code

  • SwaggerUI integration for quickly testing and iterating through versions

  • Automatic data model to JSON Schema transformation that allows for finer grain configuration

Getting Started

Visit documentation page for more details.

Install

from pypi

$ pip install flaskdoc

from github

$ pip install https://github.com/kulgan/flaskdoc/tarball/master

To run examples you will need to install the dev extension

$ pip install flaskdoc[dev]

Register OpenAPI

Add top level openapi objects like Info, Contact, License etc

import flask
from flaskdoc import register_openapi, swagger

app = flask.Flask()
# initialize app, add all the blueprints you care about

# Create top level OpenAPI objects
# the info object
info = swagger.Info(
    title="Test",
    version="1.2.2",
    contact=swagger.Contact(
        name="Rowland", email="r.ogwara@gmail.com", url="https://github.com/kulgan"
    ),
    license=swagger.License(name="Apache 2.0", url="https://www.example.com/license"),
)

# servers names and variables if necessary
servers = [swagger.Server(url="http://localhost:15172")]

# top level tags
tags = [
    swagger.Tag(name="admin", description="Secured Admin-Only calls"),
    swagger.Tag(name="developers", description="Operations available to regular developers"),
]

security_schemes = {
    "api_key": swagger.ApiKeySecurityScheme(name="api_key"),
}

# register spec
register_openapi(app, info=info, servers=servers, tags=tags, security=security_schemes)

This adds the following endpoints to your list

  • /docs

  • /docs/openapi.yaml

  • /docs/openapi.json

Start Documenting

Now start documenting you flask routes

A simple post example

blp = flask.Blueprint("Dummy", __name__, url_prefix="/v1")
@swagger.POST(
    tags=["administrator"],
    description="Posts an Echo",
    responses={"201": swagger.ResponseObject(description="OK")},
)
@blp.route("/echo", methods=["POST"])
def post():
    req = flask.request.get_json(force=True)
    return flask.jsonify(req), 200

A GET example with path parameter

blp = flask.Blueprint("Dummy", __name__, url_prefix="/v1")

@swagger.GET(
    tags=["getEcho"],
    operation_id="getEcho",
    parameters=[swagger.PathParameter(name="sample", schema=str)],
    description="Retrieve echos wit Get",
    responses={
        "200": swagger.ResponseObject(
            description="Success", content=jo.PlainText(schema=jo.Email()),
        )
    },
)
@blp.route("/echo/<string:sample>", methods=["GET"])
def echo(sample: str):
    """
    Sample GET request
    Returns: Echos back whatever was sent

    """
    return sample

Run your app and visit /docs to see the generated openapi specs

Running Examples

Two example projects are currently provided

To run

$ pip install flaskdoc[dev]
$ flaskdoc start -n petstore

Contributing

Don’t hesitate to create a Github issue for any bugs or suggestions

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flaskdoc-0.2.0.tar.gz (5.2 MB view details)

Uploaded Source

Built Distribution

flaskdoc-0.2.0-py2.py3-none-any.whl (5.2 MB view details)

Uploaded Python 2 Python 3

File details

Details for the file flaskdoc-0.2.0.tar.gz.

File metadata

  • Download URL: flaskdoc-0.2.0.tar.gz
  • Upload date:
  • Size: 5.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for flaskdoc-0.2.0.tar.gz
Algorithm Hash digest
SHA256 bb783b2c500157577083dc3399c0f9b57c1ed8c91d56f192bd229c0ee77153ad
MD5 d825df8192b9a6734b8348a7a0411f0d
BLAKE2b-256 7867c1a7bfaa79da037c8e1b3183aebf22062817f6962ef0da504dc89dea2ec6

See more details on using hashes here.

File details

Details for the file flaskdoc-0.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: flaskdoc-0.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.2 MB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for flaskdoc-0.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b186272876a3358cedb457fff3e8b74f07f0da55e9445f1803594fca4a1c013e
MD5 0d875c1e31fc37793c9ad0fd483611b4
BLAKE2b-256 c2f6112651c91435458b787629727ccd669eebc2baf2d435af2705d3e511fb91

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page