I am about to deploy a very simple flask app on aws Elastic Beanstalk. What ways do I have to put some seed data so that the live instance has some users?
from dateutil import parser
from datetime import datetime
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'db.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
ma = Marshmallow(app)
.
.
.
@app.route('/user/<id>', methods=['PUT'])
def update_user(id):
user = User.query.get(id)
weight = request.json['weight']
user.weight = weight
db.session.commit()
return user_schema.jsonify(user)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
weight = db.Column(db.Float)
workouts = db.relationship('Workout', backref='user', lazy=True)
def __init__(self, name, weight):
self.name = name
self.weight = weight
class UserSchema(ma.Schema):
class Meta:
fields = ('id', 'name', 'weight')
user_schema = UserSchema(strict=True)
users_schema = UserSchema(many=True, strict=True)
.
.
.
db.create_all()
if __name__ == '__main__':
app.run(debug=True)
Again, I want the live instance to have some seed data (I know that I can just create some entries using the console locally). I was thinking that I should put include seeds in the block
if __name__ == '__main__':
user1 = User('Jon',75)
db.session.add(user1)
db.session.commit()
But am not sure what the proper way to do this is. Also wouldn't this run every time the application is started? I just need it to run once the very first time