Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Quickstart: Create a Python app with Azure App Configuration

Feedback

In this article

In this quickstart, you will use the Python provider for Azure App Configuration to centralize storage and management of application settings using theAzure App Configuration Python provider client library.

The Python App Configuration provider is a library running on top of theAzure 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 toCreate a key-value.

KeyValueLabelContent type
messageHelloLeave emptyLeave empty
test.messageHello testLeave emptyLeave empty
my_json{"key":"value"}Leave emptyapplication/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 namedapp-configuration-quickstart.

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

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

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

    You use theDefaultAzureCredential to authenticate to your App Configuration store. Follow theinstructions to assign your credential theApp Configuration Data Reader role. Be sure to allow sufficient time for the permission to propagate before running your application.

    from azure.appconfiguration.provider import (    load,    SettingSelector)from azure.identity import DefaultAzureCredentialimport osendpoint = os.environ.get("AZURE_APPCONFIG_ENDPOINT")# Connect to Azure App Configuration using Microsoft Entra ID.config = load(endpoint=endpoint, credential=credential)credential = DefaultAzureCredential()# 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 Entra ID and trimmed key prefixes.trimmed = {"test."}config = load(endpoint=endpoint, credential=credential, 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(endpoint=endpoint, credential=credential, 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.

    Set the environment variable namedAZURE_APPCONFIG_ENDPOINT to the endpoint of your App Configuration store found under theOverview of your store in the Azure portal.

    If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:

    setx AZURE_APPCONFIG_ENDPOINT "endpoint-of-your-app-configuration-store"

    If you use PowerShell, run the following command:

    $Env:AZURE_APPCONFIG_ENDPOINT = "endpoint-of-your-app-configuration-store"

    If you use macOS or Linux, run the following command:

    export AZURE_APPCONFIG_ENDPOINT='<endpoint-of-your-app-configuration-store>'
  2. 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:

    HellovalueHello testmessage found: Truetest.message found: False

Web applications

The App Configuration provider loads data into aMapping 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 theupdate function of your Flask app instance inapp.py:

azure_app_config = load(endpoint=os.environ.get("AZURE_APPCONFIG_ENDPOINT"), credential=credential)# 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 configurationmessage = app.config.get("message")

Full code samples on how to use Azure App Configuration in Python web applications can be found in theAzure 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 theAzure portal, and selectResource groups.
  2. In theFilter 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. SelectDelete resource group.
  5. You're asked to confirm the deletion of the resource group. Enter the name of your resource group to confirm, and selectDelete.

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:


Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?