liveBook · Manning

Configuration

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

Configuration data is information that determines how an application and its services are set up and run.

Configuration data determines how an application and its services are set up and run. Separating it from application code makes development, staging, and production easier to maintain. section 7.6 section 7.6.1

Use distinct configuration for each environment:

  • Development can enable debugging and expose additional application information. section 7.6.1
  • Staging gives a team an environment for testing before deployment. section 7.6.1
  • Production removes debug information and restricts access to the intended public feature set. section 7.6.1

TOML files provide human-readable configuration with typed values, including Boolean values and lists. Sections such as default, development, and production let the active environment select appropriate settings while sharing inherited defaults. section 7.7.1

Configuring a Flask application

Dynaconf loads TOML configuration into Flask. A default section can hold shared values such as SECRET_KEY, while development enables the Flask Debug Toolbar and production disables debugging. Set FLASK_ENV before running the application so the matching section is used. Keep the secret value private and generate it with Python’s secrets module rather than revealing it in shared code. section 7.7.1

[default]                                                    #1
 
[development]                                                #2
flask_debug = true                                           #3
extensions = ["flask_debugtoolbar:DebugToolbarExtension"]    #4
debug_tb_enabled = true                                      #4
 
[production]                                                 #4
flask_debug = false                                          #5

Inspect a representative settings.toml file showing shared defaults, development-only debugging, extensions, and production settings. section 7.7.1

import os
import sys
from flask import Flask
from dynaconf import FlaskDynaconf                        #1
 
def create_app():
    app = Flask(__name__)
    dynaconf = FlaskDynaconf(extensions_list=True)        #2
 
    with app.app_context():
        os.environ[
            "ROOT_PATH_FOR_DYNACONF"
        ] = app.root_path                               #3
        dynaconf.init_app(app)                            #4
        app.config["SECRET_KEY"] = 
        bytearray(app.config["SECRET_KEY"], "UTF-8")    #5
        from . import intro
        app.register_blueprint(intro.intro_bp)
        return app

Inspect the Flask application factory wiring that initializes FlaskDynaconf and applies the configured secret key. section 7.7.1

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Explains environment separation, TOML sections, typed values, inheritance, loading, and secret generation without detailing the configuration library internals.
Examples
Uses MyBlog service settings, TOML files, FLASK_ENV, SECRET_KEY, Dynaconf, and Flask Debug Toolbar settings.
What it assumes
Assumes a Flask application and Python virtual environment while teaching environment-specific settings and cryptographically strong secret generation.
What you can do after
Configure a Flask application with Dynaconf, environment-specific TOML settings, and a privately generated secret key.
How it connects
Links application configuration with Flask, TOML, environment variables, secrets, and the Flask Debug Toolbar.

Also explained in

Compare how 23 books explain configuration →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.