Feature settings

Table of contents

  1. Introduction
  2. Taking the fields from your parameters
  3. Reading the values
  4. Shadowing an environment variable
  5. Reference
    1. register_settings(feature, title, fields=None, icon="sliders")
    2. Field keys
    3. Types
    4. Functions
  6. See also

Introduction

A feature can let an administrator change some of its settings from the admin panel. It declares a typed schema in init_feature(app), and the feature that provides the settings panel draws the form. You write no panel.

def init_feature(app):
    from splent_framework.settings.settings_schema import register_settings

    register_settings(
        "gallery",
        "Gallery",
        [
            {"key": "autoplay", "type": "bool", "default": "1", "label": "Autoplay"},
            {"key": "interval", "type": "int", "default": "6", "label": "Interval (seconds)"},
        ],
        icon="image",
    )

Install the feature and its form appears. Remove it and the form goes.


Taking the fields from your parameters

register_settings("gallery", "Gallery", icon="image")

Leave the fields out and you get one per bind = "runtime" parameter of the feature’s [tool.splent.config_schema], so each setting is declared once. With no such parameter, the call raises ValueError.


Reading the values

from splent_framework.settings.settings_schema import get_config, setting_value

get_config("gallery")                 # {"autoplay": True, "interval": 6}
setting_value("gallery", "interval")  # 6
{{ feature_setting('gallery', 'interval') }}

A value comes from the first place that has one. What an administrator saved, then app.config (the product’s environment), then the schema’s default. Without a feature that provides SettingsService, the first step is skipped.

config.GALLERY_INTERVAL in a template reads the environment alone, and misses what the panel changed.


Shadowing an environment variable

Each field shadows an app.config key, <FEATURE>_<KEY> in uppercase (GALLERY_INTERVAL). Set env when the variable is named otherwise, and declare it under [tool.splent.config] in the feature’s pyproject.toml.

{"key": "site_title", "env": "SITE_TITLE", "type": "text", "default": "", "label": "Site title"}

Reference

register_settings(feature, title, fields=None, icon="sliders")

feature is a stable key, used in the panel’s URL and as the prefix of the stored keys. title names the form, and icon is a Feather icon.

Field keys

Key Required Description
key yes Short key, stored as <feature>_<key>
type yes bool, int, text, color or select
default yes Used until somebody sets a value
label yes Label in the form
help no Help text under the field
options for select A list of (value, label) pairs
env no The app.config key it shadows

Types

Type Renders as Read as
bool Checkbox True for 1, true, yes or on
int Number input int, else the default, else 0
text Text input A string. A list in app.config is joined with commas
color Colour picker #rgb to #rrggbbaa, else the default, else #000000
select Dropdown The chosen value

Functions

All in splent_framework.settings.settings_schema.

Function Returns
get_config(feature) Every value, typed, by short key
setting_value(feature, key) One value. In templates, feature_setting
get_schemas(), get_schema(feature) Every schema by title, or one
clear_settings_schemas() Empties the registry, as each app does before it loads its features

See also


Back to top

splent. Distributed by an LGPL license v3. Contact us: drorganvidez@us.es