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.

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry πŸ•’

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more β†’

Top comments (0)

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay