Tutorial: Use dynamic configuration in Python

The Azure App Configuration Python provider includes built-in caching and refreshing capabilities. This tutorial shows how to enable dynamic configuration in Python applications.

Prerequisites

Add key-values

Add the following key-value to your Azure App Configuration store. For more information about how to add key-values to a store using the Azure portal or the CLI, go to Create a key-value.

Key Value Label Content type
message Hello World! Leave empty Leave empty
sentinel 1 Leave empty Leave empty

Note

A sentinel key is a key that you update after you complete the change of all other keys. Your app monitors the sentinel key. When a change is detected, your app refreshes all configuration values. This approach helps to ensure the consistency of configuration in your app and reduces the overall number of requests made to your Azure App Configuration store, compared to monitoring all keys for changes.

Console applications

  1. Create a new Python file named app.py and add the following code:

    from azure.appconfiguration.provider import load, WatchKey
    import os
    import time
    
    connection_string = os.environ.get("APPCONFIGURATION_CONNECTION_STRING")
    
    # Connecting to Azure App Configuration using connection string
    # Setting up to refresh when the Sentinel key is changed.
    config = load(
        connection_string=connection_string,
        refresh_on=[WatchKey("sentinel")],
        refresh_interval=10, # Default value is 30 seconds, shorted for this sample
    )
    
    print("Update the `message` in your Azure App Configuration store using Azure portal or CLI.")
    print("First, update the `message` value, and then update the `sentinel` key value.")
    
    while (True):
        # Refreshing the configuration setting
        config.refresh()
    
        # Current value of message
        print(config["message"])
    
        # Waiting before the next refresh
        time.sleep(5)
    
  2. Run your script:

    python app.py
    
  3. Verify Output:

    Update the `message` in your Azure App Configuration store using Azure portal or CLI.
    First, update the `message` value, and then update the `sentinel` key value.
    Hello World!
    
  4. Update the following key-values to the Azure App Configuration store.

    Key Value Label Content type
    message Hello World Refreshed! Leave empty Leave empty
    sentinel 2 Leave empty Leave empty
  5. Once the values have been updated the updated value will print out when the refresh interval has passed.

    Hello World Refreshed!
    

Web applications

The following example shows how to update an existing web application to use refreshable configuration values. A callback can be supplied to the on_refresh_success keyword argument of the load function. This callback will be invoked when a configuration change is detected on the server, and it can be used to update the configuration values in the application.

In app.py, set up Azure App Configuration to load your configuration values. Then update your endpoints to check for updated configuration values.

from azure.appconfiguration.provider import load, WatchKey

azure_app_config = None  # declare azure_app_config as a global variable

def on_refresh_success():
   app.config.update(azure_app_config)


global azure_app_config
azure_app_config = load(connection_string=os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING")
                        refresh_on=[WatchKey("sentinel")],
                        on_refresh_success=on_refresh_success,
                        refresh_interval=10, # Default value is 30 seconds, shortened for this sample
                    )




@app.route("/")
def index():
    global azure_app_config
    # Refresh the configuration from Azure App Configuration service.
    azure_app_config.refresh()

    # Access a configuration setting directly from within Flask configuration
    print("Request for index page received")
    context = {}
    context["message"] = app.config.get("message")
    return render_template("index.html", **context)

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

<!doctype html>
<head>
  <title>Hello Azure App Configuration - Python Flask Example</title>
</head>
<html>

<body>
  <main>
    <div>
      <h1>{{message}}</h1>
    </div>
  </main>
</body>
</html>

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

NOTE: If the refresh interval hasn't passed, then the refresh won't be attempted and returned right away.

Next steps

In this tutorial, you enabled your Python app to dynamically refresh configuration settings from Azure App Configuration. To learn how to use an Azure managed identity to streamline the access to Azure App Configuration, continue to the next tutorial.