Skip to content

Commit 9e480ed

Browse files
committed
Add Microsoft Entra ID tab to Python quickstart
1 parent c127bd8 commit 9e480ed

File tree

1 file changed

+103
-55
lines changed

1 file changed

+103
-55
lines changed

articles/azure-app-configuration/quickstart-python.md

Lines changed: 103 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.service: azure-app-configuration
77
ms.devlang: python
88
ms.topic: sample
99
ms.custom: devx-track-python, mode-other, engagement-fy23, py-fresh-zinc
10-
ms.date: 11/20/2023
10+
ms.date: 02/03/2025
1111
ms.author: malev
1212
#Customer intent: As a Python developer, I want to use the Azure SDK for Python to access my data in Azure App Configuration.
1313
---
@@ -70,75 +70,52 @@ Add the following key-value to the App Configuration store and leave **Label** a
7070
> [!NOTE]
7171
> The code snippets in this example will help you get started with the App Configuration client library for Python. For your application, you should also consider handling exceptions according to your needs. To learn more about exception handling, please refer to our [Python SDK documentation](https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/appconfiguration/azure-appconfiguration).
7272

73-
## Configure your App Configuration connection string
73+
## Configure an environment variable.
7474

75-
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:
75+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
7676

77-
### [Windows command prompt](#tab/windowscommandprompt)
77+
Set the environment variable named **AZURE_APPCONFIG_ENDPOINT** to the endpoint of your App Configuration store found under the *Overview* of your store in the Azure portal.
7878

79-
To 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:
79+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
8080

81-
```cmd
82-
setx AZURE_APPCONFIG_CONNECTION_STRING "<connection-string-of-your-app-configuration-store>"
83-
```
84-
85-
### [PowerShell](#tab/powershell)
86-
87-
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:
88-
89-
```azurepowershell
90-
$Env:AZURE_APPCONFIG_CONNECTION_STRING = "<app-configuration-store-connection-string>"
91-
```
92-
93-
### [macOS](#tab/unix)
94-
95-
If you use macOS, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
96-
97-
```console
98-
export AZURE_APPCONFIG_CONNECTION_STRING='<app-configuration-store-connection-string>'
99-
```
100-
101-
### [Linux](#tab/linux)
102-
103-
If you use Linux, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
104-
105-
```console
106-
export AZURE_APPCONFIG_CONNECTION_STRING='<app-configuration-store-connection-string>'
107-
```
108-
109-
1. Print out the value of the environment variable to validate that it is set properly with the command below.
81+
```cmd
82+
setx AZURE_APPCONFIG_ENDPOINT "endpoint-of-your-app-configuration-store"
83+
```
11084

111-
### [Windows command prompt](#tab/windowscommandprompt)
85+
If you use PowerShell, run the following command:
11286

113-
Using the Windows command prompt, restart the command prompt to allow the change to take effect and run the following command:
87+
```powershell
88+
$Env:AZURE_APPCONFIG_ENDPOINT = "endpoint-of-your-app-configuration-store"
89+
```
11490

115-
```cmd
116-
echo %AZURE_APPCONFIG_CONNECTION_STRING%
117-
```
91+
If you use macOS or Linux, run the following command:
11892

119-
### [PowerShell](#tab/powershell)
93+
```bash
94+
export AZURE_APPCONFIG_ENDPOINT='<endpoint-of-your-app-configuration-store>'
95+
```
12096

121-
If you use Windows PowerShell, run the following command:
97+
### [Connection string](#tab/connection-string)
12298

123-
```azurepowershell
124-
$Env:AZURE_APPCONFIG_CONNECTION_STRING
125-
```
99+
Set the environment variable named **AZURE_APPCONFIG_CONNECTION_STRING** to the read-only connection string of your App Configuration store found under *Access keys* of your store in the Azure portal.
126100

127-
### [macOS](#tab/unix)
101+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
128102

129-
If you use macOS, run the following command:
103+
```cmd
104+
setx AZURE_APPCONFIG_CONNECTION_STRING "<connection-string-of-your-app-configuration-store>"
105+
```
130106

131-
```console
132-
echo "$AZURE_APPCONFIG_CONNECTION_STRING"
133-
```
107+
If you use PowerShell, run the following command:
134108

135-
### [Linux](#tab/linux)
109+
```powershell
110+
$Env:AZURE_APPCONFIG_CONNECTION_STRING = "connection-string-of-your-app-configuration-store"
111+
```
136112

137-
If you use Linux, run the following command:
113+
If you use macOS or Linux, run the following command:
138114

