Skip to main content

django-bandits

This package enables optimization of your Django site via multi-armed bandits.

This package extends Django-Waffle by wrapping the feature flipper functions with bandit algorithms. This allows automatic testing and optimization as users interact with your site.

Installation

pip install django-bandits

or

poetry add django-bandits

Additionally, you can install it directly from GitHub:

Pip

pip install git+https://github.com/hubbs5/django-bandits.git

Poetry

poetry add git+https://github.com/hubbs5/django-bandits.git

Django Settings

Go to your settings.py file and add waffle to your installed apps list:

INSTALLED_APPS = [
    ...
    "waffle",
    "django_bandits",
    ...
]

Additionally, add django_bandits.middleware.UserActivityMiddleware to MIDDLEWARE after CSRF and authentication middleware, e.g.:

MIDDLEWARE = [
    ...
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django_bandits.middleware.UserActivityMiddleware", # It goes here!
    ...
]

Update settings.py to exclude any particular URL from conversion tracking, e.g.:

EXCLUDE_FROM_TRACKING = [
    ADMIN_URL,
]

Additionally, you can add a REGEX option to filter URLs. Helps to ensure you aren't picking up bot traffic that's trying to break into a Wordpress site or something of the sort.

EXCLUDE_FROM_TRACKING_REGEX = r"\.php|wordpress|\/wp-"

Finally add the following line to your settings.py file to ensure the bandit model is overriding Waffle's Flags.

WAFFLE_FLAG_MODEL = "django_bandits.BanditFlag"

Migrations

Create migrations by running python manage.py makemigrations followed by python manage.py migrate.

Enabling Bandits

With migrations complete, go to the Django site admin page. You should see something like this:

View of Django admin page

Select Flags under DJANGO_BANDITS and click "Add."

Make a name for the flag - typically the feature you want to test. Set it to test for whatever group you'd like to test it on. Refer to the Waffle documentation for details.

The bandit needs a FLAG URL to be set - this is the URL where the flag will be shown to users and is needed to track conversions and call the bandit to flip the feature when users reach that URL. Enter this as the Source URL.

To track a conversion, then you need to add a Target URL, which is where you want the user to end up.

In the example below, we're tracking the homepage (/) and want to see how many users click on a link to go to the contact page (/contact/).

FLAG URL settings determines a successful conversion

This will consider any user who views your source URL and target URL to be a conversion during the session and will update the count shown in the image above.

To add a bandit, you'll need to select one of the bandit options from the list below: Select from Epsilon Greedy, Epsilon Decay, or UCB1 Bandits

Some bandits have customizable parameters (e.g. how frequently a random action is taken such as $\epsilon$). All allow you to set a minimum number of views and confidence interval before a winning version is selected (see more details below).

Now that a bandit is enabled, you need to update your templates or views in as done with Waffle to enable the feature flipping flag.

In a template, it may be something like this:

{% extends "base.html" %}
{% load waffle_tags %}

{% block content %}
{% flag "headline-flag" %}
    <h1>This is my test headline: Waffle Flag is <strong>active</strong></h1>
{% else %}
    <h1>This is my base headline: Waffle Flag is <strong>inactive</strong></h1>
{% endflag %}
    <a href="{% url 'ab_test:target' %}">Click here to see the target page</a>
{% endblock %}

Where "headline-flag" is the name of the flag you defined in the admin page.

Performance Tracking

The bandits will automatically select a winning option when the given criteria are met. When setting up a bandit, you can select the SIGNIFICANCE LEVEL(default is 0.05) and the MIN VIEWS (default is 100).

After the MIN VIEWS threshold is reached, the model will perform a two-sided t-test to determine if there's significant difference in the two options (i.e. p-value < SIGNIFICANCE LEVEL). If this criteria is met, the bandit will then default to the winning option for all future visits to avoid potential conversion losses from testing.

As a user, you can also view the conversion rate and the upper and lower bounds of the confidence intervals at any time within the admin page.

A view of the bandit stats

Metadata

Release files for django-bandits 0.1.6

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-bandits 0.1.6
File Size Uploaded
django_bandits-0.1.6.tar.gz 16.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-bandits 0.1.6
File Interpreter ABI Platform
django_bandits-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 34.8 kB

Release files / django_bandits-0.1.6.tar.gz

Download URL django_bandits-0.1.6.tar.gz
Size 16.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d2b11b8accfc4f6a63e69d6aa83798268e29329b748e4ee4157dad8bf41fd562
BLAKE2b-256 checksum
How to use checksums
f9bd75a8c6708851c202a069051221d5cde2f8a9fd02423515e8963e9a02107b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.8 Linux/6.2.0-31-generic

Release files / django_bandits-0.1.6-py3-none-any.whl

Download URL django_bandits-0.1.6-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6e7e797a1598eb3f587c9e0b97375c9fb44d706d930c375c2d52abccd899c674
BLAKE2b-256 checksum
How to use checksums
bfabd03ce4fd70a5fd7cb3776045f3ed062cd9d290337de6a21511ea5cf6d9d2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.10.8 Linux/6.2.0-31-generic

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

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