title: Python Flask create schedule on startup
body: <p>I am new-ish to python and new to Flask. I am trying to use <code>APScheduler</code> to schedule a <code>db</code> update when the app starts. It works until I tried to update the database. It says the <strong>database is not defined</strong>, but I am importing with <code>from .models import Readings</code>. Error can be recreated by uncommenting the lines in <code>interval_task()</code>.</p>
  <p><strong>init</strong>.py</p>
  <pre><code>from flask import Flask
  from flask_sqlalchemy import SQLAlchemy
  from flask_apscheduler import APScheduler
  from os import path
  from flask_login import LoginManager
  import pandas as pd
  import multiprocessing
  import random
  
  db = SQLAlchemy()
  DB_NAME_LEOTTA = &quot;leotta.db&quot;
  DB_NAME_QUICK = &quot;quick.db&quot;
  simulated_room_temperature = multiprocessing.Value('d', 29)
  
  def create_app():
      app = Flask(__name__)
      app.config['SECRET_KEY'] = '0ddish'
      app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///databases/{DB_NAME_LEOTTA}'
      app.config['SQLALCHEMY_BINDS'] = {'quickdb' : f'sqlite:///databases/{DB_NAME_QUICK}'}
      db.init_app(app)
      
      from .views import views
      from .auth import auth
      from .scheduler import scheduler
  
      app.register_blueprint(views, url_prefix='/')
      app.register_blueprint(auth, url_prefix='/')
      app.register_blueprint(scheduler, url_prefix='/')
  
      from .models import User, Note, Readings
  
      create_database(app)
  
      sched = APScheduler()
      sched.init_app(app)
      sched.start()
      INTERVAL_TASK_ID = '1'
      sched.add_job(id=INTERVAL_TASK_ID, func=interval_task, trigger='interval', seconds=5)
  
      login_manager = LoginManager()
      login_manager.login_view = 'auth.login'
      login_manager.init_app(app)
  
      @login_manager.user_loader
      def load_user(id):
          return User.query.get(int(id))
  
      return app
  
  def create_database(app):
      if not path.exists('website/databases/' + DB_NAME_LEOTTA):
          #since no bind is specified, it will use default bind
          db.create_all(app=app)
          print('Created Database ' + DB_NAME_LEOTTA)
      if not path.exists('website/databases/' + DB_NAME_QUICK):
          db.create_all(bind=DB_NAME_QUICK)
          print('Created Database ' + DB_NAME_QUICK)
  
  def interval_task():
      simulated_room_temperature.value = random.uniform(19, 31)
      #reading = Readings(param_name='Temperature', param_value=simulated_room_temperature.value)
      #db.session.add(reading)
      #db.session.commit()
      print(random.uniform(19, 31))
  </code></pre>
  <p>main.py</p>
  <pre><code>from website import create_app
  
  app = create_app()
  
  if __name__ == '__main__':
      app.run(debug=True)
  </code></pre>
  <p>models.py</p>
  <pre><code>from . import db
  from flask_login import UserMixin
  from sqlalchemy.sql import func
  
  class Note(db.Model):
      #no bind specified, will use default
      id = db.Column(db.Integer, primary_key=True)
      note = db.Column(db.String(150))
      date_time = db.Column(db.DateTime(timezone=True), default=func.now('UTC'))
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
  
  class User(db.Model, UserMixin):
      #no bind specified, will use default
      id = db.Column(db.Integer, primary_key=True)
      email = db.Column(db.String(150), unique=True)
      password = db.Column(db.String(150))
      first_name = db.Column(db.String(150))
      notes = db.relationship('Note')
  
  class Readings(db.Model):
      __bind_key__ = 'quickdb'
      id = db.Column(db.Integer, primary_key=True)
      param_name = db.Column(db.String(150), unique=True)
      date_time = db.Column(db.DateTime(timezone=True), default=func.now('UTC'))
      param_value = db.Column(db.Float)
  </code></pre>