Library that works with Flask & SqlAlchemy to store files in your database and server.
Project description
Library that works with Flask & SqlAlchemy to store files in your database
Read the docs: Documentation
Installation
pip install flask-file-upload
Flask File Upload
General Flask config options
UPLOAD_FOLDER = join(dirname(realpath(__file__)), "uploads/lessons")
ALLOWED_EXTENSIONS = ["jpg", "png", "mov", "mp4", "mpg"]
MAX_CONTENT_LENGTH = 1000 * 1024 * 1024 # 1000mb
Setup
# my_app.py
app = Flask(__name__)
db = SQLAlchemy()
file_upload = FileUpload()
def create_app():
db.init_app(app)
file_upload.init_app(app)
# Or we can pass directly:
db = SQLAlchemy(app)
file_upload = FileUpload(app)
FlaskFileUploads needs to do some work with your SqlAlchemy model
Decorate your SqlAlchemy model with file_upload's Model class:
from my_app import db, file_upload
@file_upload.Model
class ModelTest(db.Model):
__tablename__ = "tests"
id = db.Column(db.Integer, primary_key=True)
# Your files - Notice how we pass in the SqlAlchemy instance
# or `db` to the `file_uploads.Column` class:
my_placeholder = file_upload.Column(db)
my_video = file_upload.Column(db)
define files to be upload:
(This is an example of a video with placeholder image attached):
my_video = request.files["my_video"]
placeholder_img = request.files["placeholder_img"]
Get main form data and pass to your SqlAlchemy Model
blog_post = BlogPostModel(title="Hello World Today")
file_upload.save_files(blog_post, files={
"my_video": my_video,
"placeholder_img": placeholder_img,
})
Update file
blog_post = BlogPostModel(title="Hello World Today")
blog_post = file_upload.update_files(blog_post, files={
"my_video": new_my_video,
"placeholder_img": new_placeholder_img,
})
Delete files
file_upload.delete_files(BlogPostModel, files=["my_video"])
Stream a file
First get your entity
my_blog_post = BlogModel().get(id=1) # Or your way of getting an entity
file_upload.stream_file(blog_post, filename="my_video")
File Url paths
file_upload.get_file_url(blog_post, filename="placeholder_img")
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.