Zum Inhalt springen

Setting up config variables in a Flask app

Here is how I set up configuration files in my Flask app.

my_app/config.py:

import os
basedir = os.path.abspath(os.path.dirname(__file__))

class Config(object):
    SQLALCHEMY_DATABASE_URI = 'sqlite:///test_whatever.db'
    SQLALCHEMY_TRACK_MODIFICATIONS = False

class ProductionConfig(Config):
    SQLALCHEMY_DATABASE_URI = 'sqlite:///prod_whatever.db'
    SQLALCHEMY_TRACK_MODIFICATIONS = False

class DevelopmentConfig(Config):
    SQLALCHEMY_DATABASE_URI = 'sqlite:///test_whatever.db'
    SQLALCHEMY_TRACK_MODIFICATIONS = False

my_app/app/app.py:

from flask import Flask, send_from_directory
from config import ProductionConfig, Config, DevelopmentConfig
from app.models import db
import os

def create_app():
    app = Flask(__name__)

    # Here, you refer to the DevelopmentConfig as a string
    app.config.from_object('config.DevelopmentConfig')

    # more init stuff here, blueprints, etc

    db.init_app(app)
    return app

You can find a more exhaustive guide to configuration here.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert