Skip to content

Commit 3eb89f8

Browse files
committed
Reworked sections
1 parent 854ef75 commit 3eb89f8

File tree

1 file changed

+69
-36
lines changed

1 file changed

+69
-36
lines changed

articles/azure-app-configuration/howto-variant-feature-flags-python.md

Lines changed: 69 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ In this tutorial, you use a variant feature flag to manage experiences for diffe
2020
* Python 3.8 or later - for information on setting up Python on Windows, see the [Python on Windows documentation](/windows/python/)
2121
* Follow the [Use variant feature flags](./use-variant-feature-flags.md) tutorial and create the variant feature flag named *Greeting*.
2222

23-
## Create a Python web app
23+
## Set up a Python Flask web app
24+
25+
If you already have a Python Flask web app, you can skip [Use the variant feature flag](#use-the-variant-feature-flag) section.
2426

2527
1. Create a new project folder named *QuoteOfTheDay*.
2628

@@ -43,19 +45,13 @@ In this tutorial, you use a variant feature flag to manage experiences for diffe
4345
pip install flask-login
4446
pip install flask_sqlalchemy
4547
pip install flask_bcrypt
46-
pip install azure-appconfiguration-provider
47-
pip install azure-identity featuremanagement[AzureMonitor]
4848
```
4949

50-
1. Create a new file named *app.py* in the *QuoteOfTheDay* folder.
50+
## Create the Quote of the Day app
5151

52-
You use the `DefaultAzureCredential` to authenticate to your App Configuration store. Follow the [instructions](./concept-enable-rbac.md#authentication-with-token-credentials) to assign your credential the **App Configuration Data Reader** role. Be sure to allow sufficient time for the permission to propagate before running your application.
52+
1. Create a new file named *app.py* in the *QuoteOfTheDay* folder.
5353

5454
```python
55-
import os
56-
from azure.appconfiguration.provider import load
57-
from featuremanagement import FeatureManager
58-
from azure.identity import DefaultAzureCredential
5955
from flask_bcrypt import Bcrypt
6056
6157
from flask_sqlalchemy import SQLAlchemy
@@ -66,22 +62,6 @@ In this tutorial, you use a variant feature flag to manage experiences for diffe
6662
app = Flask(__name__, template_folder="../templates", static_folder="../static")
6763
bcrypt = Bcrypt(app)
6864
69-
ENDPOINT = os.getenv("AzureAppConfigurationEndpoint")
70-
71-
def callback():
72-
app.config.update(azure_app_config)
73-
74-
global azure_app_config
75-
azure_app_config = load(
76-
endpoint=ENDPOINT,
77-
credential=DefaultAzureCredential(),
78-
on_refresh_success=callback,
79-
feature_flag_enabled=True,
80-
feature_flag_refresh_enabled=True,
81-
)
82-
app.config.update(azure_app_config)
83-
feature_manager = FeatureManager(azure_app_config)
84-
8565
db = SQLAlchemy()
8666
db.init_app(app)
8767
@@ -133,19 +113,15 @@ In this tutorial, you use a variant feature flag to manage experiences for diffe
133113
```python
134114
import random
135115
136-
from featuremanagement.azuremonitor import track_event
137116
from flask import Blueprint, render_template, request, flash, redirect, url_for
138117
from flask_login import current_user, login_user, logout_user
139-
from . import azure_app_config, feature_manager, db, bcrypt
118+
from . import db, bcrypt
140119
from .model import Quote, Users
141120
142121
bp = Blueprint("pages", __name__)
143122
144123
@bp.route("/", methods=["GET", "POST"])
145124
def index():
146-
global azure_app_config
147-
# Refresh the configuration from App Configuration service.
148-
azure_app_config.refresh()
149125
context = {}
150126
user = ""
151127
if current_user.is_authenticated:
@@ -154,17 +130,13 @@ In this tutorial, you use a variant feature flag to manage experiences for diffe
154130
else:
155131
context["user"] = "Guest"
156132
if request.method == "POST":
157-
track_event("Liked", user)
158133
return redirect(url_for("pages.index"))
159134
160135
quotes = [
161136
Quote("You cannot change what you are, only what you do.", "Philip Pullman"),
162137
]
163138
164-
greeting = feature_manager.get_variant("Greeting", user)
165-
greeting_message = ""
166-
if greeting:
167-
greeting_message = greeting.configuration
139+
greeting_message = "Hi"
168140
169141
context["model"] = {}
170142
context["model"]["greeting_message"] = greeting_message
@@ -440,7 +412,68 @@ In this tutorial, you use a variant feature flag to manage experiences for diffe
440412
}
441413
```
442414
443-
### Build and run the app
415+
## Use the variant feature flag
416+
417+
1. Install the latest versions of the following packages.
418+
419+
```bash
420+
pip install azure-identity
421+
pip install azure-appconfiguration-provider
422+
pip install featuremanagement[AzureMonitor]
423+
```
424+
425+
1. Open `app.py` to connect to App Configuration and set up feature management.
426+
427+
You use the `DefaultAzureCredential` to authenticate to your App Configuration store. Follow the [instructions](./concept-enable-rbac.md#authentication-with-token-credentials) to assign your credential the **App Configuration Data Reader** role. Be sure to allow sufficient time for the permission to propagate before running your application.
428+
429+
```python
430+
import os
431+
from azure.appconfiguration.provider import load
432+
from featuremanagement import FeatureManager
433+
from azure.identity import DefaultAzureCredential
434+
435+
ENDPOINT = os.getenv("AzureAppConfigurationEndpoint")
436+
437+
# Updates the flask app configuration with the Azure App Configuration settings whenever a refresh happens
438+
def callback():
439+
app.config.update(azure_app_config)
440+
441+
# Connect to App Configuration
442+
global azure_app_config
443+
azure_app_config = load(
444+
endpoint=ENDPOINT,
445+
credential=DefaultAzureCredential(),
446+
on_refresh_success=callback,
447+
feature_flag_enabled=True,
448+
feature_flag_refresh_enabled=True,
449+
)
450+
app.config.update(azure_app_config)
451+
452+
# Create a FeatureManager
453+
feature_manager = FeatureManager(azure_app_config)
454+
```
455+
456+
1. Open `routes.py` to refresh configuration and get the feature variant.
457+
458+
```python
459+
from featuremanagement.azuremonitor import track_event
460+
from . import azure_app_config, feature_manager
461+
462+
...
463+
# Update the post request to track liked events
464+
if request.method == "POST":
465+
track_event("Liked", user)
466+
return redirect(url_for("pages.index"))
467+
468+
...
469+
# Update greeting_message to variant
470+
greeting = feature_manager.get_variant("Greeting", user)
471+
greeting_message = ""
472+
if greeting:
473+
greeting_message = greeting.configuration
474+
```
475+
476+
## Build and run the app
444477
445478
1. Set an environment variable. Set the environment variable named **AzureAppConfigurationEndpoint** to the endpoint of your App Configuration store found under the *Overview* of your store in the Azure portal.
446479

0 commit comments

Comments
 (0)