Skip to main content

django-flippy

A Flipper Cloud package for Django apps. (Not affiliated with the Flipper Cloud folks, just think they're a good idea.) Feature flags for Django projects, because why should the Rails people have all the fun?

Installation

# pip install django-flippy

Next open settings.py. You need to add Flippy to your installed apps, middleware, and template context processors.

# settings.py

INSTALLED_APPS = [
    ...,
    'flippy', # <-- the app
]

MIDDLEWARE = [
    ...,
    'flippy.middleware.flippy_middleware', # <-- the middleware
]

TEMPLATES = [
    {
        ...,
        'OPTIONS': {
            'context_processors': [
                ...,
                'flippy.context.processor',  # <-- the context processor
            ],
        },
    },
]

Finally, ./manage.py migrate to run the migrations.

Usage

Raw, end-to-end example

from django.contrib.auth.models import User

from flippy import Flippy
from flippy.backends import DjangoBackend

f = Flippy(DjangoBackend())

# create a feature
f.create('my_cool_feature')

# enable for half of all users
f.enable_percentage_of_actors('my_cool_feature', 50)

# check if a given user should get the feature
# (depending on chance, the user may or may not be flagged in -- if so,
# crank the percentage_of_actors up to 100 if you want to confirm it worked)
u = User.objects.get(pk=1)
if f.is_enabled('my_cool_feature', u):
  print('This user gets the cool new feature.')
else:
  print('This user gets the old, less cool feature.')

In a view

# views.py
def index(request):
    if request.flippy.feature_exists('my_cool_feature')
        user_has_feature = request.flippy.is_enabled('my_cool_feature', request.user)

    if user_has_feature:
      return render(request, 'cool_new_feature_index.html')

    return render(request, 'index.html')

In a template

# views.py
def index(request):
  return render(request, 'index.html')
<!-- index.html, abridged -->
{% if flippy.my_cool_feature.for_user %}
<p>You have access to the cool new feature!</p>
{% else %}
<p>Nothing new to see here.</p>
{% endif %}

Using Flipper Cloud

The above recipes only use the local Django-based backend and does not connect you to Flipper Cloud. There is a FlipperCloudBackend which expects a Flipper Cloud token passed in the constructor. Be warned: this backend makes direct API calls for every operation.

The better way to use Flipper Cloud is to set yourself up to use the Django backend. Then run this periodically:

FLIPPER_CLOUD_TOKEN=mytoken python manage.py sync-from-cloud

That will sync your Flipper Cloud data down to your local Django backend.

The raw way

from flippy import Flippy
from flippy.backends import FlipperCloudBackend

f = Flippy(FlipperCloudBackend('MY-TOKEN-HERE'))

Using settings.py

In addition to the setup you did for Installation, add the following to your settings.py:

# settings.py

FLIPPY_BACKEND = 'flippy.backends.FlipperCloudBackend'
FLIPPY_ARGS = ['MY-TOKEN-HERE']

This will configure the Flipper Cloud backend everywhere, including the middleware (request.flippy) and context processor ({% if flippy.foo.for_user %}).

Testing

We test with pytest. By default, tests which require an active Flipper Cloud account aren't run. You can run them with pytest -m flippercloud. Note that you must set FLIPPER_CLOUD_TOKEN in your environment for them to run.

Creating and testing a new backend

You may want to store your feature data differently. You can model your backend off of flippy.backends.MemoryBackend, extending flippy.core.BaseBackend and implementing all the methods. That interface is how Flippy (the user side) will expect to interact with you. You can take a look at tests/memory_backend_test.py to see how to run a suite of tests against your implementation.

Metadata

Release files for django-flippy 0.9.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 django-flippy 0.9.0
File Size Uploaded Provenance Actions
django_flippy-0.9.0.tar.gz 16.5 kB Without provenance Details for django_flippy-0.9.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for django-flippy 0.9.0
File Interpreter ABI Platform Actions
django_flippy-0.9.0-py3-none-any.whl Python 3 none any Details for django_flippy-0.9.0-py3-none-any.whl

Total release size: 37.3 kB

Release files / django_flippy-0.9.0.tar.gz

Download URL django_flippy-0.9.0.tar.gz
Size 16.5 kB
Tags Source
SHA-256 checksum
How to use checksums
7bc4118ab8043465a40c74a03524ca50946089221cd62ea009946b8e7f37adc1
BLAKE2b-256 checksum
How to use checksums
a656edd53a84c4e7c2d886969c66bb03ff589cf8473cfd8e622be929810d1a6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.4 Darwin/22.6.0

Release files / django_flippy-0.9.0-py3-none-any.whl

Download URL django_flippy-0.9.0-py3-none-any.whl
Size 20.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
31099212e67736520bc4bf1c889d0400ff7ef1dbf2259d278338866c9fe8552f
BLAKE2b-256 checksum
How to use checksums
fd9ec1664e97682a19c0c6baff631cf8193bf8043c9865ccbbc73c422eaa3524
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.11.4 Darwin/22.6.0

Security for django-flippy 0.9.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