Skip to content

Commit e4e3b73

Browse files
committed
Apply changes from review
1 parent 9e480ed commit e4e3b73

File tree

1 file changed

+187
-185
lines changed

1 file changed

+187
-185
lines changed

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

Lines changed: 187 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -70,53 +70,6 @@ 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 an environment variable.
74-
75-
### [Microsoft Entra ID (recommended)](#tab/entra-id)
76-
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.
78-
79-
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
80-
81-
```cmd
82-
setx AZURE_APPCONFIG_ENDPOINT "endpoint-of-your-app-configuration-store"
83-
```
84-
85-
If you use PowerShell, run the following command:
86-
87-
```powershell
88-
$Env:AZURE_APPCONFIG_ENDPOINT = "endpoint-of-your-app-configuration-store"
89-
```
90-
91-
If you use macOS or Linux, run the following command:
92-
93-
```bash
94-
export AZURE_APPCONFIG_ENDPOINT='<endpoint-of-your-app-configuration-store>'
95-
```
96-
97-
### [Connection string](#tab/connection-string)
98-
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.
100-
101-
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
102-
103-
```cmd
104-
setx AZURE_APPCONFIG_CONNECTION_STRING "<connection-string-of-your-app-configuration-store>"
105-
```
106-
107-
If you use PowerShell, run the following command:
108-
109-
```powershell
110-
$Env:AZURE_APPCONFIG_CONNECTION_STRING = "connection-string-of-your-app-configuration-store"
111-
```
112-
113-
If you use macOS or Linux, run the following command:
114-
115-
```bash
116-
export AZURE_APPCONFIG_CONNECTION_STRING='<connection-string-of-your-app-configuration-store>'
117-
```
118-
---
119-
12073
## Code samples
12174

12275
The sample code snippets in this section show you how to perform common operations with the App Configuration client library for Python. Add these code snippets to the `try` block in *app-configuration-example.py* file you created earlier.
@@ -137,10 +90,12 @@ Learn below how to:
13790

13891
### Connect to an App Configuration store
13992

140-
The following code snippet creates an instance of **AzureAppConfigurationClient** using the environment variable you created in the step above.
93+
The following code snippet creates an instance of **AzureAppConfigurationClient**. You can connect to your App Configuration store using Microsoft Entra ID (recommended), or a connection string.
14194

14295
### [Microsoft Entra ID (recommended)](#tab/entra-id)
14396

