|
| 1 | +--- |
| 2 | +title: Quickstart for using Azure App Configuration with Python apps using the Python provider | Microsoft Docs |
| 3 | +description: In this quickstart, create a Python app with the Azure App Configuration Python provider to centralize storage and management of application settings separate from your code. |
| 4 | +services: azure-app-configuration |
| 5 | +author: maud-lv |
| 6 | +ms.service: azure-app-configuration |
| 7 | +ms.devlang: python |
| 8 | +ms.topic: quickstart |
| 9 | +ms.custom: devx-track-python, mode-other |
| 10 | +ms.date: 10/31/2022 |
| 11 | +ms.author: malev |
| 12 | +#Customer intent: As a Python developer, I want to manage all my app settings in one place. |
| 13 | +--- |
| 14 | +# Quickstart: Create a Python app with the Azure App Configuration Python provider |
| 15 | + |
| 16 | +In this quickstart, you will use the Python provider for Azure App Configuration to centralize storage and management of application settings using the [Azure App Configuration Python provider client library](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/appconfiguration/azure-appconfiguration-provider). |
| 17 | + |
| 18 | +The Python App Configuration provider is a library running on top of the Azure SDK for Python, helping Python developers easily consume the App Configuration service. It enables configuration settings to be used like a dictionary. |
| 19 | + |
| 20 | +## Prerequisites |
| 21 | + |
| 22 | +- Azure subscription - [create one for free](https://azure.microsoft.com/free/) |
| 23 | +- Python 3.6 or later - for information on setting up Python on Windows, see the [Python on Windows documentation](/windows/python/) |
| 24 | + |
| 25 | +## Create an App Configuration store |
| 26 | + |
| 27 | +[!INCLUDE [azure-app-configuration-create](../../includes/azure-app-configuration-create.md)] |
| 28 | + |
| 29 | +9. Select **Configuration Explorer** > **Create** > **Key-value** to add the following key-value pairs: |
| 30 | + |
| 31 | + | Key | Value | Label | Content type | |
| 32 | + |----------------|-------------------|-------------|--------------------| |
| 33 | + | *message* | *Hello* | Leave empty | Leave empty | |
| 34 | + | *test.message* | *Hello test* | Leave empty | Leave empty | |
| 35 | + | *my_json* | *{"key":"value"}* | Leave empty | *application/json* | |
| 36 | + |
| 37 | +10. Select **Apply**. |
| 38 | + |
| 39 | +## Set up the Python app |
| 40 | + |
| 41 | +1. Create a new directory for the project named *app-configuration-quickstart*. |
| 42 | + |
| 43 | + ```console |
| 44 | + mkdir app-configuration-quickstart |
| 45 | + ``` |
| 46 | + |
| 47 | +1. Switch to the newly created *app-configuration-quickstart* directory. |
| 48 | + |
| 49 | + ```console |
| 50 | + cd app-configuration-quickstart |
| 51 | + ``` |
| 52 | + |
| 53 | +1. Install the Azure App Configuration provider by using the `pip install` command. |
| 54 | + |
| 55 | + ```console |
| 56 | + pip install azure-appconfiguration-provider |
| 57 | + ``` |
| 58 | + |
| 59 | +1. Create a new file called *app-configuration-quickstart.py* in the *app-configuration-quickstart* directory and add the following code: |
| 60 | + |
| 61 | + ```python |
| 62 | + from azure.appconfiguration.provider import ( |
| 63 | + AzureAppConfigurationProvider, |
| 64 | + SettingSelector |
| 65 | + ) |
| 66 | + import os |
| 67 | + |
| 68 | + connection_string = os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING") |
| 69 | + |
| 70 | + # Connect to Azure App Configuration using a connection string. |
| 71 | + config = AzureAppConfigurationProvider.load( |
| 72 | + connection_string=connection_string) |
| 73 | + |
| 74 | + # Find the key "message" and print its value. |
| 75 | + print(config["message"]) |
| 76 | + # Find the key "my_json" and print the value for "key" from the dictionary. |
| 77 | + print(config["my_json"]["key"]) |
| 78 | + |
| 79 | + # Connect to Azure App Configuration using a connection string and trimmed key prefixes. |
| 80 | + trimmed = {"test."} |
| 81 | + config = AzureAppConfigurationProvider.load( |
| 82 | + connection_string=connection_string, trimmed_key_prefixes=trimmed) |
| 83 | + # From the keys with trimmed prefixes, find a key with "message" and print its value. |
| 84 | + print(config["message"]) |
| 85 | + |
| 86 | + # Connect to Azure App Configuration using SettingSelector. |
| 87 | + selects = {SettingSelector("message*", "\0")} |
| 88 | + config = AzureAppConfigurationProvider.load( |
| 89 | + connection_string=connection_string, selects=selects) |
| 90 | + |
| 91 | + # Print True or False to indicate if "message" is found in Azure App Configuration. |
| 92 | + print("message found: " + str("message" in config)) |
| 93 | + print("test.message found: " + str("test.message" in config)) |
| 94 | + ``` |
| 95 | + |
| 96 | +## Configure your App Configuration connection string |
| 97 | + |
| 98 | +1. Set an environment variable named **AZURE_APPCONFIG_CONNECTION_STRING**, and set it to the connection string of your App Configuration store. At the command line, run the following command: |
| 99 | + |
| 100 | + ### [Windows command prompt](#tab/windowscommandprompt) |
| 101 | + |
| 102 | + To build and run the app locally using the Windows command prompt, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store: |
| 103 | + |
| 104 | + ```cmd |
| 105 | + setx AZURE_APPCONFIG_CONNECTION_STRING "connection-string-of-your-app-configuration-store" |
| 106 | + ``` |
| 107 | + |
| 108 | + ### [PowerShell](#tab/powershell) |
| 109 | + |
| 110 | + If you use Windows PowerShell, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store: |
| 111 | + |
| 112 | + ```azurepowershell |
| 113 | + $Env:AZURE_APPCONFIG_CONNECTION_STRING = "<app-configuration-store-connection-string>" |
| 114 | + ``` |
| 115 | + |
| 116 | + ### [macOS](#tab/unix) |
| 117 | + |
| 118 | + If you use macOS, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store: |
| 119 | + |
| 120 | + ```console |
| 121 | + export AZURE_APPCONFIG_CONNECTION_STRING='<app-configuration-store-connection-string>' |
| 122 | + ``` |
| 123 | + |
| 124 | + ### [Linux](#tab/linux) |
| 125 | + |
| 126 | + If you use Linux, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store: |
| 127 | + |
| 128 | + ```console |
| 129 | + export AZURE_APPCONFIG_CONNECTION_STRING='<app-configuration-store-connection-string>' |
| 130 | + ``` |
| 131 | + |
| 132 | +1. Restart the command prompt to allow the change to take effect. Print out the value of the environment variable to validate that it is set properly with the command below. |
| 133 | + |
| 134 | + ### [Windows command prompt](#tab/windowscommandprompt) |
| 135 | + |
| 136 | + Using the Windows command prompt, run the following command: |
| 137 | + |
| 138 | + ```cmd |
| 139 | + printenv AZURE_APPCONFIG_CONNECTION_STRING |
| 140 | + ``` |
| 141 | +
|
| 142 | + ### [PowerShell](#tab/powershell) |
| 143 | +
|
| 144 | + If you use Windows PowerShell, run the following command: |
| 145 | +
|
| 146 | + ```azurepowershell |
| 147 | + $Env:AZURE_APPCONFIG_CONNECTION_STRING |
| 148 | + ``` |
| 149 | +
|
| 150 | + ### [macOS](#tab/unix) |
| 151 | +
|
| 152 | + If you use macOS, run the following command: |
| 153 | +
|
| 154 | + ```console |
| 155 | + echo "$AZURE_APPCONFIG_CONNECTION_STRING" |
| 156 | + ``` |
| 157 | +
|
| 158 | + ### [Linux](#tab/linux) |
| 159 | +
|
| 160 | + If you use Linux, run the following command: |
| 161 | +
|
| 162 | + ```console |
| 163 | + echo "$AZURE_APPCONFIG_CONNECTION_STRING" |
| 164 | +
|
| 165 | +1. After the build successfully completes, run the following command to run the app locally: |
| 166 | +
|
| 167 | + ```python |
| 168 | + python app-configuration-quickstart.py |
| 169 | + ``` |
| 170 | +
|
| 171 | + You should see the following output: |
| 172 | +
|
| 173 | + ```Output |
| 174 | + Hello |
| 175 | + value |
| 176 | + Hello test |
| 177 | + message found: True |
| 178 | + test.message found: False |
| 179 | + ``` |
| 180 | +
|
| 181 | +## Clean up resources |
| 182 | +
|
| 183 | +[!INCLUDE [azure-app-configuration-cleanup](../../includes/azure-app-configuration-cleanup.md)] |
| 184 | +
|
| 185 | +## Next steps |
| 186 | +
|
| 187 | +In this quickstart, you created a new App Configuration store and learned how to access key-values from a Python app. |
| 188 | +
|
| 189 | +For additional code samples, visit: |
| 190 | +
|
| 191 | +> [!div class="nextstepaction"] |
| 192 | +> [Azure App Configuration Python provider](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/appconfiguration/azure-appconfiguration-provider) |
0 commit comments