|
| 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) |
0 commit comments