- Notifications
You must be signed in to change notification settings - Fork58
Flask + marshmallow for beautiful APIs
License
NotificationsYou must be signed in to change notification settings
marshmallow-code/flask-marshmallow
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Flask-Marshmallow is a thin integration layer forFlask (a Python web framework) andmarshmallow (an object serialization/deserialization library) that adds additional features to marshmallow, including URL and Hyperlinks fields for HATEOAS-ready APIs. It also (optionally) integrates withFlask-SQLAlchemy.
pip install flask-marshmallow
Create your app.
fromflaskimportFlaskfromflask_marshmallowimportMarshmallowapp=Flask(__name__)ma=Marshmallow(app)
Write your models.
fromyour_ormimportModel,Column,Integer,String,DateTimeclassUser(Model):email=Column(String)password=Column(String)date_created=Column(DateTime,auto_now_add=True)
Define your output format with marshmallow.
classUserSchema(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/")defusers():all_users=User.all()returnusers_schema.dump(all_users)@app.route("/api/users/<id>")defuser_detail(id):user=User.get(id)returnuser_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/"# }# }
To learn more about marshmallow, check out itsdocs.
- Docs:https://flask-marshmallow.readthedocs.io/
- Changelog:http://flask-marshmallow.readthedocs.io/en/latest/changelog.html
- PyPI:https://pypi.org/project/flask-marshmallow/
- Issues:https://github.com/marshmallow-code/flask-marshmallow/issues
MIT licensed. See the bundledLICENSE file for more details.
About
Flask + marshmallow for beautiful APIs
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.