Create a Python app with the Azure SDK for Python
This document shows examples of how to use the Azure SDK for Python to access your data in Azure App Configuration.
Tip
App Configuration offers a Python provider library that is built on top of the Python SDK and is designed to be easier to use with richer features. It enables configuration settings to be used like a dictionary, and offers other features like configuration composition from multiple labels, key name trimming, and automatic resolution of Key Vault references. Go to the Python quickstart to learn more.
Prerequisites
- Azure subscription - create one for free
- Python 3.8 or later - for information on setting up Python on Windows, see the Python on Windows documentation
- An App Configuration store. Create a store.
Create a key-value
Add the following key-value to the App Configuration store and leave Label and Content Type with their default values. 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 |
---|---|
TestApp:Settings:Message | Data from Azure App Configuration |
Set up the Python app
Create a new directory for the project named app-configuration-example.
mkdir app-configuration-example
Switch to the newly created app-configuration-example directory.
cd app-configuration-example
Install the Azure App Configuration client library by using the
pip install
command.pip install azure-appconfiguration
Create a new file called app-configuration-example.py in the app-configuration-example directory and add the following code:
import os from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting try: print("Azure App Configuration - Python example") # Example code goes here except Exception as ex: print('Exception:') print(ex)
Note
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.
Configure your App Configuration connection string
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:
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:setx AZURE_APPCONFIG_CONNECTION_STRING "connection-string-of-your-app-configuration-store"
Print out the value of the environment variable to validate that it is set properly with the command below.
Using the Windows command prompt, restart the command prompt to allow the change to take effect and run the following command:
echo %AZURE_APPCONFIG_CONNECTION_STRING%
Code samples
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.
Note
The App Configuration client library refers to a key-value object as ConfigurationSetting
. Therefore, in this article, the key-values in App Configuration store will be referred to as configuration settings.
Learn below how to:
- Connect to an App Configuration store
- Get a configuration setting
- Add a configuration setting
- Get a list of configuration settings
- Lock a configuration setting
- Unlock a configuration setting
- Update a configuration setting
- Delete a configuration setting
Connect to an App Configuration store
The following code snippet creates an instance of AzureAppConfigurationClient using the connection string stored in your environment variables.
connection_string = os.getenv('AZURE_APPCONFIG_CONNECTION_STRING')
app_config_client = AzureAppConfigurationClient.from_connection_string(connection_string)
Get a configuration setting
The following code snippet retrieves a configuration setting by key
name.
retrieved_config_setting = app_config_client.get_configuration_setting(key='TestApp:Settings:Message')
print("\nRetrieved configuration setting:")
print("Key: " + retrieved_config_setting.key + ", Value: " + retrieved_config_setting.value)
Add a configuration setting
The following code snippet creates a ConfigurationSetting
object with key
and value
fields and invokes the add_configuration_setting
method.
This method will throw an exception if you try to add a configuration setting that already exists in your store. If you want to avoid this exception, the set_configuration_setting method can be used instead.
config_setting = ConfigurationSetting(
key='TestApp:Settings:NewSetting',
value='New setting value'
)
added_config_setting = app_config_client.add_configuration_setting(config_setting)
print("\nAdded configuration setting:")
print("Key: " + added_config_setting.key + ", Value: " + added_config_setting.value)
Get a list of configuration settings
The following code snippet retrieves a list of configuration settings. The key_filter
and label_filter
arguments can be provided to filter key-values based on key
and label
respectively. For more information on filtering, see how to query configuration settings.
filtered_settings_list = app_config_client.list_configuration_settings(key_filter="TestApp*")
print("\nRetrieved list of configuration settings:")
for item in filtered_settings_list:
print("Key: " + item.key + ", Value: " + item.value)
Lock a configuration setting
The lock status of a key-value in App Configuration is denoted by the read_only
attribute of the ConfigurationSetting
object. If read_only
is True
, the setting is locked. The set_read_only
method can be invoked with read_only=True
argument to lock the configuration setting.
locked_config_setting = app_config_client.set_read_only(added_config_setting, read_only=True)
print("\nRead-only status for " + locked_config_setting.key + ": " + str(locked_config_setting.read_only))
Unlock a configuration setting
If the read_only
attribute of a ConfigurationSetting
is False
, the setting is unlocked. The set_read_only
method can be invoked with read_only=False
argument to unlock the configuration setting.
unlocked_config_setting = app_config_client.set_read_only(locked_config_setting, read_only=False)
print("\nRead-only status for " + unlocked_config_setting.key + ": " + str(unlocked_config_setting.read_only))
Update a configuration setting
The set_configuration_setting
method can be used to update an existing setting or create a new setting. The following code snippet changes the value of an existing configuration setting.
added_config_setting.value = "Value has been updated!"
updated_config_setting = app_config_client.set_configuration_setting(added_config_setting)
print("\nUpdated configuration setting:")
print("Key: " + updated_config_setting.key + ", Value: " + updated_config_setting.value)
Delete a configuration setting
The following code snippet deletes a configuration setting by key
name.
deleted_config_setting = app_config_client.delete_configuration_setting(key="TestApp:Settings:NewSetting")
print("\nDeleted configuration setting:")
print("Key: " + deleted_config_setting.key + ", Value: " + deleted_config_setting.value)
Run the app
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.
At this point, your app-configuration-example.py file should have the following code:
import os
from azure.appconfiguration import AzureAppConfigurationClient, ConfigurationSetting
try:
print("Azure App Configuration - Python example")
# Example code goes here
connection_string = os.getenv('AZURE_APPCONFIG_CONNECTION_STRING')
app_config_client = AzureAppConfigurationClient.from_connection_string(connection_string)
retrieved_config_setting = app_config_client.get_configuration_setting(key='TestApp:Settings:Message')
print("\nRetrieved configuration setting:")
print("Key: " + retrieved_config_setting.key + ", Value: " + retrieved_config_setting.value)
config_setting = ConfigurationSetting(
key='TestApp:Settings:NewSetting',
value='New setting value'
)
added_config_setting = app_config_client.add_configuration_setting(config_setting)
print("\nAdded configuration setting:")
print("Key: " + added_config_setting.key + ", Value: " + added_config_setting.value)
filtered_settings_list = app_config_client.list_configuration_settings(key_filter="TestApp*")
print("\nRetrieved list of configuration settings:")
for item in filtered_settings_list:
print("Key: " + item.key + ", Value: " + item.value)
locked_config_setting = app_config_client.set_read_only(added_config_setting, read_only=True)
print("\nRead-only status for " + locked_config_setting.key + ": " + str(locked_config_setting.read_only))
unlocked_config_setting = app_config_client.set_read_only(locked_config_setting, read_only=False)
print("\nRead-only status for " + unlocked_config_setting.key + ": " + str(unlocked_config_setting.read_only))
added_config_setting.value = "Value has been updated!"
updated_config_setting = app_config_client.set_configuration_setting(added_config_setting)
print("\nUpdated configuration setting:")
print("Key: " + updated_config_setting.key + ", Value: " + updated_config_setting.value)
deleted_config_setting = app_config_client.delete_configuration_setting(key="TestApp:Settings:NewSetting")
print("\nDeleted configuration setting:")
print("Key: " + deleted_config_setting.key + ", Value: " + deleted_config_setting.value)
except Exception as ex:
print('Exception:')
print(ex)
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:
python app-configuration-example.py
You should see the following output:
Azure App Configuration - Python example
Retrieved configuration setting:
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
Added configuration setting:
Key: TestApp:Settings:NewSetting, Value: New setting value
Retrieved list of configuration settings:
Key: TestApp:Settings:Message, Value: Data from Azure App Configuration
Key: TestApp:Settings:NewSetting, Value: New setting value
Read-only status for TestApp:Settings:NewSetting: True
Read-only status for TestApp:Settings:NewSetting: False
Updated configuration setting:
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
Deleted configuration setting:
Key: TestApp:Settings:NewSetting, Value: Value has been updated!
Clean up resources
If you don't want to continue using the resources created in this article, delete the resource group you created here to avoid charges.
Important
Deleting a resource group is irreversible. The resource group and all the resources in it are permanently deleted. Ensure that you don't accidentally delete the wrong resource group or resources. If you created the resources for this article inside a resource group that contains other resources you want to keep, delete each resource individually from its respective pane instead of deleting the resource group.
- Sign in to the Azure portal, and select Resource groups.
- In the Filter by name box, enter the name of your resource group.
- In the result list, select the resource group name to see an overview.
- Select Delete resource group.
- You're asked to confirm the deletion of the resource group. Enter the name of your resource group to confirm, and select Delete.
After a few moments, the resource group and all its resources are deleted.
Next steps
This guide showed you how to use the Azure SDK for Python to access your data in Azure App Configuration.
For additional code samples, visit:
To learn how to use Azure App Configuration with Python apps, go to:
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for