Skip to main content

Flask + marshmallow for beautiful APIs

Project description

Latest version Build status Documentation marshmallow 3|4 compatible

Flask + marshmallow for beautiful APIs

Flask-Marshmallow is a thin integration layer for Flask (a Python web framework) and marshmallow (an object serialization/deserialization library) that adds additional features to marshmallow, including URL and Hyperlinks fields for HATEOAS-ready APIs. It also (optionally) integrates with Flask-SQLAlchemy.

Get it now

pip install flask-marshmallow

Create your app.

from flask import Flask
from flask_marshmallow import Marshmallow

app = Flask(__name__)
ma = Marshmallow(app)

Write your models.

from your_orm import Model, Column, Integer, String, DateTime


class User(Model):
    email = Column(String)
    password = Column(String)
    date_created = Column(DateTime, auto_now_add=True)

Define your output format with marshmallow.

class UserSchema(ma.Schema):
    email = ma.Email()
    date_created = ma.DateTime()

    # Smart hyperlinking
    _links = ma.Hyperlinks(
        {
            "self": ma.URLFor("user_detail", values=dict(id="<id>")),
            "collection": ma.URLFor("users"),
        }
    )


user_schema = UserSchema()
users_schema = UserSchema(many=True)

Output the data in your views.

@app.route("/api/users/")
def users():
    all_users = User.all()
    return users_schema.dump(all_users)


@app.route("/api/users/<id>")
def user_detail(id):
    user = User.get(id)
    return user_schema.dump(user)


# {
#     "email": "fred@queen.com",
#     "date_created": "Fri, 25 Apr 2014 06:02:56 -0000",
#     "_links": {
#         "self": "/api/users/42",
#         "collection": "/api/users/"
#     }
# }

http://flask-marshmallow.readthedocs.io/

Learn More

To learn more about marshmallow, check out its docs.

License

MIT licensed. See the bundled LICENSE file for more details.

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-1.5.0.tar.gz (41.2 kB view details)

Uploaded Source

Built Distribution

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

flask_marshmallow-1.5.0-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

Details for the file flask_marshmallow-1.5.0.tar.gz.

File metadata

  • Download URL: flask_marshmallow-1.5.0.tar.gz
  • Upload date:
  • Size: 41.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for flask_marshmallow-1.5.0.tar.gz
Algorithm Hash digest
SHA256 7c06b56e41647eccdb3cd57c25b109f19191b4c62509362bd64920cdf601a066
MD5 bb280ba1bd45f3b800078ac39d6f4f7f
BLAKE2b-256 65ca22f00dffdd6709332247960e0c1223995bc22ff7537834bffc44ab9ab90b

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_marshmallow-1.5.0.tar.gz:

Publisher: build-release.yml on marshmallow-code/flask-marshmallow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file flask_marshmallow-1.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_marshmallow-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 99951c77e5654111ed733811c6dc9310bfb4c3688c78a9e76f80b5ae0b2279a6
MD5 827ea13e29d2d731d1cf096b458ee0af
BLAKE2b-256 d7ef953c6395966388ab37cfb77493c441372201406a260d722e2fefdff134fb

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_marshmallow-1.5.0-py3-none-any.whl:

Publisher: build-release.yml on marshmallow-code/flask-marshmallow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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