Skip to content

Files

Latest commit

 

History

History
271 lines (192 loc) · 11.9 KB

quickstart-feature-flag-python.md

File metadata and controls

271 lines (192 loc) · 11.9 KB
title description author ms.service ms.devlang ms.topic ms.date ms.author ms.custom
Quickstart for adding feature flags to Python with Azure App Configuration
Add feature flags to Python apps and manage them using Azure App Configuration.
mrm9084
azure-app-configuration
python
quickstart
07/01/2024
mametcal
devx-track-python, mode-other

Quickstart: Add feature flags to a Python app

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.

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 and the tutorial to create python apps with dynamic configuration first.

This library does not have a dependency on any Azure libraries. They seamlessly integrate with App Configuration through its Python configuration provider.

Prerequisites

Add a feature flag

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. At this stage the Enable feature flag check bock should be unchecked.

[!div class="mx-imgBorder"] Screenshot of enable feature flag named Beta.

Console applications

  1. Install Feature Management by using the pip install command.

    pip install featuremanagement
  2. Create a new python file called app.py and add the following code:

    from featuremanagement import FeatureManager
    from azure.identity import InteractiveBrowserCredential
    from azure.appconfiguration.provider import load
    import os
    from time import sleep
    
    endpoint = os.environ["APP_CONFIGURATION_ENDPOINT"]
    
    # Connecting to Azure App Configuration using an endpoint
    # 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).
    # feature_flag_enabled makes it so that the provider will load feature flags from Azure App Configuration
    # feature_flag_refresh_enabled makes it so that the provider will refresh feature flags from Azure App Configuration, when the refresh operation is triggered
    config = load(endpoint=endpoint, credential=InteractiveBrowserCredential(), feature_flag_enabled=True, feature_flag_refresh_enabled=True)
    
    feature_manager = FeatureManager(config)
    
    # Is always false
    print("Beta is ", feature_manager.is_enabled("Beta"))
    
    while not feature_manager.is_enabled("Beta"):
        sleep(5)
        config.refresh()
    
    print("Beta is ", feature_manager.is_enabled("Beta"))

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 for more info.

  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:

    To build and run the app locally using the Windows command prompt, run the following command:

    setx APP_CONFIGURATION_ENDPOINT "endpoint-of-your-app-configuration-store"

    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.

    If you use Windows PowerShell, run the following command:

    $Env:APP_CONFIGURATION_ENDPOINT = "endpoint-of-your-app-configuration-store"
    

    If you use macOS, run the following command:

    export APP_CONFIGURATION_ENDPOINT='endpoint-of-your-app-configuration-store'

    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.

    If you use Linux, run the following command:

    export APP_CONFIGURATION_ENDPOINT='endpoint-of-your-app-configuration-store'

    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.


  2. Run the python application.

    python app.py
  3. 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.

    Key State
    Beta On
  4. After about 30s, which is the refresh interval for the provider, the application will print the following:

    Beta is True

Web applications

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 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.

In app.py, set up Azure App Configuration's load method to additionally load feature flags, along with enabling refresh of feature flags.

from featuremanagement import FeatureManager

...

global azure_app_config, feature_manager
# Connecting to Azure App Configuration using an endpoint
# 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).
# feature_flag_enabled makes it so that the provider will load feature flags from Azure App Configuration
# feature_flag_refresh_enabled makes it so that the provider will refresh feature flags from Azure App Configuration, when the refresh operation is triggered
azure_app_config = load(endpoint=endpoint, credential=InteractiveBrowserCredential(),
                        refresh_on=[WatchKey("sentinel")],
                        on_refresh_success=on_refresh_success,
                        refresh_interval=10, # Default value is 30 seconds, shortened for this sample
                        feature_flag_enabled=True,
                        feature_flag_refresh_enabled=True,
                    )
feature_manager = FeatureManager(config)

Also update your routes to check for updated feature flags.

@app.route("/")
def index():
    ...
    context["message"] = azure_app_config.get("message")
    context["beta"] = feature_manager.is_enabled("Beta")
    ...

Update your template index.html to use the new feature flags.

...

<body>
  <main>
    <div>
      <h1>{{message}}</h1>
      {% if beta %}
      <h2>Beta is enabled</h2>
      {% endif %}
    </div>
  </main>
</body>

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.

[!div class="mx-imgBorder"] Screenshot of enable feature flag beta enabled.

You can find a full sample project here.

Set up Azure App Configuration in your Django settings file, settings.py to load feature flags.

from featuremanagement import FeatureManager

...
# Connecting to Azure App Configuration using an endpoint
# 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).
# feature_flag_enabled makes it so that the provider will load feature flags from Azure App Configuration
# feature_flag_refresh_enabled makes it so that the provider will refresh feature flags from Azure App Configuration, when the refresh operation is triggered
AZURE_APPCONFIGURATION = load(
        endpoint=endpoint, credential=InteractiveBrowserCredential(),
        refresh_on=[WatchKey("sentinel")],
        on_refresh_success=on_refresh_success,
        refresh_interval=10, # Default value is 30 seconds, shortened for this sample
        feature_flag_enabled=True,
        feature_flag_refresh_enabled=True,
    )
FEATURE_MANAGER = FeatureManager(config)

You can access your feature flags to add them to the context. For example, in views.py:

def index(request):
    ...

    context = {
      "message": settings.AZURE_APPCONFIGURATION.get('message'),
      "beta": settings.FEATURE_MANAGER.is_enabled('Beta')
    }
    return render(request, 'hello_azure/index.html', context)

Update your template index.html to use the new configuration values.

<body>
  <main>
    <div>
      <h1>{{message}}</h1>
      {% if beta %}
      <h2>Beta is enabled</h2>
      {% endif %}
    </div>
  </main>
</body>

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.

[!div class="mx-imgBorder"] Screenshot of enable feature flag beta enabled.

You can find a full sample project here.


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.

When a refresh is complete all values are updated at once, so the configuration is always consistent within the object.

Clean up resources

[!INCLUDE azure-app-configuration-cleanup]

Next steps

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.