def create_app(config_module): app = Flask(__name__) app.config.from_object(config_module) # make app.config support dot notation. FlaskDynaconf(app=app) # init Cache cache.init_app(app, { "CACHE_TYPE": "redis", "CACHE_REDIS_URL": app.config.CACHE_REDIS_URL }) # init SocketIO if not app.config.CELERY_BROKER_URL: logging.warn( """app.config.CELERY_BROKER_URL is not set. """ """SocketIO may not work with Celery workers now.""") socket_io.init_app(app=app, message_queue=app.config.CELERY_BROKER_URL) # init Session session.init_app(app=app) # register blueprints from app.api import (config_blp, task_blp) register_blueprints(app, config_blp, task_blp) return app
def test_named_tuple_config_using_initapp(): app = Flask(__name__) FlaskDynaconf(app) app.config["DBDATA"] = DBDATA(server="localhost", port=5432) assert app.config["DBDATA"].server == "localhost" assert app.config["DBDATA"].port == 5432 assert isinstance(app.config["DBDATA"], DBDATA)
def test_dynamic_load_exts_list(settings): """Assert that a config based extensions are loaded""" app = Flask(__name__) app.config["EXTENSIONS"] = ["example.dummy_flask_extension.dummy:init_app"] FlaskDynaconf(app, dynaconf_instance=settings, extensions_list=True) assert app.config.EXTENSIONS == [ "example.dummy_flask_extension.dummy:init_app" ] assert app.is_dummy_loaded is True
def test_dynamic_load_entry_point(settings): """Assert that a config based extensions support entry point syntax""" app = Flask(__name__) app.config["EXTENSIONS"] = [ "example.dummy_flask_extension:dummy_instance.init_app" ] FlaskDynaconf(app, dynaconf_instance=settings) app.config.load_extensions() assert app.config.EXTENSIONS == [ "example.dummy_flask_extension:dummy_instance.init_app" ] assert app.extensions["dummy"].__class__.__name__ == "DummyExtensionType"
def create_app(config_module): app = Flask(__name__) app.config.from_object(config_module) # make app.config support dot notation. FlaskDynaconf(app=app) # register blueprints from app.router import user_api, pair_api register_blueprints(app, user_api) register_blueprints(app, pair_api) return app
def test_flask_dynaconf(settings): """ Test Flask app wrapped with FlaskDynaconf """ app = Flask(__name__) app.config["MY_VAR"] = "foo" FlaskDynaconf(app, dynaconf_instance=settings) app.config["MY_VAR2"] = "bar" assert app.config.HOSTNAME == "host.com" assert app.config.MY_VAR == "foo" assert app.config["HOSTNAME"] == "host.com" assert app.config["MY_VAR"] == "foo" assert app.config.get("HOSTNAME") == "host.com" assert app.config.get("MY_VAR") == "foo" assert app.config("HOSTNAME") == "host.com" assert app.config("MY_VAR") == "foo" assert "HOSTNAME" in app.config assert "MY_VAR" in app.config # ref: #521 assert "NONEXISTENETVAR" not in app.config assert ("NONEXISTENETVAR" in app.config) is False assert "MY_VAR" in app.config assert "MY_VAR2" in app.config assert "MY_VAR" in app.config.keys() assert "MY_VAR2" in app.config.keys() assert ("MY_VAR", "foo") in app.config.items() assert ("MY_VAR2", "bar") in app.config.items() assert "foo" in app.config.values() assert "bar" in app.config.values() assert "MY_VAR" in list(app.config) assert "MY_VAR2" in list(app.config) assert app.config.setdefault("MY_VAR", "default") == "foo" assert app.config.setdefault("MY_VAR2", "default") == "bar" assert app.config.setdefault("DEFAULT_VAR", "default") == "default" assert app.config["DEFAULT_VAR"] == "default" with pytest.raises(KeyError): app.config["NONEXISTENETVAR"] with pytest.raises(AttributeError): app.config.nonexistentattribute
def test_flask_dynaconf(settings): """ Test Flask app wrapped with FlaskDynaconf """ app = Flask(__name__) app.config["MY_VAR"] = "foo" FlaskDynaconf(app, dynaconf_instance=settings) assert app.config.HOSTNAME == "host.com" assert app.config.MY_VAR == "foo" assert app.config["HOSTNAME"] == "host.com" assert app.config["MY_VAR"] == "foo" assert app.config.get("HOSTNAME") == "host.com" assert app.config.get("MY_VAR") == "foo" assert app.config("HOSTNAME") == "host.com" assert app.config("MY_VAR") == "foo"
def test_flask_dynaconf(settings): """ Test Flask app wrapped with FlaskDynaconf """ app = Flask(__name__) app.config['MY_VAR'] = 'foo' FlaskDynaconf(app, dynaconf_instance=settings) assert app.config.HOSTNAME == 'host.com' assert app.config.MY_VAR == 'foo' assert app.config['HOSTNAME'] == 'host.com' assert app.config['MY_VAR'] == 'foo' assert app.config.get('HOSTNAME') == 'host.com' assert app.config.get('MY_VAR') == 'foo' assert app.config('HOSTNAME') == 'host.com' assert app.config('MY_VAR') == 'foo'
def configure_dynaconf(app): settings_file = 'quokka.yml' initial_envmode = app.config.get('ENVMODE') # Extension is supposed to override envmode FlaskDynaconf( app, ENVVAR_FOR_DYNACONF="QUOKKA_SETTINGS_MODULE", DYNACONF_NAMESPACE='QUOKKA', SETTINGS_MODULE_FOR_DYNACONF=settings_file, # extra yaml file override values on settings.yml # secrets file is a hidden file and must be excluded on .gitignore # all password, token and other sensitive must go there # or exported as env var ex: QUOKKA_SECRET_KEY=12345 YAML='.secrets.yml') # Configure extra environment envmode = initial_envmode or app.config.get('ENVMODE') if envmode is not None: yaml_loader.load(obj=app.config, namespace=envmode, filename=settings_file)
def create_app(**config): app = Flask(__name__) FlaskDynaconf(app, **config) return app
from flask import Flask from flask import render_template from dynaconf.contrib import FlaskDynaconf # create your app app = Flask(__name__) # will populate app.config from .env + environment variables FlaskDynaconf(app) @app.route("/") def index(): return render_template("dynaconf.html") @app.route("/test") def test(): return app.config["HELLO"] app.add_url_rule(app.config.get("TEST_RULE", "/default_route"), view_func=lambda: "test") if __name__ == "__main__": app.run()
def test_dynamic_load_exts_no_list(settings): """Assert that a config based extensions are loaded""" app = Flask(__name__) FlaskDynaconf(app, dynaconf_instance=settings, extensions_list=True)
def configure_dynaconf(app): # extra vars in .secrets.yml override values on quokka.yml # secrets file is a hidden file and must be excluded on .gitignore # all password, token and other sensitive must go there # or exported as env var ex: QUOKKA_SECRET_KEY=12345 settings_file = 'quokka.yml,.secrets.yml' initial_envmode = app.config.get('ENVMODE') # Extension is supposed to override envmode FlaskDynaconf(app, ENVVAR_FOR_DYNACONF="QUOKKA_SETTINGS_MODULE", DYNACONF_NAMESPACE='QUOKKA', SETTINGS_MODULE_FOR_DYNACONF=settings_file, DYNACONF_SILENT_ERRORS=True) # Configure extra environment envmode = initial_envmode or app.config.get('ENVMODE') if envmode is not None: yaml_loader.load(obj=app.config, namespace=envmode, filename=settings_file) # overload with envvars env_loader.load_from_env(identifier=envmode, key=None, namespace=f'quokka_{envmode}', obj=app.config, silent=True) # configure theme options app.theme_context = { 'JINJA_ENVIRONMENT': app.jinja_env, 'DEFAULT_LANG': app.config.get('BABEL_DEFAULT_LOCALE'), 'default_locale': app.config.get('BABEL_DEFAULT_LOCALE'), 'PAGES': [], 'pages': [], 'tags': [], 'articles': [], 'categories': [], # https://github.com/getpelican/pelican-plugins/tree/master/tag_cloud 'tag_cloud': [], 'CATEGORIES_URL': 'categories/index.html', 'JINJA_EXTENSIONS': app.jinja_env.extensions, 'USE_LESS': False, # For some themes like bootstrap3 theme SITEURL must be '' 'SITEURL': 'http://localhost:5000', 'THEME_STATIC_DIR': 'theme', 'FAVICON': 'favicon.ico', 'FAVICON_IE': 'favicon.ico', 'FAVICON_FILENAME': 'favicon.ico', # 'AVATAR': 'LOAD FROM UPLOADS', 'NEWEST_FIRST_ARCHIVES': True } # load theme variables from YAML file yaml_loader.load(obj=app.theme_context, namespace='theme', filename=app.config.get('SETTINGS_MODULE')) # overrride with QUOKKA_THEME_ prefixed env vars if exist env_loader.load_from_env(identifier='theme', key=None, namespace='quokka_theme', obj=app.theme_context, silent=True) # remove prefix for pelican-themes active = app.theme_context.get('ACTIVE', 'default') if active.startswith('pelican'): active = active.lstrip('pelican-') app.theme_context['ACTIVE'] = active # load theme specific variables from YAML yaml_loader.load(obj=app.theme_context, namespace=f'theme_{app.theme_context.get("ACTIVE")}', filename=app.config.get('SETTINGS_MODULE')) # overrride with QUOKKA_THEME_THEMENAME prefixed env vars if exist env_loader.load_from_env( identifier=f'theme_{app.theme_context.get("ACTIVE")}', key=None, namespace=f'quokka_theme_{app.theme_context.get("ACTIVE")}', obj=app.theme_context, silent=True) # TODO: LOAD THEME VARS FROM MODEL # mark strings as safe Markup for k, v in app.theme_context.items(): if isinstance(v, str): app.theme_context[k] = Markup(v)
4) Update with data in environmente vars `DYNACONF_NAMESPACE_` YAML files are very useful to have `namespaced` settings, lets say, `production` and `development`. You can also achieve the same using multiple `.py` files naming as `settings.py`, `production_settings.py` and `development_settings.py` (see examples/validator) Now lets apply FlaskDynaconf to this app. """ FlaskDynaconf( app, ENVVAR_FOR_DYNACONF="MYSITE_SETTINGS_MODULE", DYNACONF_NAMESPACE='MYSITE', SETTINGS_MODULE_FOR_DYNACONF='settings.yml', YAML='.secrets.yml', EXTRA_VALUE='You can add aditional config vars here' ) @app.route('/') def index(): return render_template('dynaconf.html') if __name__ == '__main__': app.run()
def configure_dynaconf(app): FlaskDynaconf(app, ENVVAR_FOR_DYNACONF="QUOKKA_SETTINGS_MODULE", DYNACONF_NAMESPACE='QUOKKA', SETTINGS_MODULE_FOR_DYNACONF='settings.yml', YAML='.secrets.yml')