139-
```console
140-
echo "$AZURE_APPCONFIG_CONNECTION_STRING"
141-
```
115+
```bash
116+
export AZURE_APPCONFIG_CONNECTION_STRING='<connection-string-of-your-app-configuration-store>'
117+
```
118+
---
142119

143120
## Code samples
144121

@@ -160,12 +137,27 @@ Learn below how to:
160137

161138
### Connect to an App Configuration store
162139

163-
The following code snippet creates an instance of **AzureAppConfigurationClient** using the connection string stored in your environment variables.
140+
The following code snippet creates an instance of **AzureAppConfigurationClient** using the environment variable you created in the step above.
141+
142+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
143+
144+
```python
145+
from azure.identity import DefaultAzureCredential
146+
from azure.appconfiguration import AzureAppConfigurationClient
147+
148+
credential = DefaultAzureCredential()
149+
150+
endpoint = os.getenv('AZURE_APPCONFIG_ENDPOINT')
151+
client = AzureAppConfigurationClient(base_url=endpoint, credential=credential)
152+
```
153+
154+
### [Connection string](#tab/connection-string)
164155

165156
```python
166157
connection_string = os.getenv('AZURE_APPCONFIG_CONNECTION_STRING')
167158
app_config_client = AzureAppConfigurationClient.from_connection_string(connection_string)
168159
```
160+
---
169161

170162
### Get a configuration setting
171163

@@ -249,6 +241,60 @@ In this example, you created a Python app that uses the Azure App Configuration
249241

250242
At this point, your *app-configuration-example.py* file should have the following code:
251243

244+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
245+
246+
```python
247+
import os
248+
from azure.identity import DefaultAzureCredential
249+
from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting
250+
251+
try:
252+
print("Azure App Configuration - Python example")
253+
# Example code goes here
254+
255+
credential = DefaultAzureCredential()
256+
endpoint = os.getenv('AZURE_APPCONFIG_ENDPOINT')
257+
app_config_client = AzureAppConfigurationClient(base_url=endpoint, credential=credential)
258+
259+
retrieved_config_setting = app_config_client.get_configuration_setting(key='TestApp:Settings:Message')
260+
print("\nRetrieved configuration setting:")
261+
print("Key: " + retrieved_config_setting.key + ", Value: " + retrieved_config_setting.value)
262+
263+
config_setting = ConfigurationSetting(
264+
key='TestApp:Settings:NewSetting',
265+
value='New setting value'
266+
)
267+
added_config_setting = app_config_client.add_configuration_setting(config_setting)
268+
print("\nAdded configuration setting:")
269+
print("Key: " + added_config_setting.key + ", Value: " + added_config_setting.value)
270+
271+
filtered_settings_list = app_config_client.list_configuration_settings(key_filter="TestApp*")
272+
print("\nRetrieved list of configuration settings:")
273+
for item in filtered_settings_list:
274+
print("Key: " + item.key + ", Value: " + item.value)
275+
276+
locked_config_setting = app_config_client.set_read_only(added_config_setting, read_only=True)
277+
print("\nRead-only status for " + locked_config_setting.key + ": " + str(locked_config_setting.read_only))
278+
279+
unlocked_config_setting = app_config_client.set_read_only(locked_config_setting, read_only=False)
280+
print("\nRead-only status for " + unlocked_config_setting.key + ": " + str(unlocked_config_setting.read_only))
281+
282+
added_config_setting.value = "Value has been updated!"
283+
updated_config_setting = app_config_client.set_configuration_setting(added_config_setting)
284+
print("\nUpdated configuration setting:")
285+
print("Key: " + updated_config_setting.key + ", Value: " + updated_config_setting.value)
286+
287+
deleted_config_setting = app_config_client.delete_configuration_setting(key="TestApp:Settings:NewSetting")
288+
print("\nDeleted configuration setting:")
289+
print("Key: " + deleted_config_setting.key + ", Value: " + deleted_config_setting.value)
290+
291+
except Exception as ex:
292+
print('Exception:')
293+
print(ex)
294+
```
295+
296+
### [Connection string](#tab/connection-string)
297+
252298
```python
253299
import os
254300
from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting
@@ -297,6 +343,8 @@ except Exception as ex:
297343
print(ex)
298344
```
299345

346+
---
347+
300348
In your console window, navigate to the directory containing the *app-configuration-example.py* file and execute the following Python command to run the app:
301349

302350
```console

0 commit comments

Comments
 (0)