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.1.tar.gz (2.6 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flask_marshmallow_openapi-0.1.1-py3-none-any.whl (2.6 MB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for flask-marshmallow-openapi-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ff67a55897d17df6e58e413190a0521c5a02123a870200ca04bd5f2e1d63c214
MD5 1307491f5a53d5be6f4eeefd9881850e
BLAKE2b-256 d1d47702d7d1bf1782fb9d6e72f541444ddcceb35c4606492bf6bae2947127df

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flask_marshmallow_openapi-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c1e45f0f8c65dc047378b09e2d3f0e7dd2092e0c0f01a6f54e5747ccfb11de3e
MD5 c83524dce66dc75b190bc67b9721beb9
BLAKE2b-256 87e79691482f2350a54a7b25979ea30d2943ae1fdf4a6d4b536f204770afb320

See more details on using hashes here.

Supported by

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