Skip to main content

Feature Flagging for Flask

Project description

flask-pancake

Feature Flagging for Flask

This library was heavily inspired by django-waffle.

Installation

flask-pancake depends on Redis and the flask-redis Python package.

$ python -m pip install flask-pancake
Successfully installed flask-pancake
from flask import Flask
from flask_pancake import FlaskPancake, Switch
from flask_redis import FlaskRedis

app = Flask(__name__)
pancake = FlaskPancake(app)
redis = FlaskRedis(app)

SWITCH_FEATURE = Switch("FEATURE", default=False)


@app.route("/")
def index():
    if SWITCH_FEATURE.is_active():
        return "Hello World!", 200
    else:
        return "Not found", 404

Alternatively, if you use a create_app() method to configure your Flask app, use pancake.init_app():

from flask import Flask
from flask_pancake import FlaskPancake

pancake = FlaskPancake()


def create_app() -> Flask:
    app = Flask(__name__)
    pancake.init_app(app)
    return app

Usage

flask-pancake provides three types of flags:

  • Switches, which are either globally active or inactive. A common use case for these are system-wide enabling or disabling of a feature. E.g. in the context of a dependency on a third party service, disabling a feature with a global switch when that service is unavailable.

  • Flags are like Switches but can be overridden per user. To make use of Flags, one needs to define a function that returns a user's unique ID or None:

    from flask import request
    from flask_pancake import FlaskPancake
    
    def get_user_id():
        return getattr(getattr(request, "user", None), "uid", None)
    
    pancake = FlaskPancake(get_user_id_func=get_user_id)
    # Or, if importing a function from somewhere isn't possible, a string based
    # approach can be used.
    # Separate the the fully qualified module path from the function with a `:`
    pancake = FlaskPancake(get_user_id_func="my.app.account.utils:get_uid")
    
  • Samples, have a global "ratio" of 0 - 100%. Each time a check is done on a sample, a random value is checked within these bounds. Hence:

    # DO THIS!
    def foo():
        is_active = MY_SAMPLE.is_active()
        if is_active:
            # do something
            pass
        ...
        if is_active:
            # do more
            pass
    
    # DO NOT DO THIS!
    def foo():
        if MY_SAMPLE.is_active():
            # do something
            pass
        ...
        if MY_SAMPLE.is_active():
            # do more
            pass
    

    In the second example, each call to is_active() will be evaluated again. Thus, the first block might be executed, but the second might not (or vice versa).

The persisted state for all three types of feature flags can be cleared, using the clear() method.

Similarly, one can change the persisted state for Flags and Switches using their disable() and enable() methods. Samples can be updated using their set(value: float) method.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask-pancake-0.1.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

flask_pancake-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file flask-pancake-0.1.0.tar.gz.

File metadata

  • Download URL: flask-pancake-0.1.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for flask-pancake-0.1.0.tar.gz
Algorithm Hash digest
SHA256 12f98783987eae5c0ebd7b92eac29ed2b0a7d86ab3739d83ece6a32ecd1ce6f8
MD5 2c65f4e70133c8feb8f09a27fbc0c82c
BLAKE2b-256 a17775e55f00a67684ef3c2e8e4201369c4c2f6f59e64d7b4b1b5ca43d64039c

See more details on using hashes here.

File details

Details for the file flask_pancake-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: flask_pancake-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.1

File hashes

Hashes for flask_pancake-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d2a5f6e95835e210be25fb98727885f4a4494380db6a41d9de0d262a550e6e2
MD5 1294c75322f97e44848fe6adb26b37f7
BLAKE2b-256 8bef1bf653f0ac82139bcbdc2509c939ba7172004410c67f553bf8b987797299

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page