plain.flags
Local feature flags via database models.
Overview
You write custom flags as subclasses of Flag.
Each flag defines a "key" (to identify who/what the flag applies to) and an initial value.
The results are stored in the database, allowing you to override them later via the admin.
# app/flags.py
from plain.flags import Flag
class FooEnabled(Flag):
def __init__(self, user):
self.user = user
def get_key(self):
return self.user
def get_value(self):
# Initially all users will have this feature disabled
# and we'll enable them manually in the admin
return False
To check a flag, import it and access the .value property:
from app import flags
if flags.FooEnabled(user).value:
# Feature is enabled for this user
...
You can also use flags directly in boolean expressions since they implement __bool__:
if flags.FooEnabled(user):
# Feature is enabled
...
Usage in templates
You can use flags directly in HTML templates:
{% if flags.FooEnabled(get_current_user()) %}
<p>Foo is enabled for you!</p>
{% else %}
<p>Foo is disabled for you.</p>
{% endif %}
Usage in Python
from app import flags
# Check as a boolean
if flags.FooEnabled(user):
print("Foo is enabled!")
# Get the actual value
print(flags.FooEnabled(user).value)
Advanced usage
You can do whatever you want inside of get_key and get_value. For example, you might want to check URL parameters to temporarily enable a feature during development:
class OrganizationFeature(Flag):
url_param_name = ""
def __init__(self, request=None, organization=None):
# Both of these are optional, but will usually both be given
self.request = request
self.organization = organization
def get_key(self):
if (
self.url_param_name
and self.request
and self.url_param_name in self.request.query_params
):
return None
if not self.organization:
# Don't save the flag result for PRs without an organization
return None
return self.organization
def get_value(self):
if self.url_param_name and self.request:
if self.request.query_params.get(self.url_param_name) == "1":
return True
if self.request.query_params.get(self.url_param_name) == "0":
return False
if not self.organization:
return False
# All organizations will start with False,
# and I'll override in the DB for the ones that should be True
return False
class AIEnabled(OrganizationFeature):
pass
Settings
| Setting | Default | Env var |
|---|---|---|
FLAGS_MODULE |
"app.flags" |
PLAIN_FLAGS_MODULE |
See default_settings.py for more details.
FAQs
How do flags get stored in the database?
When you first use a flag, plain.flags creates a Flag record in the database to track the flag itself, and a FlagResult record for each unique key. The FlagResult stores the computed value so subsequent calls return the cached result.
How do I override a flag value?
You can modify flag results directly in the database or through the admin interface. Each FlagResult has a value field that you can update to override the computed value.
What if I want to temporarily compute the value without storing it?
Return a falsy value (like None) from get_key(). When there's no key, the flag will compute the value fresh each time without storing it in the database.
How do I disable a flag entirely?
Each Flag record has an enabled field. Set it to False to disable the flag. In debug mode, accessing a disabled flag raises a FlagDisabled exception. In production, it logs an error and returns None.
Installation
Install the plain.flags package from PyPI:
uv add plain.flags
Add to your INSTALLED_PACKAGES:
INSTALLED_PACKAGES = [
...
"plain.flags",
]
Create a flags.py at the top of your app (or configure FLAGS_MODULE to point to a different location).
Metadata
Release files for plain.flags 0.38.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| plain_flags-0.38.0.tar.gz | 17.5 kB | Without provenance | Details for plain_flags-0.38.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| plain_flags-0.38.0-py3-none-any.whl | Python 3 | none | any | Details for plain_flags-0.38.0-py3-none-any.whl |
Total release size: 36.7 kB
Release files / plain_flags-0.38.0.tar.gz
| Download URL | plain_flags-0.38.0.tar.gz |
|---|---|
| Size | 17.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
320419462318bde52f8397d9fc181a118c8258a2bb9bb5149049b870dc91f370
|
|
BLAKE2b-256 checksum How to use checksums |
58e796d6da0ee603325efc3f29859722000f70fc2059cb836bb4046d903f8afc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / plain_flags-0.38.0-py3-none-any.whl
| Download URL | plain_flags-0.38.0-py3-none-any.whl |
|---|---|
| Size | 19.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a5d7cdd8c025081c6a16d8c2d2cecf6845ddf4f24fe37c1b389613f762bec490
|
|
BLAKE2b-256 checksum How to use checksums |
ddc31ac41b041deb255dc04de16fbc36d00cd7d95b4480a591760f2183a74c69
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.12.17 {"installer":{"name":"uv","version":"0.12.17","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for plain.flags 0.38.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.