Quickstart: Create a Python app with Azure App Configuration

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.

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.

Prerequisites

Add key-values

Add the following key-values to the App Configuration store. 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 Label Content type
message Hello Leave empty Leave empty
test.message Hello test Leave empty Leave empty
my_json {"key":"value"} Leave empty application/json

Console applications

In this section, you will create a console application and load data from your App Configuration store.

Connect to App Configuration

  1. Create a new directory for the project named app-configuration-quickstart.

    mkdir app-configuration-quickstart
    
  2. Switch to the newly created app-configuration-quickstart directory.

    cd app-configuration-quickstart
    
  3. Install the Azure App Configuration provider by using the pip install command.

    pip install azure-appconfiguration-provider
    
  4. Create a new file called app-configuration-quickstart.py in the app-configuration-quickstart directory and add the following code:

    from azure.appconfiguration.provider import (
        load,
        SettingSelector
    )
    import os
    
    connection_string = os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING")
    
    # Connect to Azure App Configuration using a connection string.
    config = load(connection_string=connection_string)
    
    # Find the key "message" and print its value.
    print(config["message"])
    # Find the key "my_json" and print the value for "key" from the dictionary.
    print(config["my_json"]["key"])
    
    # Connect to Azure App Configuration using a connection string and trimmed key prefixes.
    trimmed = {"test."}
    config = load(connection_string=connection_string, trim_prefixes=trimmed)
    # From the keys with trimmed prefixes, find a key with "message" and print its value.
    print(config["message"])
    
    # Connect to Azure App Configuration using SettingSelector.
    selects = {SettingSelector(key_filter="message*", label_filter="\0")}
    config = load(connection_string=connection_string, selects=selects)
    
    # Print True or False to indicate if "message" is found in Azure App Configuration.
    print("message found: " + str("message" in config))
    print("test.message found: " + str("test.message" in config))
    

Run the application

  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:

    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"
    
  2. 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%
    
  3. After the environment variable is properly set, run the following command to run the app locally:

    python app-configuration-quickstart.py
    

    You should see the following output:

    Hello
    value
    Hello test
    message found: True
    test.message found: False
    

Web applications

The App Configuration provider loads data into a Mapping object, accessible as a dictionary, which can be used in combination with the existing configuration of various Python frameworks. This section shows how to use the App Configuration provider in popular web frameworks like Flask and Django.

You can use Azure App Configuration in your existing Flask web apps by updating its in-built configuration. You can do this by passing your App Configuration provider object to the update function of your Flask app instance in app.py:

azure_app_config = load(connection_string=os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING"))

# NOTE: This will override all existing configuration settings with the same key name.
app.config.update(azure_app_config)

# Access a configuration setting directly from within Flask configuration
message = app.config.get("message")

Full code samples on how to use Azure App Configuration in Python web applications can be found in the Azure App Configuration GitHub repo.

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.

  1. Sign in to the Azure portal, and select Resource groups.
  2. In the Filter by name box, enter the name of your resource group.
  3. In the result list, select the resource group name to see an overview.
  4. Select Delete resource group.
  5. 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

In this quickstart, you created a new App Configuration store and learned how to access key-values from a Python app.

For additional code samples, visit: