Skip to content

Commit 21b51a1

Browse files
authored
Merge pull request #277323 from mrm9084/NewPythonFeatureManagementPages
New python feature management pages
2 parents 0462ab6 + 6626e36 commit 21b51a1

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

articles/azure-app-configuration/TOC.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@
136136
href: howto-feature-filters.md
137137
- name: ASP.NET Core
138138
href: howto-feature-filters-aspnet-core.md
139+
- name: Python
140+
href: howto-feature-filters-python.md
139141
- name: Enable features on a schedule
140142
items:
141143
- name: Overview
@@ -327,6 +329,10 @@
327329
items:
328330
- name: API reference
329331
href: https://azure.github.io/azure-sdk-for-java/springboot.html
332+
- name: Python
333+
items:
334+
- name: API reference
335+
href: https://microsoft.github.io/FeatureManagement-Python/html/index.html
330336
- name: Deployment
331337
items:
332338
- name: ARM template
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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)

articles/azure-app-configuration/howto-feature-filters.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ To learn how to implement a custom feature filter, continue to the following tut
6666
> [!div class="nextstepaction"]
6767
> [ASP.NET Core](./howto-feature-filters-aspnet-core.md)
6868
69+
> [!div class="nextstepaction"]
70+
> [Python](./howto-feature-filters-python.md)
71+
6972
To learn more about the built-in feature filters, continue to the following tutorials:
7073

7174
> [!div class="nextstepaction"]

articles/azure-app-configuration/index.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ landingContent:
170170
url: /dotnet/api/overview/azure/appconfiguration/featuremanagement
171171
- text: Java Spring feature management API reference
172172
url: https://azure.github.io/azure-sdk-for-java/springboot.html
173+
- text: Python feature management API reference
174+
url: https://microsoft.github.io/FeatureManagement-Python/html/index.html
173175

174176
- title: Client libraries and tools
175177
linkLists:

0 commit comments

Comments
 (0)