Skip to content

Commit 75e11a0

Browse files
Merge pull request #235109 from albertofori/pythonQuickstart
Python quickstart updates with Django and Flask snippets
2 parents 0f963f3 + f0c317b commit 75e11a0

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

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

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ Add the following key-values to the App Configuration store. For more informatio
3333
| *test.message* | *Hello test* | Leave empty | Leave empty |
3434
| *my_json* | *{"key":"value"}* | Leave empty | *application/json* |
3535

36-
## Set up the Python app
36+
## Console applications
37+
In this section, you will create a console application and load data from your App Configuration store.
3738

39+
### Connect to App Configuration
3840
1. Create a new directory for the project named *app-configuration-quickstart*.
3941

4042
```console
@@ -87,35 +89,35 @@ Add the following key-values to the App Configuration store. For more informatio
8789
print("test.message found: " + str("test.message" in config))
8890
```
8991

90-
## Configure your App Configuration connection string
92+
### Run the application
9193

9294
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:
9395

94-
### [Windows command prompt](#tab/windowscommandprompt)
96+
#### [Windows command prompt](#tab/windowscommandprompt)
9597

9698
To build and 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:
9799

98100
```cmd
99101
setx AZURE_APPCONFIG_CONNECTION_STRING "connection-string-of-your-app-configuration-store"
100102
```
101103

102-
### [PowerShell](#tab/powershell)
104+
#### [PowerShell](#tab/powershell)
103105

104106
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:
105107

106108
```azurepowershell
107109
$Env:AZURE_APPCONFIG_CONNECTION_STRING = "<app-configuration-store-connection-string>"
108110
```
109111

110-
### [macOS](#tab/unix)
112+
#### [macOS](#tab/unix)
111113

112114
If you use macOS, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
113115

114116
```console
115117
export AZURE_APPCONFIG_CONNECTION_STRING='<app-configuration-store-connection-string>'
116118
```
117119

118-
### [Linux](#tab/linux)
120+
#### [Linux](#tab/linux)
119121

120122
If you use Linux, run the following command and replace `<app-configuration-store-connection-string>` with the connection string of your app configuration store:
121123

@@ -125,31 +127,31 @@ Add the following key-values to the App Configuration store. For more informatio
125127

126128
1. Restart the command prompt to allow the change to take effect. Print out the value of the environment variable to validate that it is set properly with the command below.
127129

128-
### [Windows command prompt](#tab/windowscommandprompt)
130+
#### [Windows command prompt](#tab/windowscommandprompt)
129131

130132
Using the Windows command prompt, run the following command:
131133

132134
```cmd
133135
printenv AZURE_APPCONFIG_CONNECTION_STRING
134136
```
135137
136-
### [PowerShell](#tab/powershell)
138+
#### [PowerShell](#tab/powershell)
137139
138140
If you use Windows PowerShell, run the following command:
139141
140142
```azurepowershell
141143
$Env:AZURE_APPCONFIG_CONNECTION_STRING
142144
```
143145
144-
### [macOS](#tab/unix)
146+
#### [macOS](#tab/unix)
145147
146148
If you use macOS, run the following command:
147149
148150
```console
149151
echo "$AZURE_APPCONFIG_CONNECTION_STRING"
150152
```
151153
152-
### [Linux](#tab/linux)
154+
#### [Linux](#tab/linux)
153155
154156
If you use Linux, run the following command:
155157
@@ -172,6 +174,41 @@ Add the following key-values to the App Configuration store. For more informatio
172174
test.message found: False
173175
```
174176
177+
## Web applications
178+
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.
179+
180+
### [Flask](#tab/flask)
181+
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`:
182+
183+
```python
184+
azure_app_config = load(connection_string=os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING"))
185+
186+
# NOTE: This will override all existing configuration settings with the same key name.
187+
app.config.update(azure_app_config)
188+
189+
# Access a configuration setting directly from within Flask configuration
190+
message = app.config.get("message")
191+
```
192+
193+
### [Django](#tab/django)
194+
You can use Azure App Configuration in your existing Django web apps by adding the following lines of code into your `settings.py` file
195+
196+
```python
197+
CONFIG = load(connection_string=os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING"))
198+
```
199+
200+
To access individual configuration settings in the Django views, you can reference them from the provider object created in Django settings. For example, in `views.py`:
201+
```python
202+
# Import Django settings
203+
from django.conf import settings
204+
205+
# Access a configuration setting from Django settings instance.
206+
MESSAGE = settings.CONFIG.get("message")
207+
```
208+
---
209+
210+
Full code samples on how to use Azure App Configuration in Python web applications can be found in the [Azure App Configuration](https://github.com/Azure/AppConfiguration/tree/main/examples/Python) GitHub repo.
211+
175212
## Clean up resources
176213

177214
[!INCLUDE [azure-app-configuration-cleanup](../../includes/azure-app-configuration-cleanup.md)]

0 commit comments

Comments
 (0)