Skip to content

Commit 21eda0a

Browse files
authored
Merge pull request #267696 from mrm9084/PythonFeatureManagement
Python Feature Management - Preview
2 parents f6cd0e0 + 3f4a205 commit 21eda0a

File tree

4 files changed

+275
-0
lines changed

4 files changed

+275
-0
lines changed

articles/azure-app-configuration/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
href: quickstart-feature-flag-spring-boot.md
4444
- name: Azure Functions
4545
href: quickstart-feature-flag-azure-functions-csharp.md
46+
- name: Python app
47+
href: quickstart-feature-flag-python.md
4648
- name: Azure Kubernetes Service
4749
href: quickstart-feature-flag-azure-kubernetes-service.md
4850
- name: Service integration

articles/azure-app-configuration/index.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ landingContent:
146146
url: quickstart-feature-flag-dotnet.md
147147
- text: Spring Boot app
148148
url: quickstart-feature-flag-spring-boot.md
149+
- text: Python app
150+
url: quickstart-feature-flag-python.md
149151
- linkListType: tutorial
150152
links:
151153
- text: Use feature flags in ASP.NET Core
3.38 KB
Loading
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
---
2+
title: Quickstart for adding feature flags to Python with Azure App Configuration (Preview)
3+
description: Add feature flags to Python apps and manage them using Azure App Configuration.
4+
author: mrm9084
5+
ms.service: azure-app-configuration
6+
ms.devlang: python
7+
ms.topic: quickstart
8+
ms.date: 05/29/2024
9+
ms.author: mametcal
10+
ms.custom: devx-track-python, mode-other
11+
#Customer intent: As an Python developer, I want to use feature flags to control feature availability quickly and confidently.
12+
---
13+
14+
# Quickstart: Add feature flags to a Python app (preview)
15+
16+
In this quickstart, you'll create a feature flag in Azure App Configuration and use it to dynamically control Python apps to create an end-to-end implementation of feature management.
17+
18+
The feature management support extends the dynamic configuration feature in App Configuration. These examples in the quickstart build on thePpython apps introduced in the dynamic configuration tutorial. Before you continue, finish the [quickstart](./quickstart-python-provider.md) and the [tutorial](./enable-dynamic-configuration-python.md) to create python apps with dynamic configuration first.
19+
20+
This library does **not** have a dependency on any Azure libraries. They seamlessly integrate with App Configuration through its Python configuration provider.
21+
22+
## Prerequisites
23+
24+
- An Azure account with an active subscription. [Create one for free](https://azure.microsoft.com/free/).
25+
- An App Configuration store. [Create a store](./quickstart-azure-app-configuration-create.md#create-an-app-configuration-store).
26+
- Python 3.8 or later - for information on setting up Python on Windows, see the [Python on Windows documentation](/windows/python/), otherwise see [python downloads](https://www.python.org/downloads/).
27+
- [azure-appconfiguration-provider library](https://pypi.org/project/azure-appconfiguration-provider/) 1.2.0 or later.
28+
29+
## Add a feature flag
30+
31+
Add a feature flag called *Beta* to the App Configuration store and leave **Label** and **Description** with their default values. For more information about how to add feature flags to a store using the Azure portal or the CLI, go to [Create a feature flag](./manage-feature-flags.md#create-a-feature-flag). At this stage the Enable feature flag check bock should be unchecked.
32+
33+
> [!div class="mx-imgBorder"]
34+
> ![Screenshot of enable feature flag named Beta.](media/add-beta-feature-flag.png)
35+
36+
## Console applications
37+
38+
1. Install Feature Management by using the `pip install` command.
39+
40+
```console
41+
pip install featuremanagement
42+
```
43+
44+
1. Create a new python file called `app.py` and add the following code:
45+
46+
```python
47+
from featuremanagement import FeatureManager
48+
from azure.identity import InteractiveBrowserCredential
49+
from azure.appconfiguration.provider import load
50+
import os
51+
from time import sleep
52+
53+
endpoint = os.environ["APP_CONFIGURATION_ENDPOINT"]
54+
55+
# Connecting to Azure App Configuration using an endpoint
56+
# credential is used to authenticate the client, the InteractiveBrowserCredential is used for this sample. It will open a browser window to authenticate the user. For all credential options see [credential classes](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#credential-classes).
57+
# feature_flag_enabled makes it so that the provider will load feature flags from Azure App Configuration
58+
# feature_flag_refresh_enabled makes it so that the provider will refresh feature flags from Azure App Configuration, when the refresh operation is triggered
59+
config = load(endpoint=endpoint, credential=InteractiveBrowserCredential(), feature_flag_enabled=True, feature_flag_refresh_enabled=True)
60+
61+
feature_manager = FeatureManager(config)
62+
63+
# Is always false
64+
print("Beta is ", feature_manager.is_enabled("Beta"))
65+
66+
while not feature_manager.is_enabled("Beta"):
67+
sleep(5)
68+
config.refresh()
69+
70+
print("Beta is ", feature_manager.is_enabled("Beta"))
71+
```
72+
73+
When starting the application, a browser window will open to authenticate the user. The user must have at least the `App Configuration Data Reader` role to access the App Configuration store, see [App Configuration roles](./concept-enable-rbac.md#azure-built-in-roles-for-azure-app-configuration) for more info.
74+
75+
1. Set an environment variable named **APP_CONFIGURATION_ENDPOINT**, and set it to the endpoint to your App Configuration store. At the command line, run the following command and restart the command prompt to allow the change to take effect:
76+
77+
### [Windows command prompt](#tab/windowscommandprompt)
78+
79+
To build and run the app locally using the Windows command prompt, run the following command:
80+
81+
```console
82+
setx APP_CONFIGURATION_ENDPOINT "endpoint-of-your-app-configuration-store"
83+
```
84+
85+
Restart the command prompt to allow the change to take effect. Validate that it's set properly by printing the value of the environment variable.
86+
87+
### [PowerShell](#tab/powershell)
88+
89+
If you use Windows PowerShell, run the following command:
90+
91+
```azurepowershell
92+
$Env:APP_CONFIGURATION_ENDPOINT = "endpoint-of-your-app-configuration-store"
93+
```
94+
95+
### [macOS](#tab/unix)
96+
97+
If you use macOS, run the following command:
98+
99+
```console
100+
export APP_CONFIGURATION_ENDPOINT='endpoint-of-your-app-configuration-store'
101+
```
102+
103+
Restart the command prompt to allow the change to take effect. Validate that it's set properly by printing the value of the environment variable.
104+
105+
### [Linux](#tab/linux)
106+
107+
If you use Linux, run the following command:
108+
109+
```console
110+
export APP_CONFIGURATION_ENDPOINT='endpoint-of-your-app-configuration-store'
111+
```
112+
113+
Restart the command prompt to allow the change to take effect. Validate that it's set properly by printing the value of the environment variable.
114+
115+
---
116+
117+
1. Run the python application.
118+
119+
```shell
120+
python app.py
121+
```
122+
123+
1. In the App Configuration portal select **Feature Manager**, and change the state of the **Beta** feature flag to **On**, using the toggle in the **Enabled** column.
124+
125+
| Key | State |
126+
|---|---|
127+
| Beta | On |
128+
129+
1. After about 30s, which is the refresh interval for the provider, the application will print the following:
130+
131+
```console
132+
Beta is True
133+
```
134+
135+
## Web applications
136+
137+
The following example shows how to update an existing web application, using Azure App Configuration with dynamic refresh to also use feature flags. See [Python Dynamic Configuration](./enable-dynamic-configuration-python.md) for a more detailed example of how to use dynamic refresh for configuration values. Before continuing, make sure you have the Beta feature flag enabled in your App Configuration store.
138+
139+
### [Flask](#tab/flask)
140+
141+
In `app.py`, set up Azure App Configuration's load method to additionally load feature flags, along with enabling refresh of feature flags.
142+
143+
```python
144+
from featuremanagement import FeatureManager
145+
146+
...
147+
148+
global azure_app_config, feature_manager
149+
# Connecting to Azure App Configuration using an endpoint
150+
# credential is used to authenticate the client, the InteractiveBrowserCredential is used for this sample. It will open a browser window to authenticate the user. For all credential options see [credential classes](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#credential-classes).
151+
# feature_flag_enabled makes it so that the provider will load feature flags from Azure App Configuration
152+
# feature_flag_refresh_enabled makes it so that the provider will refresh feature flags from Azure App Configuration, when the refresh operation is triggered
153+
azure_app_config = load(endpoint=endpoint, credential=InteractiveBrowserCredential(),
154+
refresh_on=[WatchKey("sentinel")],
155+
on_refresh_success=on_refresh_success,
156+
refresh_interval=10, # Default value is 30 seconds, shortened for this sample
157+
feature_flag_enabled=True,
158+
feature_flag_refresh_enabled=True,
159+
)
160+
feature_manager = FeatureManager(config)
161+
```
162+
163+
Also update your routes to check for updated feature flags.
164+
165+
```python
166+
@app.route("/")
167+
def index():
168+
...
169+
context["message"] = azure_app_config.get("message")
170+
context["beta"] = feature_manager.is_enabled("Beta")
171+
...
172+
```
173+
174+
Update your template `index.html` to use the new feature flags.
175+
176+
```html
177+
...
178+
179+
<body>
180+
<main>
181+
<div>
182+
<h1>{{message}}</h1>
183+
{% if beta %}
184+
<h2>Beta is enabled</h2>
185+
{% endif %}
186+
</div>
187+
</main>
188+
</body>
189+
```
190+
191+
Once you have updated and run your application, you can see the feature flag in action, where the `Beta is enabled` message will appear on the page, but only if the feature flag is enabled in the App Configuration store.
192+
193+
> [!div class="mx-imgBorder"]
194+
> ![Screenshot of enable feature flag beta enabled.](media/manage-feature-flags/beta-enabled.png)
195+
196+
You can find a full sample project [here](https://github.com/Azure/AppConfiguration/tree/main/examples/Python/python-flask-webapp-sample).
197+
198+
### [Django](#tab/django)
199+
200+
Set up Azure App Configuration in your Django settings file, `settings.py` to load feature flags.
201+
202+
```python
203+
from featuremanagement import FeatureManager
204+
205+
...
206+
# Connecting to Azure App Configuration using an endpoint
207+
# credential is used to authenticate the client, the InteractiveBrowserCredential is used for this sample. It will open a browser window to authenticate the user. For all credential options see [credential classes](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/identity/azure-identity#credential-classes).
208+
# feature_flag_enabled makes it so that the provider will load feature flags from Azure App Configuration
209+
# feature_flag_refresh_enabled makes it so that the provider will refresh feature flags from Azure App Configuration, when the refresh operation is triggered
210+
AZURE_APPCONFIGURATION = load(
211+
endpoint=endpoint, credential=InteractiveBrowserCredential(),
212+
refresh_on=[WatchKey("sentinel")],
213+
on_refresh_success=on_refresh_success,
214+
refresh_interval=10, # Default value is 30 seconds, shortened for this sample
215+
feature_flag_enabled=True,
216+
feature_flag_refresh_enabled=True,
217+
)
218+
FEATURE_MANAGER = FeatureManager(config)
219+
```
220+
221+
You can access your feature flags to add them to the context. For example, in views.py:
222+
223+
```python
224+
def index(request):
225+
...
226+
227+
context = {
228+
"message": settings.AZURE_APPCONFIGURATION.get('message'),
229+
"beta": settings.FEATURE_MANAGER.is_enabled('Beta')
230+
}
231+
return render(request, 'hello_azure/index.html', context)
232+
```
233+
234+
Update your template `index.html` to use the new configuration values.
235+
236+
```html
237+
<body>
238+
<main>
239+
<div>
240+
<h1>{{message}}</h1>
241+
{% if beta %}
242+
<h2>Beta is enabled</h2>
243+
{% endif %}
244+
</div>
245+
</main>
246+
</body>
247+
```
248+
249+
Once you have updated and run your application, you can see the feature flag in action, where the `Beta is enabled` message will appear on the page, but only if the feature flag is enabled in the App Configuration store.
250+
251+
> [!div class="mx-imgBorder"]
252+
> ![Screenshot of enable feature flag beta enabled.](media/manage-feature-flags/beta-enabled.png)
253+
254+
You can find a full sample project [here](https://github.com/Azure/AppConfiguration/tree/main/examples/Python/python-django-webapp-sample).
255+
256+
---
257+
258+
Whenever these endpoints are triggered, a refresh check can be performed to ensure the latest configuration values are used. The check can return immediately if the refresh interval has yet to pass or a refresh is already in progress.
259+
260+
When a refresh is complete all values are updated at once, so the configuration is always consistent within the object.
261+
262+
## Clean up resources
263+
264+
[!INCLUDE [azure-app-configuration-cleanup](../../includes/azure-app-configuration-cleanup.md)]
265+
266+
## Next steps
267+
268+
In this quickstart, you created a new App Configuration store and used it to manage features in a Python app via the [Feature Management library](https://microsoft.github.io/FeatureManagement-Python/html/index.html).
269+
270+
- Learn more about [feature management](./concept-feature-management.md).
271+
- [Manage feature flags](./manage-feature-flags.md).

0 commit comments

Comments
 (0)