Python Feature Flag Resources/Solutions
- LaunchDarkly Python Feature Flag SDK – LaunchDarkly
- Flask Feature Flags – Rachel Sanders
- “This is a Flask extension that adds feature flagging to your applications. This lets you turn parts of your site on or off based on configuration. It’s useful for any setup where you deploy from trunk but want to hide unfinished features from your users, such as continuous integration builds. You can also extend it to do simple a/b testing or whitelisting.”
- GitHub Repo
- Disqus Gutter – Disqus
- “This repo is the client for Gargoyle 2, known as “Gutter”. It does not work with the existing Gargoyle 1 codebase. Gutter is feature switch management library. It allows users to create feature switches and setup conditions those switches will be enabled for. Once configured, switches can then be checked against inputs (requests, user objects, etc) to see if the switches are active. For a UI to configure Gutter with see the gutter-django project.”
- GitHub Repo
- Forrst – Zurb
- Discussion regarding feature flags for python
- GitHub Repo
- Flagon – Stephen Milner
LaunchDarkly Python SDK Reference
This reference guide documents all of the methods available in LaunchDarkly’s Python SDK, and explains in detail how these methods work. If you want to dig even deeper, LaunchDarkly’s SDKs are open source– head to the Python SDK GitHub repository to look under the hood.
If you haven’t taken a look at our Quickstart guide yet, we recommend starting there to see how install our SDK into your Python application.
Once the SDK is installed, you’ll want to configure the LaunchDarkly client:
Customizing your client
You can also pass custom parameters to the client by creating a custom configuration object:
Config allows you to specify a custom base URL (you probably don’t need to do this), as well as customize timeout parameters. In this example, we’ve set the connection timeout to LaunchDarkly to 1 second, and the read timeout to 2 seconds.
Feature flag targeting and rollouts are all determined by the user you pass to your
Variation calls. In our Python SDK, users are simply dictionaries. Here’s an example:
Let’s walk through this snippet. The most important attribute is the user key– in this case we’ve used the hash
"aa0ceb". The user key is the only mandatory user attribute. The key should also uniquely identify each user. You can use a primary key, an e-mail address, or a hash, as long as the same user always has the same key. We recommend using a hash if possible.
All of the other attributes (like
custom attributes) are optional. The attributes you specify will automatically appear on our dashboard, meaning that you can start segmenting and targeting users with these attributes.
key, LaunchDarkly supports the following attributes at the “top level”. Remember, all of these are optional:
ip: Must be an IP address. If you provide an IP, LaunchDarkly will use a geolocation service to automatically infer a
countryfor the user (unless you’ve already specified one).
firstName: Must be a string. If you provide a first name, you can search for users on the Users page by name.
lastName: Must be a string. If you provide a last name, you can search for users on the Users page by name.
country: Must be a string representing the country associated with the user.
avatarURL is not provided, we’ll useGravatar to try to display an avatar for the user on the Users page.
avatar: Must be an absolute URL to an avatar image for the user.
name: Must be a string. You can search for users on the User page by name
anonymous: Must be a boolean. See the section below on anonymous users for more details.
In addition to built-in attributes, you can pass us any of your own user data by passing
custom attributes, like the
groups attribute in the example above.
A note on types
Most of our built-in attributes (like names and e-mail addresses) expect string values. Custom attribute values can be strings, booleans (like True or False), numbers, or lists of strings, booleans or numbers.
If you enter a custom value on our dashboard that looks like a number or a boolean, it’ll be interpreted that way.
Custom attributes are one of the most powerful features of LaunchDarkly. They let you target users according to any data that you want to send to us– organizations, groups, account plans– anything you pass to us becomes available instantly on our dashboard.
You can also distinguish logged-in users from anonymous users in the SDK, as follows:
You will still need to generate a unique key for anonymous users– session IDs or UUIDs work best for this.
Anonymous users work just like regular users, except that they won’t appear on your Users page in LaunchDarkly. You also can’t search for anonymous users on your Features page, and you can’t search or autocomplete by anonymous user keys. This is actually a good thing– it keeps anonymous users from polluting your Users page!
variation method determines which variation of a feature flag a user receives.
variation calls take the feature flag key, an
LDUser, and a default value.
The default value will only be returned if an error is encountered– for example, if the feature flag key doesn’t exist or the user doesn’t have a key specified.
variation call will automatically create a user in LaunchDarkly if a user with that user key doesn’t exist already. There’s no need to create users ahead of time (but if you do need to, take a look at Identify).
all_flags method produces a dictionary of feature flag keys to their values for a specific user.
track method allows you to record actions your users take on your site. This lets you record events that take place on your server. In LaunchDarkly, you can tie these events to goals in A/B tests. Here’s a simple example:
You can also attach an extra dictionary containing arbitrary data to your event:
identify method creates or updates users on LaunchDarkly, making them available for targeting and autocomplete on the dashboard. In most cases, you won’t need to call
variation call will automatically create users on the dashboard for you.
identify can be useful if you want to pre-populate your dashboard before launching any features.
Secure mode hash
In some situations, you might want avoid remote calls to LaunchDarkly and fall back to default values for your feature flags. For example, if your software is both cloud-hosted and distributed to customers to run on premise, it might make sense to fall back to defaults when running on premise. You can do this by setting
offline mode in the client’s
# Initialization: ldclient.config = Config(offline = True) ldclient.sdk_key = "YOUR_SDK_KEY" ldclient.get().variation("any.feature.flag", user, False) # will always return the default value (false)
Internally, the LaunchDarkly SDK keeps an event buffer for
identify calls. These are flushed periodically in a background thread. In some situations (for example, if you’re testing out the SDK in a REPL), you may want to manually call
flush to process events immediately.
Note that the flush interval is configurable– if you need to change the interval, you can do so when configuring your client instance.
Close safely shuts down the client instance and releases all resources associated with the client. In most long-running applications, you should not have to call close.
The LaunchDarkly SDK is compatible with uWSGI. However, in uWSGI environments, the SDK requires the
enable-threads option to be set.