Skip to content

Commit 2bd817e

Browse files
Merge pull request #307261 from MicrosoftDocs/main
Auto Publish – main to live - 2025-10-22 22:00 UTC
2 parents 1040ec5 + 1db3c86 commit 2bd817e

23 files changed

Lines changed: 349 additions & 124 deletions

articles/azure-app-configuration/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@
224224
href: howto-targetingfilter-javascript.md
225225
- name: Go Gin
226226
href: howto-targetingfilter-go.md
227+
- name: Python
228+
href: how-to-targeting-filter-python.md
227229
- name: Use variant feature flags
228230
items:
229231
- name: Overview
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
---
2+
title: Roll out features to targeted audiences in a Python app
3+
titleSuffix: Azure App Configuration
4+
description: Learn how to enable staged rollout of features for targeted audiences in a Python application.
5+
ms.service: azure-app-configuration
6+
ms.devlang: python
7+
author: mrm9084
8+
ms.author: mametcal
9+
ms.topic: how-to
10+
ms.date: 10/07/2025
11+
---
12+
13+
# Roll out features to targeted audiences in a Python application
14+
15+
In this guide, you use a targeting filter to roll out a feature to targeted audiences for your Python application. For more information about this targeting filter, see [Roll out features to targeted audiences](./howto-targetingfilter.md).
16+
17+
## Prerequisites
18+
19+
- An Azure account with an active subscription. [Create one for free](https://azure.microsoft.com/free/).
20+
- An App Configuration store, as shown in the [tutorial for creating a store](./quickstart-azure-app-configuration-create.md#create-an-app-configuration-store).
21+
- A _Beta_ feature flag with targeting filter. [Create the feature flag](./howto-targetingfilter.md).
22+
- [Python 3.8 or later](https://www.python.org/downloads/).
23+
24+
## Create a web application with a feature flag
25+
26+
In this section, you create a web application that uses the [_Beta_ feature flag](./howto-targetingfilter.md) to control the access to the beta version of a web page.
27+
28+
### Set up a Python Flask project
29+
30+
1. Create a folder called `targeting-filter-tutorial` and navigate to it.
31+
32+
```bash
33+
mkdir targeting-filter-tutorial
34+
cd targeting-filter-tutorial
35+
```
36+
37+
1. Create a virtual environment and activate it.
38+
39+
```bash
40+
# For Windows
41+
python -m venv venv
42+
venv\Scripts\activate
43+
44+
# For macOS/Linux
45+
python -m venv venv
46+
source venv/bin/activate
47+
```
48+
49+
1. Install the following packages.
50+
51+
```bash
52+
pip install azure-appconfiguration-provider
53+
pip install azure-identity
54+
pip install featuremanagement
55+
pip install flask
56+
```
57+
58+
1. Create a new file named _app.py_ and add the following code.
59+
60+
```python
61+
from flask import Flask
62+
63+
app = Flask(__name__)
64+
65+
if __name__ == "__main__":
66+
app.run(debug=True)
67+
```
68+
69+
### Connect to Azure App Configuration
70+
71+
1. Update _app.py_ and add the following code.
72+
73+
```python
74+
from flask import Flask
75+
import os
76+
from azure.identity import DefaultAzureCredential
77+
from azure.appconfiguration.provider import load
78+
from featuremanagement import FeatureManager
79+
80+
app = Flask(__name__)
81+
82+
# Get the App Configuration endpoint from environment variables
83+
app_config_endpoint = os.environ.get("AZURE_APPCONFIG_ENDPOINT")
84+
85+
# Declare App Configuration and feature manager variables
86+
azure_app_config = None
87+
feature_manager = None
88+
89+
def initialize_config():
90+
global azure_app_config, feature_manager
91+
# Load feature flags from App Configuration
92+
azure_app_config = load(
93+
endpoint=app_config_endpoint,
94+
credential=DefaultAzureCredential(),
95+
feature_flag_enabled=True,
96+
feature_flag_refresh_enabled=True
97+
)
98+
99+
# Create a feature manager with the loaded configuration
100+
feature_manager = FeatureManager(azure_app_config)
101+
102+
# Flask route before the request to refresh configuration
103+
@app.before_request
104+
def refresh_config():
105+
if azure_app_config:
106+
azure_app_config.refresh()
107+
108+
if __name__ == "__main__":
109+
# Initialize configuration before starting the app
110+
initialize_config()
111+
112+
app.run(debug=True)
113+
```
114+
115+
You connect to Azure App Configuration to load feature flags, enable automatic refresh, and create a `FeatureManager` object for accessing feature flags later. The `app.before_request` decorator ensures that configuration is refreshed before each request.
116+
117+
### Use the feature flag
118+
119+
Add the following code to the _app.py_ file to create a route handler for the Flask application. The application will serve different contents based on whether the **Beta** feature flag is enabled.
120+
121+
```python
122+
@app.route("/")
123+
def home():
124+
is_beta_enabled = feature_manager.is_enabled("Beta")
125+
126+
title = "Home Page"
127+
message = "Welcome."
128+
129+
if is_beta_enabled:
130+
title = "Beta Page"
131+
message = "This is a beta page."
132+
133+
return f"""
134+
<!DOCTYPE html>
135+
<html>
136+
<head><title>{title}</title></head>
137+
<body style="display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0;">
138+
<h1 style="text-align: center; font-size: 5rem;">{message}</h1>
139+
</body>
140+
</html>
141+
"""
142+
```
143+
144+
## Enable targeting for the web application
145+
146+
A targeting context is required when evaluating features with targeting enabled. In Python, you need to create a `TargetingContext` object and pass it to the `is_enabled` method of the feature manager.
147+
148+
Update the _app.py_ file to import the `TargetingContext` class and use it in the home route:
149+
150+
```python
151+
from flask import Flask, request
152+
from featuremanagement import FeatureManager, TargetingContext
153+
154+
...
155+
156+
@app.route("/")
157+
def home():
158+
# Get targeting context from query parameters
159+
user_id = request.args.get("userId", "")
160+
groups_param = request.args.get("groups", "")
161+
groups = groups_param.split(",") if groups_param else []
162+
163+
targeting_context = TargetingContext(user_id=user_id, groups=groups)
164+
is_beta_enabled = feature_manager.is_enabled("Beta", targeting_context)
165+
166+
167+
title = "Home Page"
168+
message = "Welcome."
169+
170+
if is_beta_enabled:
171+
title = "Beta Page"
172+
message = "This is a beta page."
173+
174+
175+
return f"""
176+
<!DOCTYPE html>
177+
<html>
178+
<head><title>{title}</title></head>
179+
<body style="display: flex; justify-content: center; align-items: center; min-height: 100vh; margin: 0;">
180+
<h1 style="text-align: center; font-size: 5rem;">{message}</h1>
181+
</body>
182+
</html>
183+
"""
184+
```
185+
186+
187+
## Targeting filter in action
188+
189+
1. Set the environment variable named _AZURE_APPCONFIG_ENDPOINT_ to the endpoint of your App Configuration store found under the _Overview_ of your store in the Azure portal.
190+
191+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
192+
193+
```cmd
194+
setx AZURE_APPCONFIG_ENDPOINT "<endpoint-of-your-app-configuration-store>"
195+
```
196+
197+
If you use PowerShell, run the following command:
198+
199+
```powershell
200+
$Env:AZURE_APPCONFIG_ENDPOINT = "<endpoint-of-your-app-configuration-store>"
201+
```
202+
203+
If you use macOS or Linux, run the following command:
204+
205+
```bash
206+
export AZURE_APPCONFIG_ENDPOINT='<endpoint-of-your-app-configuration-store>'
207+
```
208+
209+
1. Run the application.
210+
211+
```bash
212+
python app.py
213+
```
214+
215+
1. Open your browser and navigate to the address displayed in your terminal (by default, http://127.0.0.1:5000). You should see the default view of the app.
216+
217+
:::image type="content" source="media/how-to-targeting-filter-python/beta-disabled.png" alt-text="Screenshot of the app, showing the default greeting message.":::
218+
219+
1. Add `userId` as a query parameter in the URL to specify the user ID. Visit `localhost:5000/?[email protected]`. You see the beta page, because `[email protected]` is specified as a targeted user.
220+
221+
:::image type="content" source="media/how-to-targeting-filter-python/beta-enabled.png" alt-text="Screenshot of the app, showing the beta page.":::
222+
223+
1. Visit `localhost:5000/?[email protected]`. You cannot see the beta page, because `[email protected]` is specified as an excluded user.
224+
225+
:::image type="content" source="media/how-to-targeting-filter-python/beta-not-targeted.png" alt-text="Screenshot of the app, showing the default content.":::
226+
227+
## Next steps
228+
229+
To learn more about feature filters, continue to the following documents.
230+
231+
> [!div class="nextstepaction"]
232+
> [Enable conditional features with feature filters](./howto-feature-filters.md)
233+
234+
> [!div class="nextstepaction"]
235+
> [Enable features on a schedule](./howto-timewindow-filter-aspnet-core.md)
236+
237+
For more information about the Python Feature Management library, continue to the following document.
238+
239+
> [!div class="nextstepaction"]
240+
> [Feature Management for Python](https://github.com/microsoft/FeatureManagement-Python)
57.5 KB
Loading
71 KB
Loading
66.2 KB
Loading

articles/azure-app-configuration/use-feature-flags-spring-boot.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ manager: zhenlan
77
ms.service: azure-app-configuration
88
ms.devlang: java
99
ms.topic: tutorial
10-
ms.date: 08/25/2025
10+
ms.date: 10/22/2025
1111
ms.author: mametcal
1212
ms.custom: mvc, devx-track-java
1313

@@ -55,7 +55,7 @@ The easiest way to connect your Spring Boot application to App Configuration is
5555
<dependency>
5656
<groupId>com.azure.spring</groupId>
5757
<artifactId>spring-cloud-azure-dependencies</artifactId>
58-
<version>5.18.0</version>
58+
<version>6.0.0</version>
5959
<type>pom</type>
6060
<scope>import</scope>
6161
</dependency>
@@ -65,20 +65,24 @@ The easiest way to connect your Spring Boot application to App Configuration is
6565

6666
## Feature flag declaration
6767

68-
Each feature flag has two parts: a name and a list of one or more filters that are used to evaluate if a feature's state is *on* (that is, when its value is `True`). A filter defines a use case for when a feature should be turned on.
68+
Each feature flag has two parts: an id and a list of one or more filters that are used to evaluate if a feature's state is *on* (that is, when its value is `True`). A filter defines a use case for when a feature should be turned on.
6969

7070
When a feature flag has multiple filters, the filter list is traversed in order until one of the filters determines the feature should be enabled. At that point, the feature flag is *on*, and any remaining filter results are skipped. If no filter indicates the feature should be enabled, the feature flag is *off*.
7171

7272
The feature manager supports *application.yml* as a configuration source for feature flags. The following example shows how to set up feature flags in a YAML file:
7373

7474
```yml
7575
feature-management:
76-
feature-a: true
77-
feature-b: false
78-
feature-c:
79-
enabled-for:
80-
-
81-
name: PercentageFilter
76+
feature_flags:
77+
- id: feature-a
78+
enabled: true
79+
- id: feature-b
80+
enabled: false
81+
- id: feature-c
82+
enabled: true
83+
conditions:
84+
client_filters:
85+
- name: PercentageFilter
8286
parameters:
8387
Value: 50
8488
```

0 commit comments

Comments
 (0)