Skip to main content

Weni Feature Flags

Weni feature flags SDK for Python backends

What is it

Weni Feature Flags is a Python Library that functions as an abstraction layer between Django projects and GrowthBook.

Requirements

  • Python: 3.8+
  • Django: 3.2.22+
  • Django REST Framework: 3.12.0+
  • Celery: 5.0+

This project also relies on Celery to update feature flags asynchronously. This library uses Django's cache abstraction to avoid hitting the database constantly and also reduce latency.

Installation

To install it, you can use both pip or Poetry.

Using PIP

pip install weni-feature-flags

Using Poetry

poetry add weni-feature-flags

Initial configuration

On your Django project, you should add "weni.feature_flags" to INSTALLED_APPS. When testing and deploying you should apply the migrations using:

python manage.py migrate

This library saves a snapshot of the feature flags on your database and cache layer to be resilient to eventual GrowthBook unavailability.

Quick Start

Here's a minimal example to get you started:

  1. Install the package:
pip install weni-feature-flags
  1. Add to Django settings:
# settings.py
INSTALLED_APPS = [
    # ... other apps
    'weni.feature_flags',
]

# Environment variables
GROWTHBOOK_CLIENT_KEY = "your-client-key"
GROWTHBOOK_HOST_BASE_URL = "https://your-growthbook-instance.com"
  1. Run migrations:
python manage.py migrate
  1. Use in your code:
from weni.feature_flags.shortcuts import is_feature_active

# Check if a feature is active for a user
if is_feature_active("new-dashboard", "user@example.com", "project-uuid"):
    # Show new dashboard
    pass

SDK Connection

On GrowthBook, you can create a Python SDK connection, which you generate your client key. You should save this client key and use it as the GROWTHBOOK_CLIENT_KEY environment variable, as well as your GrowthBook host to GROWTHBOOK_HOST_BASE_URL.

Remember to select the correct environment and project on the "New SDK Connection" modal.

Webhooks

You can also configure webhooks to apply feature flags changes faster. For this, first import the webhook view and configure it in your Django project's URLs

from django.urls import path
from weni.feature_flags.views import FeatureFlagsWebhookView

urlpatterns = [
    path('webhooks/feature-flags/', FeatureFlagsWebhookView.as_view(), name='feature-flags-webhook'),
]

Then, create a strong secret and save it to GROWTHBOOK_WEBHOOK_SECRET.

On Growthbook, access Settings -> Webhooks and create a new event webhook. It should be configured to use POST as the method and, on the Headers (JSON) section, include the secret that you created:

{
    "secret": "<YOUR SECRET HERE>"
}

You should select the events "feature.created", "feature.updated" and "feature.deleted" and select your environment and project.

Usage

Using the Service Class

You can import and use the whole feature flags service like this:

from weni.feature_flags.services import FeatureFlagsService

service = FeatureFlagsService()

# Get all feature flags data
features = service.get_features()

# Get active feature flags for specific attributes
active_features = service.get_active_feature_flags_for_attributes({
    "userEmail": "user@example.com",
    "projectUUID": "8065619a-2b22-4351-9914-e37c6394b1d3"
})

# Evaluate a specific feature flag by attributes
is_active = service.evaluate_feature_flag_by_attributes(
    "feature-key", 
    {
        "userEmail": "user@example.com",
        "projectUUID": "8065619a-2b22-4351-9914-e37c6394b1d3"
    }
)

Using Shortcuts

If you only really need to check whether a user and project have access to a feature, you can use shortcuts:

from weni.feature_flags.shortcuts import is_feature_active, is_feature_active_for_attributes

Using Shortcuts

If you want to check if a certain user in a certain project has access to a feature, you can use

# feature key/name, user's email, project's UUID
if is_feature_active("featureKey", "user@email.com", "8065619a-2b22-4351-9914-e37c6394b1d3"):
    # Something cool happens

Or, if you set the feature up in a different way, you can pass the attributes directly using:

# Just checks the project

# feature key/name, attributes
if is_feature_active_for_attributes("featureKey", {"projectUUID": "8065619a-2b22-4351-9914-e37c6394b1d3"}):
    pass

# Just checks the user's email
if is_feature_active_for_attributes("featureKey", {"userEmail": "user@email.com"}):
    pass

# Or any other rule that you have defined on GrowthBook
if is_feature_active_for_attributes("featureKey", {"example": "test"}):
    pass

Technical Details

Database Models

This library creates the following database models:

  • FeatureFlag: Stores feature flag definitions and their current state
  • FeatureFlagSnapshot: Maintains historical snapshots of feature flags for resilience

Caching Strategy

  • Feature flags are cached using Django's cache framework
  • Cache keys are prefixed with CACHE_KEY_PREFIX (default: "weni_feature_flags")
  • Cache TTL is configurable via FEATURES_CACHE_TTL (default: 60 seconds)
  • When GrowthBook is unavailable, the library falls back to cached data

Troubleshooting

Common Issues

Feature flags not updating

  • Check if Celery is running and processing tasks
  • Verify webhook configuration and secret
  • Check GrowthBook connection settings

GrowthBook connection errors

  • Verify GROWTHBOOK_CLIENT_KEY and GROWTHBOOK_HOST_BASE_URL
  • Check network connectivity to GrowthBook
  • Review timeout settings (GROWTHBOOK_REQUESTS_TIMEOUT)

Cache issues

  • Ensure that you have a caching backend, such as Redis, configured

Webhook not working

  • Verify webhook URL is accessible
  • Check GROWTHBOOK_WEBHOOK_SECRET matches GrowthBook configuration
  • Review webhook event selection in GrowthBook

All environment variables

Variable Required Default Description
GROWTHBOOK_CLIENT_KEY Yes - Client key for GrowthBook SDK connection
GROWTHBOOK_HOST_BASE_URL Yes - Base URL of your GrowthBook instance
GROWTHBOOK_REQUESTS_TIMEOUT No 60 Timeout in seconds for GrowthBook API requests
GROWTHBOOK_WEBHOOK_SECRET No - Secret key for webhook authentication
CACHE_KEY_PREFIX No weni_feature_flags Prefix for cache keys
FEATURES_CACHE_TTL No 60 Cache TTL in seconds for feature flags

License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

Metadata

Release files for weni-feature-flags 2.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for weni-feature-flags 2.2.0
File Size Uploaded Provenance
weni_feature_flags-2.2.0.tar.gz 25.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for weni-feature-flags 2.2.0
File Interpreter ABI Platform
weni_feature_flags-2.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 56.7 kB

Release files / weni_feature_flags-2.2.0.tar.gz

Download URL weni_feature_flags-2.2.0.tar.gz
Size 25.8 kB
Tags Source
SHA-256 checksum
How to use checksums
145bfe816354bed204dd5efb4981cf1b4ec8df31d058740a669a8f400b8215d1
BLAKE2b-256 checksum
How to use checksums
52a651b1bb9ec3e50a3cb6b81eeb72bc00d611908744d6fd7c7a5df9dfa3741a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.8.18 Linux/6.17.0-1018-azure

Release files / weni_feature_flags-2.2.0-py3-none-any.whl

Download URL weni_feature_flags-2.2.0-py3-none-any.whl
Size 30.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a3d1898fa4992eca979261978d3db996973adff23a7cf98a75d5a22f73ba6b83
BLAKE2b-256 checksum
How to use checksums
e2471d9e6be5c03a600b6d2392b549338dc76892fc5b4eb36b54a6468f90cca7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.5 CPython/3.8.18 Linux/6.17.0-1018-azure

Security for weni-feature-flags 2.2.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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page