Jan-28-2019, 02:04 PM
while building a flask app around an existing database i realized that flask refuse to load the instance specific config.
instance config
instance config
# This file contains configuration variables that shouldn’t be in version control. # This includes things like API keys and database URIs containing passwords. # This also contains variables that are specific to this particular instance of your application. # For example, you might have DEBUG = False in config.py, but set DEBUG = True in instance/config.py on your local machine for development. # Since this file will be read in after config.py, it will override it and set DEBUG = True. import os BASEDIR = os.path.abspath(os.path.dirname(__file__)) TOP_LEVEL_DIR = os.path.abspath(os.curdir) SECRET_KEY = 'cYO13e8bFCMylMAKvicA' SQLALCHEMY_DATABASE_URI = 'mysql://aix_reg:somepass@localhost/aix_registry' SQLALCHEMY_TRACK_MODIFICATIONS = False__init__.py
# This file initializes your application and brings together all of the various components.
# third-party imports
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_admin import Admin
from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
# local imports
from config import app_config
from app import models
from app import views
from app import forms
# db variable initialization
db = SQLAlchemy()
"""Reflects all tables to declaratives
Given a valid engine URI and declarative_base base class
reflects all tables and imports them to the global namespace.
Returns a session object bound to the engine created.
"""
def reflect_all_tables_to_declarative(uri):
# create an unbound base our objects will inherit from
Base = declarative_base()
engine = create_engine(uri)
metadata = MetaData(bind=engine)
Base.metadata = metadata
g = globals()
metadata.reflect()
for tablename, tableobj in metadata.tables.items():
g[tablename] = type(str(tablename), (Base,), {'__table__' : tableobj })
print("Reflecting {0}".format(tablename))
Session = sessionmaker(bind=engine)
return Session()
def create_app(config_name):
app = Flask(__name__, instance_relative_config=True)
app.config.from_object(app_config[config_name])
app.config.from_pyfile('config.py')
admin = Admin(app)
session = reflect_all_tables_to_declarative(SQLALCHEMY_DATABASE_URI)
db.init_app(app)
return appresult* Serving Flask app "run"
* Forcing debug mode on
* Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 535-055-604
10.133.90.37 - - [28/Jan/2019 15:03:20] "GET /admin/ HTTP/1.1" 500 -
Traceback (most recent call last):
File "/root/flask/aix_registry/run.py", line 8, in <module>
app = create_app(config_name)
File "/root/flask/aix_registry/app/__init__.py", line 59, in create_app
session = reflect_all_tables_to_declarative(SQLALCHEMY_DATABASE_URI)
NameError: global name 'SQLALCHEMY_DATABASE_URI' is not defined