97+
You use the `DefaultAzureCredential` to authenticate to your App Configuration store. Follow the [instructions](./concept-enable-rbac.md#authentication-with-token-credentials) to assign your credential the **App Configuration Data Reader** role. Be sure to allow sufficient time for the permission to propagate before running your application.
98+
14499
```python
145100
from azure.identity import DefaultAzureCredential
146101
from azure.appconfiguration import AzureAppConfigurationClient
@@ -237,145 +192,192 @@ The following code snippet deletes a configuration setting by `key` name.
237192

238193
## Run the app
239194

240-
In this example, you created a Python app that uses the Azure App Configuration client library to retrieve a configuration setting created through the Azure portal, add a new setting, retrieve a list of existing settings, lock and unlock a setting, update a setting, and finally delete a setting.
241-
242-
At this point, your *app-configuration-example.py* file should have the following code:
243-
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-
298-
```python
299-
import os
300-
from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting
301-
302-
try:
303-
print("Azure App Configuration - Python example")
304-
# Example code goes here
305-
306-
connection_string = os.getenv('AZURE_APPCONFIG_CONNECTION_STRING')
307-
app_config_client = AzureAppConfigurationClient.from_connection_string(connection_string)
308-
309-
retrieved_config_setting = app_config_client.get_configuration_setting(key='TestApp:Settings:Message')
310-
print("\nRetrieved configuration setting:")
311-
print("Key: " + retrieved_config_setting.key + ", Value: " + retrieved_config_setting.value)
312-
313-
config_setting = ConfigurationSetting(
314-
key='TestApp:Settings:NewSetting',
315-
value='New setting value'
316-
)
317-
added_config_setting = app_config_client.add_configuration_setting(config_setting)
318-
print("\nAdded configuration setting:")
319-
print("Key: " + added_config_setting.key + ", Value: " + added_config_setting.value)
320-
321-
filtered_settings_list = app_config_client.list_configuration_settings(key_filter="TestApp*")
322-
print("\nRetrieved list of configuration settings:")
323-
for item in filtered_settings_list:
324-
print("Key: " + item.key + ", Value: " + item.value)
325-
326-
locked_config_setting = app_config_client.set_read_only(added_config_setting, read_only=True)
327-
print("\nRead-only status for " + locked_config_setting.key + ": " + str(locked_config_setting.read_only))
328-
329-
unlocked_config_setting = app_config_client.set_read_only(locked_config_setting, read_only=False)
330-
print("\nRead-only status for " + unlocked_config_setting.key + ": " + str(unlocked_config_setting.read_only))
331-
332-
added_config_setting.value = "Value has been updated!"
333-
updated_config_setting = app_config_client.set_configuration_setting(added_config_setting)
334-
print("\nUpdated configuration setting:")
335-
print("Key: " + updated_config_setting.key + ", Value: " + updated_config_setting.value)
336-
337-
deleted_config_setting = app_config_client.delete_configuration_setting(key="TestApp:Settings:NewSetting")
338-
print("\nDeleted configuration setting:")
339-
print("Key: " + deleted_config_setting.key + ", Value: " + deleted_config_setting.value)
340-
341-
except Exception as ex:
342-
print('Exception:')
343-
print(ex)
344-
```
345-
346-
---
347-
348-
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:
349-
350-
```console
351-
python app-configuration-example.py
352-
```
353-
354-
You should see the following output:
355-
356-
```output
357-
Azure App Configuration - Python example
358-
359-
Retrieved configuration setting:
360-
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
361-
362-
Added configuration setting:
363-
Key: TestApp:Settings:NewSetting, Value: New setting value
364-
365-
Retrieved list of configuration settings:
366-
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
367-
Key: TestApp:Settings:NewSetting, Value: New setting value
368-
369-
Read-only status for TestApp:Settings:NewSetting: True
370-
371-
Read-only status for TestApp:Settings:NewSetting: False
195+
1. In this example, you created a Python app that uses the Azure App Configuration client library to retrieve a configuration setting created through the Azure portal, add a new setting, retrieve a list of existing settings, lock and unlock a setting, update a setting, and finally delete a setting.
196+
197+
At this point, your *app-configuration-example.py* file should have the following code:
198+
199+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
200+
201+
```python
202+
import os
203+
from azure.identity import DefaultAzureCredential
204+
from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting
205+
206+
try:
207+
print("Azure App Configuration - Python example")
208+
# Example code goes here
209+
210+
credential = DefaultAzureCredential()
211+
endpoint = os.getenv('AZURE_APPCONFIG_ENDPOINT')
212+
app_config_client = AzureAppConfigurationClient(base_url=endpoint, credential=credential)
213+
214+
retrieved_config_setting = app_config_client.get_configuration_setting(key='TestApp:Settings:Message')
215+
print("\nRetrieved configuration setting:")
216+
print("Key: " + retrieved_config_setting.key + ", Value: " + retrieved_config_setting.value)
217+
218+
config_setting = ConfigurationSetting(
219+
key='TestApp:Settings:NewSetting',
220+
value='New setting value'
221+
)
222+
added_config_setting = app_config_client.add_configuration_setting(config_setting)
223+
print("\nAdded configuration setting:")
224+
print("Key: " + added_config_setting.key + ", Value: " + added_config_setting.value)
225+
226+
filtered_settings_list = app_config_client.list_configuration_settings(key_filter="TestApp*")
227+
print("\nRetrieved list of configuration settings:")
228+
for item in filtered_settings_list:
229+
print("Key: " + item.key + ", Value: " + item.value)
230+
231+
locked_config_setting = app_config_client.set_read_only(added_config_setting, read_only=True)
232+
print("\nRead-only status for " + locked_config_setting.key + ": " + str(locked_config_setting.read_only))
233+
234+
unlocked_config_setting = app_config_client.set_read_only(locked_config_setting, read_only=False)
235+
print("\nRead-only status for " + unlocked_config_setting.key + ": " + str(unlocked_config_setting.read_only))
236+
237+
added_config_setting.value = "Value has been updated!"
238+
updated_config_setting = app_config_client.set_configuration_setting(added_config_setting)
239+
print("\nUpdated configuration setting:")
240+
print("Key: " + updated_config_setting.key + ", Value: " + updated_config_setting.value)
241+
242+
deleted_config_setting = app_config_client.delete_configuration_setting(key="TestApp:Settings:NewSetting")
243+
print("\nDeleted configuration setting:")
244+
print("Key: " + deleted_config_setting.key + ", Value: " + deleted_config_setting.value)
245+
246+
except Exception as ex:
247+
print('Exception:')
248+
print(ex)
249+
```
250+
251+
### [Connection string](#tab/connection-string)
252+
253+
```python
254+
import os
255+
from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting
256+
257+
try:
258+
print("Azure App Configuration - Python example")
259+
# Example code goes here
260+
261+
connection_string = os.getenv('AZURE_APPCONFIG_CONNECTION_STRING')
262+
app_config_client = AzureAppConfigurationClient.from_connection_string(connection_string)
263+
264+
retrieved_config_setting = app_config_client.get_configuration_setting(key='TestApp:Settings:Message')
265+
print("\nRetrieved configuration setting:")
266+
print("Key: " + retrieved_config_setting.key + ", Value: " + retrieved_config_setting.value)
267+
268+
config_setting = ConfigurationSetting(
269+
key='TestApp:Settings:NewSetting',
270+
value='New setting value'
271+
)
272+
added_config_setting = app_config_client.add_configuration_setting(config_setting)
273+
print("\nAdded configuration setting:")
274+
print("Key: " + added_config_setting.key + ", Value: " + added_config_setting.value)
275+
276+
filtered_settings_list = app_config_client.list_configuration_settings(key_filter="TestApp*")
277+
print("\nRetrieved list of configuration settings:")
278+
for item in filtered_settings_list:
279+
print("Key: " + item.key + ", Value: " + item.value)
280+
281+
locked_config_setting = app_config_client.set_read_only(added_config_setting, read_only=True)
282+
print("\nRead-only status for " + locked_config_setting.key + ": " + str(locked_config_setting.read_only))
283+
284+
unlocked_config_setting = app_config_client.set_read_only(locked_config_setting, read_only=False)
285+
print("\nRead-only status for " + unlocked_config_setting.key + ": " + str(unlocked_config_setting.read_only))
286+
287+
added_config_setting.value = "Value has been updated!"
288+
updated_config_setting = app_config_client.set_configuration_setting(added_config_setting)
289+
print("\nUpdated configuration setting:")
290+
print("Key: " + updated_config_setting.key + ", Value: " + updated_config_setting.value)
291+
292+
deleted_config_setting = app_config_client.delete_configuration_setting(key="TestApp:Settings:NewSetting")
293+
print("\nDeleted configuration setting:")
294+
print("Key: " + deleted_config_setting.key + ", Value: " + deleted_config_setting.value)
295+
296+
except Exception as ex:
297+
print('Exception:')
298+
print(ex)
299+
```
300+
301+
---
372302

373-
Updated configuration setting:
374-
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
303+
1. Configure an environment variable
304+
305+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
306+
307+
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.
308+
309+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
310+
311+
```cmd
312+
setx AZURE_APPCONFIG_ENDPOINT "endpoint-of-your-app-configuration-store"
313+
```
314+
315+
If you use PowerShell, run the following command:
316+
317+
```powershell
318+
$Env:AZURE_APPCONFIG_ENDPOINT = "endpoint-of-your-app-configuration-store"
319+
```
320+
321+
If you use macOS or Linux, run the following command:
322+
323+
```bash
324+
export AZURE_APPCONFIG_ENDPOINT='<endpoint-of-your-app-configuration-store>'
325+
```
326+
327+
### [Connection string](#tab/connection-string)
328+
329+
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.
330+
331+
If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:
332+
333+
```cmd
334+
setx AZURE_APPCONFIG_CONNECTION_STRING "<connection-string-of-your-app-configuration-store>"
335+
```
336+
337+
If you use PowerShell, run the following command:
338+
339+
```powershell
340+
$Env:AZURE_APPCONFIG_CONNECTION_STRING = "connection-string-of-your-app-configuration-store"
341+
```
342+
343+
If you use macOS or Linux, run the following command:
344+
345+
```bash
346+
export AZURE_APPCONFIG_CONNECTION_STRING='<connection-string-of-your-app-configuration-store>'
347+
```
348+
---
375349

376-
Deleted configuration setting:
377-
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
378-
```
350+
1. After the environment variable is properly set, 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:
351+
352+
```console
353+
python app-configuration-example.py
354+
```
355+
356+
You should see the following output:
357+
358+
```output
359+
Azure App Configuration - Python example
360+
361+
Retrieved configuration setting:
362+
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
363+
364+
Added configuration setting:
365+
Key: TestApp:Settings:NewSetting, Value: New setting value
366+
367+
Retrieved list of configuration settings:
368+
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
369+
Key: TestApp:Settings:NewSetting, Value: New setting value
370+
371+
Read-only status for TestApp:Settings:NewSetting: True
372+
373+
Read-only status for TestApp:Settings:NewSetting: False
374+
375+
Updated configuration setting:
376+
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
377+
378+
Deleted configuration setting:
379+
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
380+
```
379381

380382
## Clean up resources
381383

0 commit comments

Comments
 (0)