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
@@ -18,6 +18,23 @@ For information on setup and configuration details, see the [overview](./functio
18
18
> When the collection is [partitioned](../cosmos-db/partitioning-overview.md#logical-partitions), lookup operations must also specify the partition key value.
19
19
>
20
20
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
+
21
38
## Example
22
39
23
40
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
The examples depend on whether you use the [v1 or v2 Python programming model](functions-reference-python.md).
1526
+
1508
1527
### Queue trigger, look up ID from JSON
1509
1528
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
+
deftest_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)
1511
1564
1512
1565
Here's the binding data in the *function.json* file:
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.
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.
### Queue trigger, get multiple docs, using SqlQuery
1669
1741
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.
1671
1743
1672
1744
The queue trigger provides a parameter `departmentId`. A queue message of `{ "departmentId" : "Finance" }` would return all records for the finance department.
1673
1745
1746
+
# [v2](#tab/python-v2)
1747
+
1748
+
No equivalent sample for v2 at this time.
1749
+
1750
+
# [v1](#tab/python-v1)
1751
+
1674
1752
Here's the binding data in the *function.json* file:
1675
1753
1676
1754
```json
@@ -1685,6 +1763,8 @@ Here's the binding data in the *function.json* file:
1685
1763
}
1686
1764
```
1687
1765
1766
+
---
1767
+
1688
1768
::: zone-end
1689
1769
::: zone pivot="programming-language-csharp"
1690
1770
## Attributes
@@ -1718,6 +1798,26 @@ Both [in-process](functions-dotnet-class-library.md) and [isolated worker proces
1718
1798
---
1719
1799
1720
1800
::: 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
+
1721
1821
::: zone pivot="programming-language-java"
1722
1822
## Annotations
1723
1823
@@ -1735,6 +1835,13 @@ From the [Java functions runtime library](/java/api/overview/azure/functions/run
1735
1835
::: zone-end
1736
1836
::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python"
1737
1837
## 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"
1738
1845
1739
1846
The following table explains the binding configuration properties that you set in the *function.json* file, where properties differ by extension version:
@@ -14,6 +14,23 @@ The Azure Cosmos DB output binding lets you write a new document to an Azure Cos
14
14
15
15
For information on setup and configuration details, see the [overview](./functions-bindings-cosmosdb-v2.md).
16
16
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
+
17
34
## Example
18
35
19
36
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
542
559
::: zone-end
543
560
::: zone pivot="programming-language-powershell"
544
561
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.
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).
@@ -654,6 +695,25 @@ Both [in-process](functions-dotnet-class-library.md) and [isolated worker proces
654
695
655
696
---
656
697
::: 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
+
657
717
::: zone pivot="programming-language-java"
658
718
## Annotations
659
719
@@ -672,6 +732,13 @@ From the [Java functions runtime library](/java/api/overview/azure/functions/run
672
732
::: zone-end
673
733
::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python"
674
734
## 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"
675
742
676
743
The following table explains the binding configuration properties that you set in the *function.json* file, where properties differ by extension version:
0 commit comments