Skip to content

Commit 6f3dd20

Browse files
Merge pull request #226387 from vmagelo/function-work
Add Python v2 model to articles.
2 parents d91af99 + 4bc143b commit 6f3dd20

7 files changed

+153
-22
lines changed

articles/azure-functions/functions-add-output-binding-storage-queue-cli.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ For more information on the details of bindings, see [Azure Functions triggers a
5757
With the queue binding defined, you can now update your function to receive the `msg` output parameter and write messages to the queue.
5858

5959
::: zone pivot="programming-language-python"
60-
[!INCLUDE [functions-add-output-binding-python](../../includes/functions-add-output-binding-python.md)]
60+
[!INCLUDE [functions-add-output-binding-python](../../includes/functions-add-output-binding-python-v1-v2.md)]
6161
::: zone-end
6262

6363
::: zone pivot="programming-language-javascript"

articles/azure-functions/functions-add-output-binding-storage-queue-vs-code.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,22 @@ Now, you can add the storage output binding to your project.
8989

9090
## Add an output binding
9191

92-
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.
9392

94-
::: zone pivot="programming-language-javascript,programming-language-typescript,programming-language-python,programming-language-powershell"
93+
::: zone pivot="programming-language-javascript,programming-language-typescript,programming-language-powershell"
94+
95+
In Functions, each type of binding requires a `direction`, `type`, and unique `name`. The way you define these attributes depends on the language of your function app.
9596

9697
[!INCLUDE [functions-add-output-binding-json](../../includes/functions-add-output-binding-json.md)]
9798

9899
::: zone-end
99100

101+
::: zone pivot="programming-language-python"
102+
In Functions, each type of binding requires a `direction`, `type`, and a unique `name`. The way you define these attributes depends on your Python programming model.
103+
104+
[!INCLUDE [functions-add-storage-binding-python](../../includes/functions-add-storage-binding-python.md)]
105+
106+
::: zone-end
107+
100108
::: zone pivot="programming-language-csharp"
101109

102110
[!INCLUDE [functions-add-storage-binding-csharp-library](../../includes/functions-add-storage-binding-csharp-library.md)]
@@ -129,7 +137,7 @@ After the binding is defined, you can use the `name` of the binding to access it
129137

130138
::: zone pivot="programming-language-python"
131139

132-
[!INCLUDE [functions-add-output-binding-python](../../includes/functions-add-output-binding-python.md)]
140+
[!INCLUDE [functions-add-output-binding-python](../../includes/functions-add-output-binding-python-v1-v2.md)]
133141

134142
::: zone-end
135143

includes/functions-add-output-binding-cli.md

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
author: ggailey777
33
ms.service: azure-functions
44
ms.topic: include
5-
ms.date: 06/10/2022
5+
ms.date: 02/09/2023
66
ms.author: glenga
77
ms.custom: devdivchpfy22
88
---
@@ -12,25 +12,69 @@ ms.custom: devdivchpfy22
1212

1313
Although a function can have only one trigger, it can have multiple input and output bindings, which lets you connect to other Azure services and resources without writing custom integration code.
1414
::: zone-end
15-
::: zone pivot="programming-language-python,programming-language-javascript,programming-language-powershell,programming-language-typescript"
15+
::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-typescript"
16+
1617
You declare these bindings in the *function.json* file in your function folder. From the previous quickstart, your *function.json* file in the *HttpExample* folder contains two bindings in the `bindings` collection:
18+
19+
::: zone-end
20+
21+
::: zone pivot="programming-language-python"
22+
23+
The way you declare binding attributes depends on your Python programming model.
24+
25+
# [v1](#tab/v1)
26+
27+
You declare these bindings in the *function.json* file in your function folder. From the previous quickstart, your *function.json* file in the *HttpExample* folder contains two bindings in the `bindings` collection:
28+
29+
:::code language="json" source="~/functions-quickstart-templates/Functions.Templates/Templates/HttpTrigger-Python/function.json" range="2-18":::
30+
31+
Each binding has at least a type, a direction, and a name. In the above example, the first binding is of type `httpTrigger` with the direction `in`. For the `in` direction, `name` specifies the name of an input parameter that's sent to the function when invoked by the trigger.
32+
33+
The second binding in the collection is of type `http` with the direction `out`, in which case the special `name` of `$return` indicates that this binding uses the function's return value rather than providing an input parameter.
34+
35+
To write to an Azure Storage queue from this function, add an `out` binding of type `queue` with the name `msg`, as shown in the code below:
36+
37+
:::code language="json" source="~/functions-docs-python/functions-add-output-binding-storage-queue-cli/HttpExample/function.json" range="3-26":::
38+
39+
In this case, `msg` is given to the function as an output argument. For a `queue` type, you must also specify the name of the queue in `queueName` and provide the *name* of the Azure Storage connection (from *local.settings.json* file) in `connection`.
40+
41+
# [v2](#tab/v2)
42+
43+
Binding attributes are defined directly in the *function_app.py* file as decorators. From the previous quickstart, your *function_app.py* file already contains one decorator-based binding:
44+
45+
```python
46+
import azure.functions as func
47+
import logging
48+
49+
app = func.FunctionApp()
50+
51+
@app.function_name(name="HttpTrigger1")
52+
@app.route(route="hello", auth_level=func.AuthLevel.ANONYMOUS)
53+
```
54+
55+
The `route` decorator adds HttpTrigger and HttpOutput binding to the function, which enables your function be triggered when http requests hit the specified route.
56+
57+
To write to an Azure Storage queue from this function, add the `queue_output` decorator to your function code:
58+
59+
```python
60+
@app.queue_output(arg_name="msg", queue_name="outqueue", connection="AzureWebJobsStorage")
61+
```
62+
63+
In the decorator, `arg_name` identifies the binding parameter referenced in your code, `queue_name` is name of the queue that the binding writes to, and `connection` is the name of an application setting that contains the connection string for the Storage account. In quickstarts you use the same storage account as the function app, which is in the `AzureWebJobsStorage` setting (from *local.settings.json* file). When the `queue_name` doesn't exist, the binding creates it on first use.
64+
65+
---
66+
1767
::: zone-end
1868

1969
::: zone pivot="programming-language-javascript,programming-language-typescript"
2070
:::code language="json" source="~/functions-quickstart-templates/Functions.Templates/Templates/HttpTrigger-JavaScript/function.json" range="2-18":::
2171
::: zone-end
2272

23-
::: zone pivot="programming-language-python"
24-
:::code language="json" source="~/functions-quickstart-templates/Functions.Templates/Templates/HttpTrigger-Python/function.json" range="2-18":::
25-
::: zone-end
2673

2774
::: zone pivot="programming-language-powershell"
2875
:::code language="json" source="~/functions-quickstart-templates/Functions.Templates/Templates/HttpTrigger-PowerShell/function.json" range="2-18":::
2976
::: zone-end
3077

31-
::: zone pivot="programming-language-python,programming-language-javascript, programming-language-powershell, programming-language-typescript"
32-
Each binding has at least a type, a direction, and a name. In the above example, the first binding is of type `httpTrigger` with the direction `in`. For the `in` direction, `name` specifies the name of an input parameter that's sent to the function when invoked by the trigger.
33-
::: zone-end
3478

3579
::: zone pivot="programming-language-javascript,programming-language-typescript"
3680
The second binding in the collection is named `res`. This `http` binding is an output binding (`out`) that is used to write the HTTP response.
@@ -40,13 +84,6 @@ To write to an Azure Storage queue from this function, add an `out` binding of t
4084
:::code language="json" source="~/functions-docs-javascript/functions-add-output-binding-storage-queue-cli/HttpExample/function.json" range="3-26":::
4185
::: zone-end
4286

43-
::: zone pivot="programming-language-python"
44-
The second binding in the collection is of type `http` with the direction `out`, in which case the special `name` of `$return` indicates that this binding uses the function's return value rather than providing an input parameter.
45-
46-
To write to an Azure Storage queue from this function, add an `out` binding of type `queue` with the name `msg`, as shown in the code below:
47-
48-
:::code language="json" source="~/functions-docs-python/functions-add-output-binding-storage-queue-cli/HttpExample/function.json" range="3-26":::
49-
::: zone-end
5087

5188
::: zone pivot="programming-language-powershell"
5289
The second binding in the collection is named `res`. This `http` binding is an output binding (`out`) that is used to write the HTTP response.
@@ -56,6 +93,6 @@ To write to an Azure Storage queue from this function, add an `out` binding of t
5693
:::code language="json" source="~/functions-docs-powershell/functions-add-output-binding-storage-queue-cli/HttpExample/function.json" range="3-26":::
5794
::: zone-end
5895

59-
::: zone pivot="programming-language-python,programming-language-javascript,programming-language-powershell,programming-language-typescript"
96+
::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-typescript"
6097
In this case, `msg` is given to the function as an output argument. For a `queue` type, you must also specify the name of the queue in `queueName` and provide the *name* of the Azure Storage connection (from *local.settings.json* file) in `connection`.
6198
::: zone-end

includes/functions-add-output-binding-json.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.date: 09/23/2019
66
ms.author: glenga
77
---
88

9-
Binding attributes are defined directly in the function.json file. Depending on the binding type, additional properties may be required. The [queue output configuration](../articles/azure-functions/functions-bindings-storage-queue-output.md#configuration) describes the fields required for an Azure Storage queue binding. The extension makes it easy to add bindings to the function.json file.
9+
Binding attributes are defined in the *function.json* file for a given function. Depending on the binding type, additional properties may be required. The [queue output configuration](../articles/azure-functions/functions-bindings-storage-queue-output.md#configuration) describes the fields required for an Azure Storage queue binding. The extension makes it easy to add bindings to the *function.json* file.
1010

1111
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:
1212

@@ -18,6 +18,6 @@ To create a binding, right-click (Ctrl+click on macOS) the `function.json` file
1818
| **The queue to which the message will be sent** | `outqueue` | The name of the queue that the binding writes to. When the *queueName* doesn't exist, the binding creates it on first use. |
1919
| **Select setting from "local.setting.json"** | `AzureWebJobsStorage` | The name of an application setting that contains the connection string for the Storage account. The `AzureWebJobsStorage` setting contains the connection string for the Storage account you created with the function app. |
2020

21-
A binding is added to the `bindings` array in your function.json, which should look like the following:
21+
A binding is added to the `bindings` array in your *function.json*, which should look like the following:
2222

2323
:::code language="json" source="~/functions-docs-javascript/functions-add-output-binding-storage-queue-cli/HttpExample/function.json" range="18-24":::
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
author: ggailey777
3+
ms.service: azure-functions
4+
ms.topic: include
5+
ms.date: 02/06/2023
6+
ms.author: glenga
7+
ms.custom: devdivchpfy22
8+
---
9+
10+
# [v1](#tab/v1)
11+
12+
[!INCLUDE [functions-add-output-binding-python](./functions-add-output-binding-python.md)]
13+
14+
# [v2](#tab/v2)
15+
16+
Update *HttpExample\\function_app.py* to match the following code, add the `msg` parameter to the function definition and `msg.set(name)` under the `if name:` statement:
17+
18+
```python
19+
import azure.functions as func
20+
import logging
21+
22+
app = func.FunctionApp()
23+
24+
@app.function_name(name="HttpTrigger1")
25+
@app.route(route="hello", auth_level=func.AuthLevel.ANONYMOUS)
26+
@app.queue_output(arg_name="msg", queue_name="outqueue", connection="AzureWebJobsStorage")
27+
def test_function(req: func.HttpRequest, msg: func.Out[func.QueueMessage]) -> func.HttpResponse:
28+
logging.info('Python HTTP trigger function processed a request.')
29+
30+
name = req.params.get('name')
31+
if not name:
32+
try:
33+
req_body = req.get_json()
34+
except ValueError:
35+
pass
36+
else:
37+
name = req_body.get('name')
38+
39+
if name:
40+
msg.set(name)
41+
return func.HttpResponse(f"Hello {name}!")
42+
else:
43+
return func.HttpResponse(
44+
"Please pass a name on the query string or in the request body",
45+
status_code=400
46+
)
47+
```
48+
49+
The `msg` parameter is an instance of the [`azure.functions.Out class`](/python/api/azure-functions/azure.functions.out). The `set` method writes a string message to the queue. In this case, it's the name passed to the function in the URL query string.
50+
51+
---
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
author: ggailey777
3+
ms.service: azure-functions
4+
ms.topic: include
5+
ms.date: 02/06/2023
6+
ms.author: glenga
7+
---
8+
9+
Binding attributes are defined by decorating specific function code in the *function_app.py* file. You use the `queue_output` decorator to add an [Azure Queue storage output binding](/azure/azure-functions/functions-bindings-triggers-python#azure-queue-storage-output-binding).
10+
11+
By using the `queue_output` decorator, the binding direction is implicitly 'out' and type is Azure Storage Queue. Add the following decorator to your function code in *function_app.py*:
12+
13+
```python
14+
@app.queue_output(arg_name="msg", queue_name="outqueue", connection="AzureWebJobsStorage")
15+
```
16+
17+
In this code, `arg_name` identifies the binding parameter referenced in your code, `queue_name` is name of the queue that the binding writes to, and `connection` is the name of an application setting that contains the connection string for the Storage account. In quickstarts you use the same storage account as the function app, which is in the `AzureWebJobsStorage` setting. When the `queue_name` doesn't exist, the binding creates it on first use.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
author: ggailey777
3+
ms.service: azure-functions
4+
ms.topic: include
5+
ms.date: 02/09/2023
6+
ms.author: glenga
7+
ms.custom: devdivchpfy22
8+
---
9+
10+
# [v1](#tab/v1)
11+
12+
[!INCLUDE [functions-add-output-binding-json](./functions-add-output-binding-json.md)]
13+
14+
# [v2](#tab/v2)
15+
16+
[!INCLUDE [functions-add-output-binding-python-v2](./functions-add-output-binding-python-v2.md)]
17+
18+
---

0 commit comments

Comments
 (0)