|
| 1 | +--- |
| 2 | +title: Use dynamic configuration in Python (preview) |
| 3 | +titleSuffix: Azure App Configuration |
| 4 | +description: Learn how to dynamically update configuration data for Python |
| 5 | +services: azure-app-configuration |
| 6 | +author: mrm9084 |
| 7 | +ms.service: azure-app-configuration |
| 8 | +ms.devlang: python |
| 9 | +ms.topic: tutorial |
| 10 | +ms.date: 09/13/2023 |
| 11 | +ms.custom: devx-track-python, devx-track-extended-python |
| 12 | +ms.author: mametcal |
| 13 | +#Customer intent: As a Python developer, I want to dynamically update my app to use the latest configuration data in App Configuration. |
| 14 | +--- |
| 15 | +# Tutorial: Use dynamic configuration in Python (preview) |
| 16 | + |
| 17 | +This tutorial shows how you can enable dynamic configuration updates in Python. It builds a script to leverage the App Configuration provider library for its built-in configuration caching and refreshing capabilities. |
| 18 | + |
| 19 | +In this tutorial, you learn how to: |
| 20 | + |
| 21 | +> [!div class="checklist"] |
| 22 | +> * Set up your app to update its configuration in response to changes in an App Configuration store. |
| 23 | +
|
| 24 | +> [!NOTE] |
| 25 | +> Requires [azure-appconfiguration-provider](https://pypi.org/project/azure-appconfiguration-provider/1.1.0b1/) package version 1.1.0b1 or later. |
| 26 | +
|
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +- An Azure subscription - [create one for free](https://azure.microsoft.com/free) |
| 30 | +- We assume you already have an App Configuration store. To create one, [create an App Configuration store](quickstart-aspnet-core-app.md). |
| 31 | + |
| 32 | +## Sentinel key |
| 33 | + |
| 34 | +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 App Configuration store, compared to monitoring all keys for changes. |
| 35 | + |
| 36 | +## Reload data from App Configuration |
| 37 | + |
| 38 | +1. Create a new Python file named *app.py* and add the following code: |
| 39 | + |
| 40 | + ```python |
| 41 | + from azure.appconfiguration.provider import load, SentinelKey |
| 42 | + from azure.appconfiguration import ( |
| 43 | + AzureAppConfigurationClient, |
| 44 | + ConfigurationSetting, |
| 45 | + ) |
| 46 | + import os |
| 47 | + import time |
| 48 | + |
| 49 | + connection_string = os.environ.get("APPCONFIGURATION_CONNECTION_STRING") |
| 50 | + |
| 51 | + # Setting up a configuration setting with a known value |
| 52 | + client = AzureAppConfigurationClient.from_connection_string(connection_string) |
| 53 | + |
| 54 | + # Creating a configuration setting to be refreshed |
| 55 | + configuration_setting = ConfigurationSetting(key="message", value="Hello World!") |
| 56 | + |
| 57 | + # Creating a Sentinel key to monitor |
| 58 | + sentinel_setting = ConfigurationSetting(key="Sentinel", value="1") |
| 59 | + |
| 60 | + # Setting the configuration setting in Azure App Configuration |
| 61 | + client.set_configuration_setting(configuration_setting=configuration_setting) |
| 62 | + client.set_configuration_setting(configuration_setting=sentinel_setting) |
| 63 | + |
| 64 | + # Connecting to Azure App Configuration using connection string, and refreshing when the configuration setting message changes |
| 65 | + config = load( |
| 66 | + connection_string=connection_string, |
| 67 | + refresh_on=[SentinelKey("Sentinel")], |
| 68 | + refresh_interval=1, # Default value is 30 seconds, shorted for this sample |
| 69 | + ) |
| 70 | + |
| 71 | + # Printing the initial value |
| 72 | + print(config["message"]) |
| 73 | + print(config["Sentinel"]) |
| 74 | + |
| 75 | + # Updating the configuration setting to a new value |
| 76 | + configuration_setting.value = "Hello World Updated!" |
| 77 | + |
| 78 | + # Updating the sentinel key to a new value, only after this is changed can a refresh happen |
| 79 | + sentinel_setting.value = "2" |
| 80 | + |
| 81 | + # Setting the updated configuration setting in Azure App Configuration |
| 82 | + client.set_configuration_setting(configuration_setting=configuration_setting) |
| 83 | + client.set_configuration_setting(configuration_setting=sentinel_setting) # Should always be done last to make sure all other keys included in the refresh |
| 84 | + |
| 85 | + # Waiting for the refresh interval to pass |
| 86 | + time.sleep(2) |
| 87 | + |
| 88 | + # Refreshing the configuration setting |
| 89 | + config.refresh() |
| 90 | + |
| 91 | + # Printing the updated value |
| 92 | + print(config["message"]) |
| 93 | + print(config["Sentinel"]) |
| 94 | + ``` |
| 95 | + |
| 96 | +1. Run your script: |
| 97 | + |
| 98 | + ```cli |
| 99 | + python app.py |
| 100 | + ``` |
| 101 | + |
| 102 | +1. Verify Output: |
| 103 | + |
| 104 | +:::image type="content" source="./media/enable-dynamic-configuration-python.png" alt-text="Screenshot of the CLI, with the results; Hello World!, 1, Hello World Updated!, 2."::: |
| 105 | + |
| 106 | + |
| 107 | +## Next steps |
| 108 | + |
| 109 | +In this tutorial, you enabled your Python app to dynamically refresh configuration settings from App Configuration. To learn how to use an Azure managed identity to streamline the access to App Configuration, continue to the next tutorial. |
| 110 | + |
| 111 | +> [!div class="nextstepaction"] |
| 112 | +> [Managed identity integration](./howto-integrate-azure-managed-service-identity.md) |
0 commit comments