PRISM is a simple way to manage your Flask APIs, providing consistent models, version management, access control, and more while leaving you in full control of your code. It's even super easy to use.
Check out this Flask-SQLAlchemy model that's using PRISM:
from prism import p
class User(db.Model):
# Define table name
__tablename__ = 'users'
# Primary key, user ID
user_id = db.Column(db.Integer, primary_key=True, autoincrement=True, index=True)
# First name
first_name = db.Column(db.String(255))
# Email
email = db.Column(db.String(255))
# Password
password = db.Column(db.String(255))
@p.api_representation()
def as_data(self):
return {
'id': self.user_id,
'email': self.email,
'first_name': self.first_name
}
Now in our API let's set up a route to return all users. All we do is return our Flask-SQLAlchemy models through a PRISM Refract
from flask import Blueprint
from models import User
from flask.ext.prism import Refract
bp = Blueprint('api', __name__, url_prefix="/api")
@bp.route('/users')
def api_users_get():
# Return with as_list=True, this forces the JSON to be an array even if there's only one result.
return Refract(User.query.all(), as_list=True)
Which returns a nice clean and easy output
[
{
"user_id": 1,
"email": "some_email@internet.co",
"first_name": "Roger"
},
{
"user_id": 2,
"email": "just@use.slack",
"first_name": "Jennifer"
}
]
- Easily manage your JSON models from a predictable location
- Insert permission based rules into your JSON output
- Models and permission checks can be easily versioned by passing a
version=INTEGER
parameter
You can grab Flask-PRISM using pip (our Pypi page is over here)
$ pip install Flask-PRISM
Instantiate our Prism object in a separate file, in this example it's in prism.py
from flask.ext.prism import Prism
p = Prism()
And finally, call init_app()
during setup.
from flask import Flask
from flask.ext.prism import Refract
from prism import p
from models import User
# Setup flask like we normally do
app = Flask(__name__)
# Setup PRISM to see Flask
p.init_app(app)
@app.route('/api/users/<int:user_id>')
def api_user_get(user_id):
return Refract(User.query.get(user_id))
if __name__ == '__main__':
app.run()
Which returns a nice clean and easy output
{
"user_id": 1,
"email": "some_email@internet.co",
"first_name": "Roger"
}
You can find all the Flask-PRISM documentation at flask-prism.readthedocs.org
Want to contribute? Awesome! If you've made a change you think will help other PRISM users, just open a pull request or raise an issue on GitHub.
If you're not already there, it's over at https://github.com/patrickmccallum/flask-prism
Didn't read the docs? My Twitter is @patsnacks.
- Renamed ReturnableResponse to Refract, shorter, makes more sense given the context
- Fixed new line issue in response mapping
- Fixed an issue with the as_list parameter that could cause it to be ignored in certain cases
- Tweaked the docs
- Fixed issue that would cause deployments to fail with Apache's mod_wsgi, and subsequently AWS Elastic Beanstalk
- PRISM representations can now contain other objects with representations! They'll be auto converted on the way out
- Removed stray print statements used for debugging
- Added documentation
- Initial release
Flask-PRISM is distributed under the MIT license. See the LICENSE file included in this project for more.