Flask extension to seed database through scripts
Project description
Flask-Seeder
Flask-Seeder is a Flask extension to help with seeding database with initial data, for example when deploying an application for the first time.
This extensions primary focus is to help populating data once, for example in a demo application where the database might get wiped over and over but you still want users to have some basic data to play around with.
Installation
pip install Flask-Seeder
This will install the Flask-Seeder extension and add a flask seed
subcommand, check it out to see what arguments are supported!
Seeders
Flask-Seeder provides a base class Seeder
that holds a database handle.
By subclassing Seeder
and implementing a run()
method you get access to the database handle object and can start seeding the database with data.
All seeders must be somewhere in the seeds/
directory and inherit from Seeder
or else they won't be detected.
Example usage
Examples show only relevant snippets of code
app.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_seeder import FlaskSeeder
create_app():
app = Flask(__name__)
db = SQLAlchemy()
db.init_app(app)
seeder = FlaskSeeder()
seeder.init_app(app, db)
return app
seeds/demo.py:
from flask_seeder import Seeder
# All seeders inherit from Seeder
class DemoSeeder(Seeder):
# run() will be called by Flask-Seeder after instantiating the class
def run(self):
self.db.session.add(...) # Standard SQLAlchemy add
self.db.session.commit()
Shell
$ flask seed run
Running database seeders
DemoSeeder... [OK]
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.
Source Distribution
Built Distribution
Hashes for Flask_Seeder-0.1a1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cb288c84c32854ce63f214a43cb923cff18ca910369a474d4f7724411afef3a |
|
MD5 | 708aaf7f6ab64464cadc80cf8c705e5e |
|
BLAKE2b-256 | 8b139b2687a676e37dbbdb6f848d0c3dde499be5a0541ddb12f9f96a6f0fa801 |