settei.presets.flask — Preset for Flask apps

New in version 0.2.0.

class settei.presets.flask.WebConfiguration(config: Mapping[str, object] = {}, **kwargs)

Settei configuration for the Flask. For more information, See the example below:

config = WebConfiguration.from_path('config.toml')
app = Flask(__name__)
app.config.update(config.web_config)

@app.before_first_request
def before_first_request():
    config.on_web_loaded(app)

app.run()
on_web_loaded(app: flask.app.Flask)

Trigger the web.on_loaded hooks. You should invoke this function when the WSGI app is ready with the WSGI app as argument. You may want to use flask.Flask.before_first_request.

web.on_loaded hook can be a Python code or list of module path.

When web.on_loaded is a single string, it will be interpreted as Python code. The configuration and the WSGI app is injected as self and app each:

[web]
on_loaded = """
print('Hello, world!')
print('self is configuration!: {}'.format(self))
print('app is flask app!: {}'.format(app))
"""

When web.on_loaded is a list of string, it will be interpreted as module paths:

[web]
on_loaded = [
    "utils.hooks:sample_hook",
    "src.main:print_hello_world",
]

The hooks must receive two arguments, Configuration and flask.Flask:

def sample_hook(conf: Configuration, app: Flask):
    print('Hello, world!')
    print('conf is configuration!: {}'.format(conf))
    print('app is flask app!: {}'.format(app))
Parameters:app (flask.Flask) – a ready wsgi/flask app

Changed in version 0.5.2: Hooks list added

web_config

(typing.Mapping) The configuration maping for web that will go to flask.Flask.config.

web_debug

Whether to enable debug mode. On debug mode the server will reload itself on code changes, and provide a helpful debugger when things go wrong.