Implement Feature Flags (Python)
Overview
This developer guide will assist you in configuring your server-side Python platform for Feature Flags using the Mixpanel Python SDK. Feature Flags allow you to control the rollout of your features, conduct A/B testing, and manage application behavior without deploying new code.
Prerequisites
Before implementing Feature Flags, ensure:
- You are on an Enterprise subscription plan and have the appropriate version of the SDK installed (minimum supported version is
v5.0.0. If not, please follow this doc to install the SDK. - You have your Project Token from your Mixpanel Project Settings
Flag Evaluation Scenarios
There are two scenarios available for using the python SDK for feature flagging, Local Evaluation and Remote Evaluation.
For local evaluation, the SDK will poll Mixpanel servers for feature flag configurations. Assignment of user contexts to variants will be done locally within the SDK. This mode is recommended for low latency since there is no network call made at assignment time.
For remote evaluation, the SDK will make a network call to Mixpanel servers at assignment time. This mode is recommended for use cases where you want to leverage Mixpanel cohorts for user targeting or sticky variants for persistent variant assignments.
In either case there is also the capability to evaluate all flags for a given user context at once, to avoid needing to make multiple calls to get individual flag variants for the same user. This is particularly useful for remote evaluation to avoid incurring additional network calls.
Local Evaluation
Targeting by Mixpanel cohorts and sticky variants are not supported in Local Evaluation mode.
-
The SDK is configured with a
LocalFlagsConfigobject that specifies parameters:api_host- If you project is in the EU/IN region, this should be set to route tohttps://api-eu.mixpanel.com/https://api-in.mixpanel.comrespectively.enable_polling- This should be set toTrueto enable local evaluation.poll_interval- This is the interval in seconds at which the SDK will poll Mixpanel servers for feature flag configurations.
-
The SDK will continue to poll for the lifetime of the SDK instance or until stopped.
from mixpanel import Mixpanel
local_config = mixpanel.LocalFlagsConfig(api_host="https://api.mixpanel.com", enable_polling=True, poll_interval=60)
mixpanel = Mixpanel("YOUR_PROJECT_TOKEN", local_flags_config=local_config)
# If enable_polling is set to false, this will fetch definitions only once for the lifetime of the SDK.
mixpanel.local_flags.start_polling_for_definitions()
# This should be the 'key' of the feature flag from Mixpanel's UX.
flag_key = "sample-flag"
# This is the fallback variant to return if the user context is not in a rollout group for the flag.
fallback_variant = "control"
# Current user context for evaluation.
# At minimum, this needs to include the user's distinct_id.
# If any of your feature flags use a Variant Assignment Key other than 'distinct_id', this should also include those keys for evaluation. For example, 'company_id' below
# If any of your feature flags use Runtime targeting, this should also include 'custom_properties' for evaluation
user_context = {
"distinct_id": "1234",
"company_id": "X",
"custom_properties": {
"platform": "python"
}
}
# Gets the assigned variant for the flag for the given user context.
# This will return the fallback_variant if the user context is not in an assignment group for the flag.
variant_value = mixpanel.local_flags.get_variant_value(flag_key, fallback_variant, user_context)Remote Evaluation
- The SDK is configured with a
RemoteFlagsConfigobject to use remote evaluation.
remote_config = mixpanel.RemoteFlagsConfig(api_host=API_HOST, request_timeout_in_seconds=5)
mixpanel = mixpanel.Mixpanel(PROJECT_TOKEN, remote_flags_config=remote_config)
# get_variant_value usage is the same as for local evaluation, but will make a network call to Mixpanel servers at assignment time.
variant_value = mixpanel.remote_flags.get_variant_value(flag_key, fallback_variant, user_context)Evaluate all flags at once
Below is a remote evaluation sample of evaluating all flags for a given user context at once.
user_context = {
"distinct_id": "1234",
}
remote_config = mixpanel.RemoteFlagsConfig(api_host=API_HOST, request_timeout_in_seconds=5)
mixpanel = mixpanel.Mixpanel(PROJECT_TOKEN, remote_flags_config=remote_config)
# Returns a dictionary, mapping flag keys to assigned variants ONLY for flags that the user context is in an assignment group for.
# By default, this will not track an exposure event.
variants = mixpanel.remote_flags.get_all_variants(user_context)
# Given a flag key and the selected variant for that key, manually track an exposure event for a given flag and assigned variant, after exposing the user to the variant
mixpanel.remote_flags.track_exposure_event(flagKey, selected_variant, user_context)Was this page useful?