Implement Feature Flags (Python)
The Python server-side SDK is currently in Beta.
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.0b2
. If not, please follow this doc to install the SDK. - You have your Project Token from your Mixpanel Project Settings
Flag Evaluation Modes
There are two modes 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.
Local Evaluation
Targeting by Mixpanel cohorts and sticky variants are not supported in Local Evaluation mode.
-
The SDK is configured with a
LocalFlagsConfig
object 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.com
respectively.enable_polling
- This should be set toTrue
to 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"
}
}
variant_value = mixpanel.local_flags.get_variant_value(flag_key, fallback_variant, user_context)
Remote Evaluation
- The SDK is configured with a
RemoteFlagsConfig
object 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) as mp:
# 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 = mp.remote_flags.get_variant_value(flag_key, fallback_variant, user_context)
print(f"Variant value: {variant_value}")
Was this page useful?