You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -20,6 +20,9 @@ Before you begin, you must complete the [quickstart: Create a C# function in Azu
20
20
::: zone pivot="programming-language-javascript"
21
21
Before you begin, you must complete the [quickstart: Create a JavaScript function in Azure using Visual Studio Code](create-first-function-vs-code-node.md). If you already cleaned up resources at the end of that article, go through the steps again to recreate the function app and related resources in Azure.
22
22
::: zone-end
23
+
::: zone pivot="programming-language-python"
24
+
Before you begin, you must complete the [quickstart: Create a Python function in Azure using Visual Studio Code](create-first-function-vs-code-python.md). If you already cleaned up resources at the end of that article, go through the steps again to recreate the function app and related resources in Azure.
25
+
::: zone-end
23
26
24
27
## Configure your environment
25
28
@@ -30,7 +33,7 @@ Before you get started, make sure to install the [Azure Databases extension](htt
30
33
> [!IMPORTANT]
31
34
> [Azure Cosmos DB serverless](../cosmos-db/serverless.md) is now generally available. This consumption-based mode makes Azure Cosmos DB a strong option for serverless workloads. To use Azure Cosmos DB in serverless mode, choose **Serverless** as the **Capacity mode** when creating your account.
32
35
33
-
1. In Visual Studio Code, select **View** > **Command Pallete...** then in the command pallete search for `Azure Databases: Create Server...`
36
+
1. In Visual Studio Code, select **View** > **Command Palette...** then in the command palette search for `Azure Databases: Create Server...`
34
37
35
38
1. Provide the following information at the prompts:
36
39
@@ -46,7 +49,7 @@ Before you get started, make sure to install the [Azure Databases extension](htt
46
49
47
50
## Create an Azure Cosmos DB database and container
48
51
49
-
1. Select the Azure icon in the Activity bar, expand **Resources** > **Azure Cosmos DB**, right-click (Ctrl+click on macOS) your account, and select **Create database...**.
52
+
1. Select the Azure icon in the Activity bar, expand **Resources** > **Azure Cosmos DB**, right-click (Ctrl+select on macOS) your account, and select **Create database...**.
50
53
51
54
1. Provide the following information at the prompts:
52
55
@@ -60,9 +63,9 @@ Before you get started, make sure to install the [Azure Databases extension](htt
60
63
61
64
## Update your function app settings
62
65
63
-
In the [previous quickstart article](./create-first-function-vs-code-csharp.md), you created a function app in Azure. In this article, you update your app to write JSON documents to the Azure Cosmos DB container you've just created. To connect to your Azure Cosmos DB account, you must add its connection string to your app settings. You then download the new setting to your local.settings.json file so you can connect to your Azure Cosmos DB account when running locally.
66
+
In the [previous quickstart article](./create-first-function-vs-code-csharp.md), you created a function app in Azure. In this article, you update your app to write JSON documents to the Azure Cosmos DB container you've created. To connect to your Azure Cosmos DB account, you must add its connection string to your app settings. You then download the new setting to your local.settings.json file so you can connect to your Azure Cosmos DB account when running locally.
64
67
65
-
1. In Visual Studio Code, right-click (Ctrl+click on macOS) on your new Azure Cosmos DB account, and select **Copy Connection String**.
68
+
1. In Visual Studio Code, right-click (Ctrl+select on macOS) on your new Azure Cosmos DB account, and select **Copy Connection String**.
66
69
67
70
:::image type="content" source="./media/functions-add-output-binding-cosmos-db-vs-code/copy-connection-string.png" alt-text="Copying the Azure Cosmos DB connection string" border="true":::
68
71
@@ -73,7 +76,7 @@ In the [previous quickstart article](./create-first-function-vs-code-csharp.md),
73
76
|Prompt| Selection|
74
77
|--|--|
75
78
|**Enter new app setting name**| Type `CosmosDbConnectionString`.|
76
-
|**Enter value for "CosmosDbConnectionString"**| Paste the connection string of your Azure Cosmos DB account you just copied.|
79
+
|**Enter value for "CosmosDbConnectionString"**| Paste the connection string of your Azure Cosmos DB account you copied.|
77
80
78
81
This creates an application setting named connection `CosmosDbConnectionString` in your function app in Azure. Now, you can download this setting to your local.settings.json file.
79
82
@@ -89,7 +92,7 @@ Because you're using an Azure Cosmos DB output binding, you must have the corres
89
92
90
93
::: zone pivot="programming-language-csharp"
91
94
92
-
With the exception of HTTP and timer triggers, bindings are implemented as extension packages. Run the following [dotnet add package](/dotnet/core/tools/dotnet-add-package) command in the Terminal window to add the Azure Cosmos DB extension package to your project.
95
+
Except for HTTP and timer triggers, bindings are implemented as extension packages. Run the following [dotnet add package](/dotnet/core/tools/dotnet-add-package) command in the Terminal window to add the Azure Cosmos DB extension package to your project.
::: zone pivot="programming-language-javascript,programming-language-python"
106
109
107
110
Your project has been configured to use [extension bundles](functions-bindings-register.md#extension-bundles), which automatically installs a predefined set of extension packages.
108
111
109
-
Extension bundles usage is enabled in the host.json file at the root of the project, which appears as follows:
112
+
Extension bundles usage is enabled in the *host.json* file at the root of the project, which appears as follows:
@@ -116,7 +119,7 @@ Now, you can add the Azure Cosmos DB output binding to your project.
116
119
117
120
## Add an output binding
118
121
119
-
In Functions, each type of binding requires a `direction`, `type`, and a unique `name` to be defined in the function.json file. The way you define these attributes depends on the language of your function app.
122
+
In Functions, each type of binding requires a `direction`, `type`, and a unique `name` to be defined in the *function.json* file. The way you define these attributes depends on the language of your function app.
120
123
121
124
::: zone pivot="programming-language-csharp"
122
125
@@ -137,7 +140,7 @@ Open the *HttpExample.cs* project file and add the following classes:
The `MyDocument` class defines an object that gets written to the database. The connection string for the Storage account is set by the `Connection` property. In this case, you could omit `Connection` because you are already using the default storage account.
143
+
The `MyDocument` class defines an object that gets written to the database. The connection string for the Storage account is set by the `Connection` property. In this case, you could omit `Connection` because you're already using the default storage account.
141
144
142
145
The `MultiResponse` class allows you to both write to the specified collection in the Azure Cosmos DB and return an HTTP success message. Because you need to return a `MultiResponse` object, you need to also update the method signature.
143
146
@@ -149,9 +152,9 @@ Specific attributes specify the name of the container and the name of its parent
149
152
150
153
::: zone pivot="programming-language-javascript"
151
154
152
-
Binding attributes are defined directly in the function.json file. Depending on the binding type, additional properties may be required. The [Azure Cosmos DB output configuration](./functions-bindings-cosmosdb-v2-output.md#configuration) describes the fields required for an Azure Cosmos DB output binding. The extension makes it easy to add bindings to the function.json file.
155
+
Binding attributes are defined directly in the *function.json* file. Depending on the binding type, other properties may be required. The [Azure Cosmos DB output configuration](./functions-bindings-cosmosdb-v2-output.md#configuration) describes the fields required for an Azure Cosmos DB output binding. The extension makes it easy to add bindings to the *function.json* file.
153
156
154
-
To create a binding, right-click (Ctrl+click on macOS) the `function.json` file in your HttpTrigger folder and choose **Add binding...**. Follow the prompts to define the following binding properties for the new binding:
157
+
To create a binding, right-click (Ctrl+select on macOS) the *function.json* file in your HttpTrigger folder and choose **Add binding...**. Follow the prompts to define the following binding properties for the new binding:
155
158
156
159
| Prompt | Value | Description |
157
160
| -------- | ----- | ----------- |
@@ -165,7 +168,7 @@ To create a binding, right-click (Ctrl+click on macOS) the `function.json` file
165
168
|**Partition key (optional)**|*leave blank*| Only required when the output binding creates the container. |
166
169
|**Collection throughput (optional)**|*leave blank*| Only required when the output binding creates the container. |
167
170
168
-
A binding is added to the `bindings` array in your function.json, which should look like the following after removing any `undefined` values present
171
+
A binding is added to the `bindings` array in your *function.json*, which should look like the following after removing any `undefined` values present
169
172
170
173
```json
171
174
{
@@ -181,6 +184,57 @@ A binding is added to the `bindings` array in your function.json, which should l
181
184
182
185
::: zone-end
183
186
187
+
::: zone pivot="programming-language-python"
188
+
189
+
The way that you define the new binding depends on your Python programming model.
190
+
191
+
# [v1](#tab/v1)
192
+
193
+
Binding attributes are defined directly in the *function.json* file. Depending on the binding type, other properties may be required. The [Azure Cosmos DB output configuration](./functions-bindings-cosmosdb-v2-output.md#configuration) describes the fields required for an Azure Cosmos DB output binding. The extension makes it easy to add bindings to the *function.json* file.
194
+
195
+
To create a binding, right-select (Ctrl+select on macOS) the *function.json* file in your HttpTrigger folder and choose **Add binding...**. Follow the prompts to define the following binding properties for the new binding:
196
+
197
+
| Prompt | Value | Description |
198
+
| -------- | ----- | ----------- |
199
+
|**Select binding direction**|`out`| The binding is an output binding. |
200
+
|**Select binding with direction "out"**|`Azure Cosmos DB`| The binding is an Azure Cosmos DB binding. |
201
+
|**The name used to identify this binding in your code**|`outputDocument`| Name that identifies the binding parameter referenced in your code. |
202
+
|**The Azure Cosmos DB database where data will be written**|`my-database`| The name of the Azure Cosmos DB database containing the target container. |
203
+
|**Database collection where data will be written**|`my-container`| The name of the Azure Cosmos DB container where the JSON documents will be written. |
204
+
|**If true, creates the Azure Cosmos DB database and collection**|`false`| The target database and container already exist. |
205
+
|**Select setting from "local.setting.json"**|`CosmosDbConnectionString`| The name of an application setting that contains the connection string for the Azure Cosmos DB account. |
206
+
|**Partition key (optional)**|*leave blank*| Only required when the output binding creates the container. |
207
+
|**Collection throughput (optional)**|*leave blank*| Only required when the output binding creates the container. |
208
+
209
+
A binding is added to the `bindings` array in your *function.json*, which should look like the following after removing any `undefined` values present
Binding attributes are defined directly in the *function_app.py* file. You use the `cosmos_db_output` decorator to add an [Azure Cosmos DB output binding](/azure/azure-functions/functions-bindings-triggers-python#azure-cosmos-db-output-binding):
In this code, `arg_name` identifies the binding parameter referenced in your code, `database_name` and `collection_name` are the database and collection names that the binding writes to, and `connection_string_setting` is the name of an application setting that contains the connection string for the Storage account, which is in the CosmosDbConnectionString setting in the *local.settings.json* file.
233
+
234
+
---
235
+
236
+
::: zone-end
237
+
184
238
## Add code that uses the output binding
185
239
186
240
::: zone pivot="programming-language-csharp"
@@ -295,11 +349,90 @@ This code now returns a `MultiResponse` object that contains both a document and
295
349
296
350
::: zone-end
297
351
352
+
::: zone pivot="programming-language-python"
353
+
354
+
# [v1](#tab/v1)
355
+
356
+
Update *HttpExample\\\_\_init\_\_.py* to match the following code. Add the `outputDocument` parameter to the function definition and `outputDocument.set()` under the `if name:` statement:
"Please pass a name on the query string or in the request body",
381
+
status_code=400
382
+
)
383
+
```
384
+
385
+
The document `{"id": "name"}` is created in the database collection specified in the binding.
386
+
387
+
# [v2](#tab/v2)
388
+
389
+
Update *HttpExample\\function_app.py* to match the following code. Add the `outputDocument` parameter to the function definition and `outputDocument.set()` under the `if name:` statement:
0 commit comments