API

Here you can find part of documentation that covers all public interfaces of Flask-Constance.

Extension object

class flask_constance.Constance(app=None, backend=None, cache=None, view_base_url=None, view_class=<class 'flask_constance.view.ConstanceView'>)[source]

Bases: object

Constance extension

Parameters:
  • app (Optional[Flask]) – Flask application.

  • backend (Optional[Backend]) – Backend instance to use.

  • backend_cache – Cache for the backend.

  • view_base_url (Optional[str]) – Base URL to register REST-like view for settings.

  • view_class (Type[ConstanceView]) – Class to register as view.

init_app(app)[source]

Initialize extension with Flask application.

Parameters:

app (Flask) – Flask application.

Raises:
  • RuntimeError – If Constance extension was already initialized.

  • ValueError – If invalid setting name was provided in CONSTANCE_PAYLOAD application config value.

Return type:

None

Global settings object

flask_constance.settings

With this global you can access settings from any point of your application. This object actually is werkzeug’s LocalProxy pointing to Storage object.

Storage aka settings object

class flask_constance.storage.Storage(backend, cache=None)[source]

Bases: object

Flask-Constance settings storage.

This is access point for getting and setting constance settings.

Parameters:
  • backend (Backend) – Backend instance to use. By default is Memory backend.

  • cache (Optional[BackendCache]) – Caching backend to use. This is optional.

Backends

Flask-Constance supports various types of backends. All of them implements Backend or flask_constance.backends.base.BackendCache protocols.

Backend Protocol

class flask_constance.backends.base.Backend(*args, **kwargs)[source]

Bases: Protocol

Base backend class.

get(name)[source]
Return type:

Any

set(name, value)[source]
Return type:

None

Backend Cache Protocol

class flask_constance.backends.base.BackendCache(*args, **kwargs)[source]

Bases: Protocol

Base backend cache class.

get(name)[source]
Return type:

Any

invalidate(name)[source]
Return type:

None

set(name, value)[source]
Return type:

None

Memory Backend object

Memory backend was implemented for testing purposes. It can be used only in single-process mode, so it really doesn’t fit production environment requirenments.

class flask_constance.backends.memory.MemoryBackend[source]

Bases: Backend

In-memory backend for testing purposes.

get(name)[source]

Get setting value.

Parameters:

key – Name of the setting.

Return type:

Any

set(name, value)[source]

Set setting value

Parameters:
  • key – Name of the setting.

  • value (Any) – Value of the setting.

Return type:

None

Flask-SQLAlchemy backend object

This backend implements intergration with Flask-SQLAlchemy extension as main settings storage.

class flask_constance.backends.fsqla.FlaskSQLAlchemyBackend(model, session)[source]

Bases: Backend

Flask-SQLAlchemy backend

Parameters:
  • model (DeclarativeMeta) – Model which describes settings.

  • session (scoped_session) – Database session.

get(name)[source]

Get setting value.

Parameters:

key – Name of the setting.

Return type:

Any

set(name, value)[source]

Set setting value

Parameters:
  • key – Name of the setting.

  • value (Any) – Value of the setting.

Return type:

None

Flask-SQLAlchemy model mixin

Mixin that will define all needed sqlalchemy fiels for your model.

class flask_constance.backends.fsqla.SettingMixin[source]

Bases: object

Model mixin for Flask-SQLAlchemy backend

id = Column(None, Integer(), table=None, primary_key=True, nullable=False)
name = Column(None, String(length=256), table=None, nullable=False)
value = Column(None, JSON(), table=None)

Signals

flask_constance.signals.constance_setup

Signal that called after extension was initialized. Called with 2 agruments:

  • Instance of Constance.

  • Instance of Flask application.

flask_constance.signals.constance_get

Signal that called after setting value was accessed. Called with 2 arguments:

  • Instance of Constance.

  • Name of the setting that was accessed.

flask_constance.signals.constance_set

Signal that called after setting value was updated. Called with 3 arguments:

  • Instance of Constance.

  • Name of the setting that was updated.

  • New value of the setting.

RESTlike view

View implementing RESTlike interface for managing Flask-Constance dynamic settings.

class flask_constance.view.ConstanceView[source]

Bases: MethodView

Method view for managing dynamic settings.

delete(name)[source]

Delete(actually reset) setting value.

get(name=None)[source]

Get specific setting or all settings.

methods: ClassVar[Optional[Collection[str]]] = {'DELETE', 'GET', 'PUT'}

The methods this view is registered for. Uses the same default (["GET", "HEAD", "OPTIONS"]) as route and add_url_rule by default.

put(name)[source]

Update value for the setting.