title | titleSuffix | description | ms.service | ms.devlang | ms.custom | author | ms.author | ms.topic | ms.date |
---|---|---|---|---|---|---|---|---|---|
Enable conditional features with a custom filter in a Python application |
Azure App Configuration |
Learn how to implement a custom feature filter to enable conditional feature flags for your Python application. |
azure-app-configuration |
python |
devx-track-python |
mrm9084 |
mametcal |
how-to |
06/05/2024 |
Feature flags can use feature filters to enable features conditionally. To learn more about feature filters, see Tutorial: Enable conditional features with feature filters.
The example used in this tutorial is based on the Python application introduced in the feature management quickstart. Before proceeding further, complete the quickstart to create a Python application with a Beta feature flag. Once completed, you must add a custom feature filter to the Beta feature flag in your App Configuration store.
In this tutorial, you'll learn how to implement a custom feature filter and use the feature filter to enable features conditionally.
You've added a custom feature filter named Random with a Percentage parameter for your Beta feature flag in the prerequisites. Next, you implement the feature filter to enable the Beta feature flag based on the chance defined by the Percentage parameter.
-
Add a
RandomFilter.py
file with the following code.import random from featuremanagement import FeatureFilter @FeatureFilter.alias("Random") class RandomFilter(FeatureFilter): def evaluate(self, context, **kwargs): value = context.get("parameters", {}).get("Value", 0) if value < random.randint(0, 100): return True return False
You added a
RandomFilter
class that implements theFeatureFilter
abstract class from theFeatureManagement
library. TheFeatureFilter
class has a single method namedevaluate
, which is called whenever a feature flag is evaluated. Inevaluate
, a feature filter enables a feature flag by returningtrue
.You decorated a
FeatureFilter.alias
to theRandomFilter
to give your filter an alias Random, which matches the filter name you set in the Beta feature flag in Azure App Configuration. -
Open the app.py file and register the
RandomFilter
when creating theFeatureManager
. Also, modify the code to not automatically refresh and to also access the Beta feature flag a few times, as seen below.from featuremanagement import FeatureManager from azure.appconfiguration.provider import load from azure.identity import DefaultAzureCredential import os endpoint = os.environ.get("APPCONFIGURATION_ENDPOINT_STRING") # Connect to Azure App Configuration using and Endpoint and Azure Entra ID # feature_flag_enabled makes it so that the provider will load feature flags from Azure App Configuration # feature_flag_refresh_enabled makes it so that the provider will refresh feature flags # from Azure App Configuration, when the refresh operation is triggered config = load(endpoint=endpoint, credential=DefaultAzureCredential(), feature_flag_enabled=True) feature_manager = FeatureManager(config, feature_filters=[RandomFilter()]) for i in range(0, 10): print("Beta is", feature_manager.is_enabled("Beta"))
When you run the application the configuration provider will load the Beta feature flag from Azure App Configuration. The result of the is_enabled("Beta")
method will be printed to the console. As the RandomFilter
is implemented and used by the Beta feature flag, the result will be True
50 percent of the time and False
the other 50 percent of the time.
Running the application will show that the Beta feature flag is sometimes enabled and sometimes not.
Beta is True
Beta is False
Beta is True
Beta is True
Beta is True
Beta is False
Beta is False
Beta is False
Beta is True
Beta is True
To learn more about the built-in feature filters, continue to the following tutorials.
[!div class="nextstepaction"] Enable features on a schedule
[!div class="nextstepaction"] Roll out features to targeted audience