version number: 0.0.7
SQLAlchemy Standarized Image Field for Flask
- Storage backends (FileStorage, S3Storage)
- Thumbnails (wand)
- Flask Application Factory compatible
To install use pip:
$ pip install Flask-ImageAlchemy
Or clone the repo:
$ git clone https://github.com/rstit/flask-image-alchemy.git
$ python setup.py install
storage = S3Storage()
storage.init_app(app)
class User(db.Model):
__tablename__ = 'example'
id = db.Column(db.Integer, primary_key=True)
image = db.Column(
StdImageField(
storage=storage,
variations={
'thumbnail': {"width": 100, "height": 100, "crop": True}
}
), nullable=True
)
If you need S3Starage, set up config in your flask application:
AWS_ACCESS_KEY_ID = "you-api-key"
AWS_SECRET_ACCESS_KEY = "you-secret-key"
AWS_REGION_NAME = "bucket-region"
S3_BUCKET_NAME = "bucket-name"
If you need FileStorage and custom MEDIA_PATH
:
MEDIA_PATH = "/var/www/assets/images/"
u = User()
u.avatar = file # werkzeug.datastructures.FileStorage
u.save()
And you have access to thumbnails:
u.avatar.url
u.avatar.thumbnail
u.avatar.thumbnail.url
u.avatar.thumbnail.path
Implementing file deletion should be done inside your own applications using the before_delete
signal. Clearing the field if blank is true, does not delete the file. This can also be achieved
using before_update
signals. This packages contains two event callback methods that handle file
deletion for all SdtImageFields of a model.
from flask_image_alchemy.events import before_update_delete_callback, before_delete_delete_callback
from sqlalchemy.event import listen
listen(User, 'before_update', before_update_delete_callback)
listen(User, 'before_delete', before_delete_delete_callback)
- Validators (MinSizeValidator, MaxSizeValidator)
- Flask-Admin widget
- Coverage
- Docs Page
- Async Jobs (Image Processing)