Flask + marshmallow + OpenAPI
Project description
Overview
Provides OpenAPI documentation generated from code for Flask APIs built around marshmallow schemas.
This hackish and organically grown (TM) package was created 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)
Serving docs via ngnix
Add collect-static
command to your app:
import shutil
import click
import flask
@app.cli.command("collect_static")
@click.argument(
"destination_dir",
nargs=1,
type=click.Path(file_okay=False, dir_okay=True, writable=True, resolve_path=True),
required=True,
)
def collect_static_command(destination_dir):
shutil.copytree(
flask.current_app.static_folder, destination_dir, dirs_exist_ok=True
)
docs.collect_static(destination_dir)
click.echo(f"Static files collected into {destination_dir}.")
Configure nginx
:
server {
# ...
location ^~ /v1/static {
alias /home/user/static;
try_files $uri $uri.html =404;
}
location ^~ /v1/docs {
alias /home/user/static/docs;
try_files $uri $uri.html =404;
}
# ...
}
Whenever deploying app, call:
flask --app foobar_api collect-static /home/user/static
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flask-marshmallow-openapi-0.1.4.tar.gz
.
File metadata
- Download URL: flask-marshmallow-openapi-0.1.4.tar.gz
- Upload date:
- Size: 2.6 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | af1e3b076d367e7d347bd99fe0b21b8c86861a6e7b12b4337e64135e77caa112 |
|
MD5 | f5f318d31fc087ab53126b4276032019 |
|
BLAKE2b-256 | b1265c517b14c2fefdeeaf98a8b10637cdd4618c457a61133be52e950f143d0b |
File details
Details for the file flask_marshmallow_openapi-0.1.4-py3-none-any.whl
.
File metadata
- Download URL: flask_marshmallow_openapi-0.1.4-py3-none-any.whl
- Upload date:
- Size: 2.6 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb3331ee5252a827a9ab79273151456d741d0bf0c348b3876c7e2fe444d9eff7 |
|
MD5 | afa6e7240c83dc32aac6de732756ee5b |
|
BLAKE2b-256 | ad3202c62754128159c12dcc4cf5fd62bac9c5db5f2244440fee6ef04d047955 |