DEV Community

Adam Gardner
Adam Gardner

Posted on

Python Feature Flags with a Flat JSON File

A while ago I posted on how to add feature flags to a nodeJS application using only a flat file + OpenFeature.

Here's how to do the same thing for Python!

Install Packages

pip install openfeature-provider-flagd
Enter fullscreen mode Exit fullscreen mode

Create flags.json

Define the feature flag (in this case a single flag called background-colour and save as flags.json:

{
    "$schema": "https://flagd.dev/schema/v0/flags.json",
    "flags": {
      "background-colour": {
        "state": "ENABLED",
        "variants": {
          "white": "#D6D4D2",
          "green": "#73A53E",
          "orange": "#FF7C00",
          "lime": "#D3D309",
          "blue": "#4AB9D9"
        },
        "defaultVariant": "green"
     }
    }
}
Enter fullscreen mode Exit fullscreen mode

The Code

Save this as app.py:

from openfeature import api
from openfeature.contrib.provider.flagd import FlagdProvider
from openfeature.contrib.provider.flagd.config import ResolverType
import time

api.set_provider(FlagdProvider(
    resolver_type=ResolverType.IN_PROCESS,
    offline_flag_source_path="flags.json",
))

client = api.get_client("my-app")

while True:

    try:
        colour = client.get_string_value(flag_key="background-colour", default_value="NA")
        print(f"The current colour is: {colour}")
    finally:
        time.sleep(1)
Enter fullscreen mode Exit fullscreen mode

Run Program

Go ahead and start the program: python app.py

This should print the Hex colour value once per second:

The current colour is: #73A53E
The current colour is: #73A53E
The current colour is: #73A53E
Enter fullscreen mode Exit fullscreen mode

Leave the program running and change the defaultVariant in flags.json from "green" to "lime" (remember to save the changes) and the program should automatically refresh:

The current colour is: #73A53E
The current colour is: #73A53E
The current colour is: #D3D309
The current colour is: #D3D309
The current colour is: #D3D309
Enter fullscreen mode Exit fullscreen mode

Summary

This method uses the in process evaluation of the open source feature flag system flagd + OpenFeature to build a feature flagging ability in Python.

Find out more about the in process evaluation engine for flagd and OpenFeature here.

Please leave your appreciation by commenting on this post!

Okay, let's go.

Happy coding ❤️

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

đź‘‹ Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay