Skip to main content

Flask + marshmallow + OpenAPI

Project description

Overview

PyPI Status license python_versions

Provides OpenAPI documentation generated from code for Flask APIs built around marshmallow schemas.

This hackish and organically grown (TM) package was crated because no other similar projects worked exactly the way I wanted them.

You will probably be better served by some other, properly maintained project with similar purpose:

If you still want to use it, welcome aboard :-) and read on!

Installation

pip install flask-marshmallow-openapi

Example

See example application. Following is incomplete excerpt to demonstrate:

import flask
import marshmallow as ma
from flask_marshmallow_openapi import OpenAPI, OpenAPISettings, open_api


class SchemaOpts(ma.SchemaOpts):
    def __init__(self, meta, *args, **kwargs):
        self.tags = getattr(meta, "tags", [])
        self.url_id_field = getattr(meta, "url_id_field", None)
        super().__init__(meta, *args, **kwargs)


class BookSchema(ma.Schema):
    OPTIONS_CLASS = SchemaOpts

    class Meta:
        url_id_field = "id"
        tags = ["Books"]
        description = "Schema for Book model"

    id = ma.fields.Integer(as_string=True)
    title = ma.fields.String(
        allow_none=False, metadata={"description": "book.title description"}
    )
    publisher = ma.fields.String(allow_none=False)
    isbn = ma.fields.String(allow_none=False)


app = flask.Flask(__name__)


@app.route("/books", methods=["GET"])
@open_api.get(BookSchema, "bookList", many=True)
def books_list():
    return "<p>Hello, World!</p>"


@app.route("/books/<int:book_id>", methods=["GET"])
@open_api.get(BookSchema, "bookDetail", many=False)
def books_detail(book_id):
    """
    description: |
        Look I can Markdown!

        | foo | bar | baz |
        | --- | --- | --- |
        | 1   | 2   | 3   |
        | 4   | 5   | 6   |
    """
    return "<p>Hello, World!</p>"


conf = OpenAPISettings(
    api_version="v1",
    api_name="Foobar API",
    app_package_name="foobar_api",
    mounted_at="/v1",
)


docs = OpenAPI(config=conf)
docs.init_app(app)

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

flask-marshmallow-openapi-0.1.2.tar.gz (2.6 MB view details)

Uploaded Source

Built Distribution

File details

Details for the file flask-marshmallow-openapi-0.1.2.tar.gz.

File metadata

File hashes

Hashes for flask-marshmallow-openapi-0.1.2.tar.gz
Algorithm Hash digest
SHA256 4afdc20efeb28887b6b0070ecf1aeb6c83dece5c8996133bd105d020d60e946e
MD5 b6fff70176f5bec51d59ddf3d91eb712
BLAKE2b-256 3350d5fa22f77206e295e0fa42c18cf9a500f437bd1d07fbcb23465de311dccb

See more details on using hashes here.

File details

Details for the file flask_marshmallow_openapi-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_marshmallow_openapi-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8c2167134421bd4757fa230f1c0d3e75ae5c880a2aa1855a386b80280e2b2646
MD5 9de03cfb1231a01dcf3f852ccd4f166d
BLAKE2b-256 0f84a14be185c7c0c94529f75dd7a2b55f0b25fd0b519c1eb155789ac6eb4b45

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