Skip to content

Commit 7146442

Browse files
Merge pull request #210085 from ggailey777/pystein
[Functions] Python v2 examples and decorators in binding refs
2 parents 81a0ebc + e373a2e commit 7146442

18 files changed

+1439
-553
lines changed

.openpublishing.redirection.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7548,6 +7548,11 @@
75487548
"redirect_url": "/azure/reliability/reliability-functions",
75497549
"redirect_document_id": false
75507550
},
7551+
{
7552+
"source_path_from_root": "/articles/azure-functions/functions-bindings-triggers-python.md",
7553+
"redirect_url": "/azure/azure-functions/functions-reference-python?pivots=python-mode-decorators#triggers-and-inputs",
7554+
"redirect_document_id": false
7555+
},
75517556
{
75527557
"source_path_from_root": "/articles/azure-government/documentation-government-k8.md",
75537558
"redirect_url": "/azure/azure-government",

articles/azure-functions/durable/durable-functions-bindings.md

Lines changed: 319 additions & 105 deletions
Large diffs are not rendered by default.

articles/azure-functions/functions-bindings-cosmosdb-v2-input.md

Lines changed: 112 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Azure Cosmos DB input binding for Functions 2.x and higher
33
description: Learn to use the Azure Cosmos DB input binding in Azure Functions.
44
ms.topic: reference
5-
ms.date: 03/04/2022
5+
ms.date: 03/02/2023
66
ms.devlang: csharp, java, javascript, powershell, python
77
ms.custom: devx-track-csharp, devx-track-python, ignite-2022
88
zone_pivot_groups: programming-languages-set-functions-lang-workers
@@ -18,6 +18,23 @@ For information on setup and configuration details, see the [overview](./functio
1818
> When the collection is [partitioned](../cosmos-db/partitioning-overview.md#logical-partitions), lookup operations must also specify the partition key value.
1919
>
2020
21+
::: zone pivot="programming-language-python"
22+
Azure Functions supports two programming models for Python. The way that you define your bindings depends on your chosen programming model.
23+
24+
# [v2](#tab/python-v2)
25+
The Python v2 programming model lets you define bindings using decorators directly in your Python function code. For more information, see the [Python developer guide](functions-reference-python.md?pivots=python-mode-decorators#programming-model).
26+
27+
# [v1](#tab/python-v1)
28+
The Python v1 programming model requires you to define bindings in a separate *function.json* file in the function folder. For more information, see the [Python developer guide](functions-reference-python.md?pivots=python-mode-configuration#programming-model).
29+
30+
---
31+
32+
This article supports both programming models.
33+
34+
> [!IMPORTANT]
35+
> The Python v2 programming model is currently in preview.
36+
::: zone-end
37+
2138
## Example
2239

2340
Unless otherwise noted, examples in this article target version 3.x of the [Azure Cosmos DB extension](functions-bindings-cosmosdb-v2.md). For use with extension version 4.x, you need to replace the string `collection` in property and attribute names with `container`.
@@ -1505,9 +1522,45 @@ This section contains the following examples that read a single document by spec
15051522

15061523
<a id="queue-trigger-look-up-id-from-json-python"></a>
15071524

1525+
The examples depend on whether you use the [v1 or v2 Python programming model](functions-reference-python.md).
1526+
15081527
### Queue trigger, look up ID from JSON
15091528

1510-
The following example shows an Azure Cosmos DB input binding in a *function.json* file and a [Python function](functions-reference-python.md) that uses the binding. The function reads a single document and updates the document's text value.
1529+
The following example shows an Azure Cosmos DB input binding. The function reads a single document and updates the document's text value.
1530+
1531+
# [v2](#tab/python-v2)
1532+
1533+
```python
1534+
import logging
1535+
import azure.functions as func
1536+
1537+
app = func.FunctionApp()
1538+
1539+
@app.queue_trigger(arg_name="msg",
1540+
queue_name="outqueue",
1541+
connection="AzureWebJobsStorage")
1542+
@app.cosmos_db_input(arg_name="documents",
1543+
database_name="MyDatabase",
1544+
collection_name="MyCollection",
1545+
id="{msg.payload_property}",
1546+
partition_key="{msg.payload_property}",
1547+
connection_string_setting="MyAccount_COSMOSDB")
1548+
@app.cosmos_db_output(arg_name="outputDocument",
1549+
database_name="MyDatabase",
1550+
collection_name="MyCollection",
1551+
connection_string_setting="MyAccount_COSMOSDB")
1552+
def test_function(msg: func.QueueMessage,
1553+
inputDocument: func.DocumentList,
1554+
outputDocument: func.Out[func.Document]):
1555+
document = documents[id]
1556+
document["text"] = "This was updated!"
1557+
doc = inputDocument[0]
1558+
doc["text"] = "This was updated!"
1559+
outputDocument.set(doc)
1560+
print(f"Updated document.")
1561+
```
1562+
1563+
# [v1](#tab/python-v1)
15111564

15121565
Here's the binding data in the *function.json* file:
15131566

@@ -1548,11 +1601,20 @@ def main(queuemsg: func.QueueMessage, documents: func.DocumentList) -> func.Docu
15481601
return document
15491602
```
15501603

1604+
---
1605+
15511606
<a id="http-trigger-look-up-id-from-query-string-python"></a>
15521607

15531608
### HTTP trigger, look up ID from query string
15541609

1555-
The following example shows a [Python function](functions-reference-python.md) that retrieves a single document. The function is triggered by an HTTP request that uses a query string to specify the ID and partition key value to look up. That ID and partition key value are used to retrieve a `ToDoItem` document from the specified database and collection.
1610+
The following example shows a function that retrieves a single document. The function is triggered by an HTTP request that uses a query string to specify the ID and partition key value to look up. That ID and partition key value are used to retrieve a `ToDoItem` document from the specified database and collection.
1611+
1612+
# [v2](#tab/python-v2)
1613+
1614+
No equivalent sample for v2 at this time.
1615+
1616+
# [v1](#tab/python-v1)
1617+
15561618

15571619
Here's the *function.json* file:
15581620

@@ -1605,11 +1667,19 @@ def main(req: func.HttpRequest, todoitems: func.DocumentList) -> str:
16051667
return 'OK'
16061668
```
16071669

1670+
---
1671+
16081672
<a id="http-trigger-look-up-id-from-route-data-python"></a>
16091673

16101674
### HTTP trigger, look up ID from route data
16111675

1612-
The following example shows a [Python function](functions-reference-python.md) that retrieves a single document. The function is triggered by an HTTP request that uses route data to specify the ID and partition key value to look up. That ID and partition key value are used to retrieve a `ToDoItem` document from the specified database and collection.
1676+
The following example shows a function that retrieves a single document. The function is triggered by an HTTP request that uses route data to specify the ID and partition key value to look up. That ID and partition key value are used to retrieve a `ToDoItem` document from the specified database and collection.
1677+
1678+
# [v2](#tab/python-v2)
1679+
1680+
No equivalent sample for v2 at this time.
1681+
1682+
# [v1](#tab/python-v1)
16131683

16141684
Here's the *function.json* file:
16151685

@@ -1663,14 +1733,22 @@ def main(req: func.HttpRequest, todoitems: func.DocumentList) -> str:
16631733
return 'OK'
16641734
```
16651735

1736+
---
1737+
16661738
<a id="queue-trigger-get-multiple-docs-using-sqlquery-python"></a>
16671739

16681740
### Queue trigger, get multiple docs, using SqlQuery
16691741

1670-
The following example shows an Azure Cosmos DB input binding in a *function.json* file and a [Python function](functions-reference-python.md) that uses the binding. The function retrieves multiple documents specified by a SQL query, using a queue trigger to customize the query parameters.
1742+
The following example shows an Azure Cosmos DB input binding Python function that uses the binding. The function retrieves multiple documents specified by a SQL query, using a queue trigger to customize the query parameters.
16711743

16721744
The queue trigger provides a parameter `departmentId`. A queue message of `{ "departmentId" : "Finance" }` would return all records for the finance department.
16731745

1746+
# [v2](#tab/python-v2)
1747+
1748+
No equivalent sample for v2 at this time.
1749+
1750+
# [v1](#tab/python-v1)
1751+
16741752
Here's the binding data in the *function.json* file:
16751753

16761754
```json
@@ -1685,6 +1763,8 @@ Here's the binding data in the *function.json* file:
16851763
}
16861764
```
16871765

1766+
---
1767+
16881768
::: zone-end
16891769
::: zone pivot="programming-language-csharp"
16901770
## Attributes
@@ -1718,6 +1798,26 @@ Both [in-process](functions-dotnet-class-library.md) and [isolated worker proces
17181798
---
17191799

17201800
::: zone-end
1801+
1802+
::: zone pivot="programming-language-python"
1803+
## Decorators
1804+
1805+
_Applies only to the Python v2 programming model._
1806+
1807+
For Python v2 functions defined using a decorator, the following properties on the `cosmos_db_input`:
1808+
1809+
| Property | Description |
1810+
|-------------|-----------------------------|
1811+
|`arg_name` | The variable name used in function code that represents the list of documents with changes. |
1812+
|`database_name` | The name of the Azure Cosmos DB database with the collection being monitored. |
1813+
|`collection_name` | The name of the Azure CosmosDB collection being monitored. |
1814+
|`connection_string_setting` | The connection string of the Azure Cosmos DB being monitored. |
1815+
|`partition_key` | The partition key of the Azure Cosmos DB being monitored. |
1816+
|`id` | The ID of the document to retrieve. |
1817+
1818+
For Python functions defined by using *function.json*, see the [Configuration](#configuration) section.
1819+
::: zone-end
1820+
17211821
::: zone pivot="programming-language-java"
17221822
## Annotations
17231823

@@ -1735,6 +1835,13 @@ From the [Java functions runtime library](/java/api/overview/azure/functions/run
17351835
::: zone-end
17361836
::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python"
17371837
## Configuration
1838+
::: zone-end
1839+
1840+
::: zone pivot="programming-language-python"
1841+
_Applies only to the Python v1 programming model._
1842+
1843+
::: zone-end
1844+
::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python"
17381845

17391846
The following table explains the binding configuration properties that you set in the *function.json* file, where properties differ by extension version:
17401847

articles/azure-functions/functions-bindings-cosmosdb-v2-output.md

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Azure Cosmos DB output binding for Functions 2.x and higher
33
description: Learn to use the Azure Cosmos DB output binding in Azure Functions.
44
ms.topic: reference
5-
ms.date: 03/04/2022
5+
ms.date: 03/02/2023
66
ms.devlang: csharp, java, javascript, powershell, python
77
ms.custom: devx-track-csharp, devx-track-python, ignite-2022
88
zone_pivot_groups: programming-languages-set-functions-lang-workers
@@ -14,6 +14,23 @@ The Azure Cosmos DB output binding lets you write a new document to an Azure Cos
1414

1515
For information on setup and configuration details, see the [overview](./functions-bindings-cosmosdb-v2.md).
1616

17+
::: zone pivot="programming-language-python"
18+
Azure Functions supports two programming models for Python. The way that you define your bindings depends on your chosen programming model.
19+
20+
# [v2](#tab/python-v2)
21+
The Python v2 programming model lets you define bindings using decorators directly in your Python function code. For more information, see the [Python developer guide](functions-reference-python.md?pivots=python-mode-decorators#programming-model).
22+
23+
# [v1](#tab/python-v1)
24+
The Python v1 programming model requires you to define bindings in a separate *function.json* file in the function folder. For more information, see the [Python developer guide](functions-reference-python.md?pivots=python-mode-configuration#programming-model).
25+
26+
---
27+
28+
This article supports both programming models.
29+
30+
> [!IMPORTANT]
31+
> The Python v2 programming model is currently in preview.
32+
::: zone-end
33+
1734
## Example
1835

1936
Unless otherwise noted, examples in this article target version 3.x of the [Azure Cosmos DB extension](functions-bindings-cosmosdb-v2.md). For use with extension version 4.x, you need to replace the string `collection` in property and attribute names with `container`.
@@ -542,7 +559,7 @@ For bulk insert form the objects first and then run the stringify function. Here
542559
::: zone-end
543560
::: zone pivot="programming-language-powershell"
544561

545-
The following example show how to write data to Azure Cosmos DB using an output binding. The binding is declared in the function's configuration file (_functions.json_), and take data from a queue message and writes out to an Azure Cosmos DB document.
562+
The following example shows how to write data to Azure Cosmos DB using an output binding. The binding is declared in the function's configuration file (_functions.json_), and takes data from a queue message and writes out to an Azure Cosmos DB document.
546563

547564
```json
548565
{
@@ -572,7 +589,29 @@ Push-OutputBinding -Name EmployeeDocument -Value @{
572589
::: zone-end
573590
::: zone pivot="programming-language-python"
574591

575-
The following example demonstrates how to write a document to an Azure Cosmos DB database as the output of a function.
592+
The following example demonstrates how to write a document to an Azure Cosmos DB database as the output of a function. The example depends on whether you use the [v1 or v2 Python programming model](functions-reference-python.md).
593+
594+
# [v2](#tab/python-v2)
595+
596+
```python
597+
import logging
598+
import azure.functions as func
599+
600+
app = func.FunctionApp()
601+
602+
@app.route()
603+
@app.cosmos_db_output(arg_name="documents",
604+
database_name="DB_NAME",
605+
collection_name="COLLECTION_NAME",
606+
create_if_not_exists=True,
607+
connection_string_setting="CONNECTION_SETTING")
608+
def main(req: func.HttpRequest, documents: func.Out[func.Document]) -> func.HttpResponse:
609+
request_body = req.get_body()
610+
documents.set(func.Document.from_json(request_body))
611+
return 'OK'
612+
```
613+
614+
# [v1](#tab/python-v1)
576615

577616
The binding definition is defined in *function.json* where *type* is set to `cosmosDB`.
578617

@@ -622,6 +661,8 @@ def main(req: func.HttpRequest, doc: func.Out[func.Document]) -> func.HttpRespon
622661
return 'OK'
623662
```
624663

664+
---
665+
625666
::: zone-end
626667
::: zone pivot="programming-language-csharp"
627668
## Attributes
@@ -654,6 +695,25 @@ Both [in-process](functions-dotnet-class-library.md) and [isolated worker proces
654695

655696
---
656697
::: zone-end
698+
699+
::: zone pivot="programming-language-python"
700+
## Decorators
701+
702+
_Applies only to the Python v2 programming model._
703+
704+
For Python v2 functions defined using a decorator, the following properties on the `cosmos_db_output`:
705+
706+
| Property | Description |
707+
|-------------|-----------------------------|
708+
|`arg_name` | The variable name used in function code that represents the list of documents with changes. |
709+
|`database_name` | The name of the Azure Cosmos DB database with the collection being monitored. |
710+
|`collection_name` | The name of the Azure CosmosDB collection being monitored. |
711+
|`create_if_not_exists` | A Boolean value that indicates whether the database and collection should be created if they do not exist. |
712+
|`connection_string_setting` | The connection string of the Azure Cosmos DB being monitored. |
713+
714+
For Python functions defined by using *function.json*, see the [Configuration](#configuration) section.
715+
::: zone-end
716+
657717
::: zone pivot="programming-language-java"
658718
## Annotations
659719

@@ -672,6 +732,13 @@ From the [Java functions runtime library](/java/api/overview/azure/functions/run
672732
::: zone-end
673733
::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python"
674734
## Configuration
735+
::: zone-end
736+
737+
::: zone pivot="programming-language-python"
738+
_Applies only to the Python v1 programming model._
739+
740+
::: zone-end
741+
::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python"
675742

676743
The following table explains the binding configuration properties that you set in the *function.json* file, where properties differ by extension version:
677744

0 commit comments

Comments
 (0)