|
| 1 | +--- |
| 2 | +title: Enable conditional features with a custom filter in a Python application |
| 3 | +titleSuffix: Azure App Configuration |
| 4 | +description: Learn how to implement a custom feature filter to enable conditional feature flags for your Python application. |
| 5 | +ms.service: azure-app-configuration |
| 6 | +ms.devlang: python |
| 7 | +ms.custom: devx-track-python |
| 8 | +author: mrm9084 |
| 9 | +ms.author: mametcal |
| 10 | +ms.topic: how-to |
| 11 | +ms.date: 06/05/2024 |
| 12 | +--- |
| 13 | + |
| 14 | +# Tutorial: Enable conditional features with a custom filter in a Python application |
| 15 | + |
| 16 | +Feature flags can use feature filters to enable features conditionally. To learn more about feature filters, see [Tutorial: Enable conditional features with feature filters](./howto-feature-filters.md). |
| 17 | + |
| 18 | +The example used in this tutorial is based on the Python application introduced in the feature management [quickstart](./quickstart-feature-flag-python.md). 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](./howto-feature-filters.md) to the *Beta* feature flag in your App Configuration store. |
| 19 | + |
| 20 | +In this tutorial, you'll learn how to implement a custom feature filter and use the feature filter to enable features conditionally. |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +- Create a [Python app with a feature flag](./quickstart-feature-flag-python.md). |
| 25 | +- [Add a custom feature filter to the feature flag](./howto-feature-filters.md) |
| 26 | + |
| 27 | +## Implement a custom feature filter |
| 28 | + |
| 29 | +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. |
| 30 | + |
| 31 | +1. Add a `RandomFilter.py` file with the following code. |
| 32 | + |
| 33 | + ```python |
| 34 | + import random |
| 35 | + from featuremanagement import FeatureFilter |
| 36 | + |
| 37 | + @FeatureFilter.alias("Random") |
| 38 | + class RandomFilter(FeatureFilter): |
| 39 | + |
| 40 | + def evaluate(self, context, **kwargs): |
| 41 | + value = context.get("parameters", {}).get("Value", 0) |
| 42 | + if value < random.randint(0, 100): |
| 43 | + return True |
| 44 | + return False |
| 45 | + ``` |
| 46 | + |
| 47 | + You added a `RandomFilter` class that implements the `FeatureFilter` abstract class from the `FeatureManagement` library. The `FeatureFilter` class has a single method named `evaluate`, which is called whenever a feature flag is evaluated. In `evaluate`, a feature filter enables a feature flag by returning `true`. |
| 48 | + |
| 49 | + You decorated a `FeatureFilter.alias` to the `RandomFilter` to give your filter an alias **Random**, which matches the filter name you set in the *Beta* feature flag in Azure App Configuration. |
| 50 | + |
| 51 | +1. Open the *app.py* file and register the `RandomFilter` when creating the `FeatureManager`. Also, modify the code to not automatically refresh and to also access the *Beta* feature flag a few times, as seen below. |
| 52 | + |
| 53 | + ```python |
| 54 | + from featuremanagement import FeatureManager |
| 55 | + from azure.appconfiguration.provider import load |
| 56 | + from azure.identity import DefaultAzureCredential |
| 57 | + import os |
| 58 | + |
| 59 | + endpoint = os.environ.get("APPCONFIGURATION_ENDPOINT_STRING") |
| 60 | + |
| 61 | + # Connect to Azure App Configuration using and Endpoint and Azure Entra ID |
| 62 | + # feature_flag_enabled makes it so that the provider will load feature flags from Azure App Configuration |
| 63 | + # feature_flag_refresh_enabled makes it so that the provider will refresh feature flags |
| 64 | + # from Azure App Configuration, when the refresh operation is triggered |
| 65 | + config = load(endpoint=endpoint, credential=DefaultAzureCredential(), feature_flag_enabled=True) |
| 66 | + |
| 67 | + feature_manager = FeatureManager(config, feature_filters=[RandomFilter()]) |
| 68 | + |
| 69 | + for i in range(0, 10): |
| 70 | + print("Beta is", feature_manager.is_enabled("Beta")) |
| 71 | + ``` |
| 72 | + |
| 73 | +## Feature filter in action |
| 74 | + |
| 75 | +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. |
| 76 | + |
| 77 | +Running the application will show that the *Beta* feature flag is sometimes enabled and sometimes not. |
| 78 | + |
| 79 | +```bash |
| 80 | +Beta is True |
| 81 | +Beta is False |
| 82 | +Beta is True |
| 83 | +Beta is True |
| 84 | +Beta is True |
| 85 | +Beta is False |
| 86 | +Beta is False |
| 87 | +Beta is False |
| 88 | +Beta is True |
| 89 | +Beta is True |
| 90 | +``` |
| 91 | + |
| 92 | +## Next steps |
| 93 | + |
| 94 | +To learn more about the built-in feature filters, continue to the following tutorials. |
| 95 | + |
| 96 | +> [!div class="nextstepaction"] |
| 97 | +> [Enable features on a schedule](./howto-timewindow-filter.md) |
| 98 | +
|
| 99 | +> [!div class="nextstepaction"] |
| 100 | +> [Roll out features to targeted audience](./howto-targetingfilter.md) |
0 commit comments