Skip to content

Commit a8ac167

Browse files
Merge pull request #300425 from hallvictoria/hallvictoria/python-updates
Adding Azure Functions Python samples
2 parents 9e9f94d + 39fa234 commit a8ac167

16 files changed

+228
-74
lines changed

articles/azure-functions/functions-bindings-azure-sql-input.md

Lines changed: 34 additions & 35 deletions
Large diffs are not rendered by default.

articles/azure-functions/functions-bindings-azure-sql-output.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ The examples refer to a `ToDoItem` class and a corresponding database table:
4444

4545
:::code language="sql" source="~/functions-sql-todo-sample/sql/create.sql" range="1-7":::
4646

47-
To return [multiple output bindings](./dotnet-isolated-process-guide.md#multiple-output-bindings) in our samples, we'll create a custom return type:
47+
To return [multiple output bindings](./dotnet-isolated-process-guide.md#multiple-output-bindings) in our samples, we create a custom return type:
4848

4949
```cs
5050
public static class OutputType
@@ -59,7 +59,7 @@ public static class OutputType
5959

6060
### HTTP trigger, write one record
6161

62-
The following example shows a [C# function](functions-dotnet-class-library.md) that adds a record to a database, using data provided in an HTTP POST request as a JSON body. The return object is the `OutputType` class we created to handle both an HTTP response and the SQL output binding.
62+
The following example shows a [C# function](functions-dotnet-class-library.md) that adds a record to a database, using data provided in an HTTP POST request as a JSON body. The return object is the `OutputType` class we created to handle both an HTTP response and the SQL output binding.
6363

6464
```cs
6565
using System;
@@ -133,7 +133,7 @@ CREATE TABLE dbo.RequestLog (
133133
)
134134
```
135135

136-
To use an additional output binding, we add a class for `RequestLog` and modify our `OutputType` class:
136+
To use an extra output binding, we add a class for `RequestLog` and modify our `OutputType` class:
137137

138138
```cs
139139
using System;
@@ -378,7 +378,7 @@ public class ToDoItem {
378378
<a id="http-trigger-write-record-to-table-java"></a>
379379
### HTTP trigger, write a record to a table
380380

381-
The following example shows a SQL output binding in a Java function that adds a record to a table, using data provided in an HTTP POST request as a JSON body. The function takes an additional dependency on the [com.google.code.gson](https://github.com/google/gson) library to parse the JSON body.
381+
The following example shows a SQL output binding in a Java function that adds a record to a table, using data provided in an HTTP POST request as a JSON body. The function takes another dependency on the [com.google.code.gson](https://github.com/google/gson) library to parse the JSON body.
382382

383383
```xml
384384
<dependency>
@@ -423,7 +423,7 @@ public class PostToDo {
423423
<a id="http-trigger-write-to-two-tables-java"></a>
424424
### HTTP trigger, write to two tables
425425

426-
The following example shows a SQL output binding in a JavaS function that adds records to a database in two different tables (`dbo.ToDo` and `dbo.RequestLog`), using data provided in an HTTP POST request as a JSON body and multiple output bindings. The function takes an additional dependency on the [com.google.code.gson](https://github.com/google/gson) library to parse the JSON body.
426+
The following example shows a SQL output binding in a JavaS function that adds records to a database in two different tables (`dbo.ToDo` and `dbo.RequestLog`), using data provided in an HTTP POST request as a JSON body and multiple output bindings. The function takes another dependency on the [com.google.code.gson](https://github.com/google/gson) library to parse the JSON body.
427427

428428
```xml
429429
<dependency>
@@ -868,7 +868,6 @@ The following is sample python code for the function_app.py file:
868868
import json
869869
import logging
870870
import azure.functions as func
871-
from azure.functions.decorators.core import DataType
872871

873872
app = func.FunctionApp()
874873

@@ -1099,7 +1098,7 @@ The [C# library](functions-dotnet-class-library.md) uses the [SqlAttribute](http
10991098
| Attribute property |Description|
11001099
|---------|---------|
11011100
| **CommandText** | Required. The name of the table being written to by the binding. |
1102-
| **ConnectionStringSetting** | Required. The name of an app setting that contains the connection string for the database to which data is being written. This isn't the actual connection string and must instead resolve to an environment variable. |
1101+
| **ConnectionStringSetting** | Required. The name of an app setting that contains the connection string for the database to which data is being written. This value isn't the actual connection string and must instead resolve to an environment variable. |
11031102

11041103

11051104
::: zone-end
@@ -1111,8 +1110,8 @@ In the [Java functions runtime library](/java/api/overview/azure/functions/runti
11111110

11121111
| Element |Description|
11131112
|---------|---------|
1114-
| **commandText** | Required. The name of the table being written to by the binding. |
1115-
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database to which data is being written. This isn't the actual connection string and must instead resolve to an environment variable.|
1113+
| **commandText** | Required. The name of the table being written to by the binding. |
1114+
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database to which data is being written. This value isn't the actual connection string and must instead resolve to an environment variable.|
11161115
|**name** | Required. The unique name of the function binding. |
11171116

11181117
::: zone-end
@@ -1127,7 +1126,7 @@ The following table explains the properties that you can set on the `options` ob
11271126
| Property | Description |
11281127
|---------|----------------------|
11291128
| **commandText** | Required. The name of the table being written to by the binding. |
1130-
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database to which data is being written. This isn't the actual connection string and must instead resolve to an environment variable. Optional keywords in the connection string value are [available to refine SQL bindings connectivity](./functions-bindings-azure-sql.md#sql-connection-string). |
1129+
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database to which data is being written. This value isn't the actual connection string and must instead resolve to an environment variable. Optional keywords in the connection string value are [available to refine SQL bindings connectivity](./functions-bindings-azure-sql.md#sql-connection-string). |
11311130

11321131
# [Model v3](#tab/nodejs-v3)
11331132

@@ -1139,7 +1138,7 @@ The following table explains the binding configuration properties that you set i
11391138
|**direction** | Required. Must be set to `out`. |
11401139
|**name** | Required. The name of the variable that represents the entity in function code. |
11411140
| **commandText** | Required. The name of the table being written to by the binding. |
1142-
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database to which data is being written. This isn't the actual connection string and must instead resolve to an environment variable. Optional keywords in the connection string value are [available to refine SQL bindings connectivity](./functions-bindings-azure-sql.md#sql-connection-string). |
1141+
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database to which data is being written. This value isn't the actual connection string and must instead resolve to an environment variable. Optional keywords in the connection string value are [available to refine SQL bindings connectivity](./functions-bindings-azure-sql.md#sql-connection-string). |
11431142

11441143
---
11451144

@@ -1155,7 +1154,7 @@ The following table explains the binding configuration properties that you set i
11551154
|**direction** | Required. Must be set to `out`. |
11561155
|**name** | Required. The name of the variable that represents the entity in function code. |
11571156
| **commandText** | Required. The name of the table being written to by the binding. |
1158-
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database to which data is being written. This isn't the actual connection string and must instead resolve to an environment variable. Optional keywords in the connection string value are [available to refine SQL bindings connectivity](./functions-bindings-azure-sql.md#sql-connection-string). |
1157+
| **connectionStringSetting** | Required. The name of an app setting that contains the connection string for the database to which data is being written. This value isn't the actual connection string and must instead resolve to an environment variable. Optional keywords in the connection string value are [available to refine SQL bindings connectivity](./functions-bindings-azure-sql.md#sql-connection-string). |
11591158

11601159
::: zone-end
11611160

@@ -1169,7 +1168,7 @@ The `CommandText` property is the name of the table where the data is to be stor
11691168

11701169
The output bindings use the T-SQL [MERGE](/sql/t-sql/statements/merge-transact-sql) statement which requires [SELECT](/sql/t-sql/statements/merge-transact-sql#permissions) permissions on the target database.
11711170

1172-
If an exception occurs when a SQL output binding is executed then the function code stop executing. This may result in an error code being returned, such as an HTTP trigger returning a 500 error code. If the `IAsyncCollector` is used in a .NET function then the function code can handle exceptions throw by the call to `FlushAsync()`.
1171+
If an exception occurs when a SQL output binding is executed, then the function code stops executing. This behavior may result in an error code being returned, such as an HTTP trigger returning a 500 error code. If the `IAsyncCollector` is used in a .NET function, then the function code can handle exceptions throw by the call to `FlushAsync()`.
11731172

11741173
## Next steps
11751174

articles/azure-functions/functions-bindings-azure-sql-trigger.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,6 @@ The following is sample python code for the function_app.py file:
444444
import json
445445
import logging
446446
import azure.functions as func
447-
from azure.functions.decorators.core import DataType
448447

449448
app = func.FunctionApp()
450449

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ namespace CosmosDBSamplesV2
102102

103103
### Queue trigger, write one doc (v4 extension)
104104

105-
Apps using [Azure Cosmos DB extension version 4.x](./functions-bindings-cosmosdb-v2.md?tabs=extensionv4) or higher will have different attribute properties which are shown below. The following example shows a [C# function](functions-dotnet-class-library.md) that adds a document to a database, using data provided in message from Queue storage.
105+
Apps using [Azure Cosmos DB extension version 4.x](./functions-bindings-cosmosdb-v2.md?tabs=extensionv4) or higher have different attribute properties which are shown below. The following example shows a [C# function](functions-dotnet-class-library.md) that adds a document to a database, using data provided in message from Queue storage.
106106

107107
```cs
108108
using Microsoft.Azure.WebJobs;
@@ -206,7 +206,7 @@ public String cosmosDbQueryById(
206206

207207
#### HTTP trigger, save one document to database via return value
208208

209-
The following example shows a Java function whose signature is annotated with `@CosmosDBOutput` and has return value of type `String`. The JSON document returned by the function will be automatically written to the corresponding Azure Cosmos DB collection.
209+
The following example shows a Java function whose signature is annotated with `@CosmosDBOutput` and has return value of type `String`. The JSON document returned by the function is automatically written to the corresponding Azure Cosmos DB collection.
210210

211211
```java
212212
@FunctionName("WriteOneDoc")
@@ -338,7 +338,7 @@ The following example shows a Java function that writes multiple documents to Az
338338
}
339339
```
340340

341-
In the [Java functions runtime library](/java/api/overview/azure/functions/runtime), use the `@CosmosDBOutput` annotation on parameters that will be written to Azure Cosmos DB. The annotation parameter type should be `OutputBinding<T>`, where `T` is either a native Java type or a POJO.
341+
In the [Java functions runtime library](/java/api/overview/azure/functions/runtime), use the `@CosmosDBOutput` annotation on parameters that is written to Azure Cosmos DB. The annotation parameter type should be `OutputBinding<T>`, where `T` is either a native Java type or a POJO.
342342

343343
::: zone-end
344344
::: zone pivot="programming-language-typescript"
@@ -467,7 +467,7 @@ Here's the JavaScript code:
467467
};
468468
```
469469

470-
For bulk insert form the objects first and then run the stringify function. Here's the JavaScript code:
470+
For bulk insert form the objects first and then, run the stringify function. Here's the JavaScript code:
471471

472472
```javascript
473473
module.exports = async function (context) {
@@ -705,6 +705,12 @@ By default, when you write to the output parameter in your function, a document
705705
> [!NOTE]
706706
> When you specify the ID of an existing document, it gets overwritten by the new output document.
707707
708+
::: zone pivot="programming-language-python"
709+
710+
The output function parameter must be defined as `func.Out[func.Document]`. Refer to the [output example](#example) for details.
711+
712+
::: zone-end
713+
708714
::: zone pivot="programming-language-csharp"
709715

710716
The parameter type supported by the Cosmos DB output binding depends on the Functions runtime version, the extension package version, and the C# modality used.

articles/azure-functions/functions-bindings-event-grid-output.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,9 @@ There are two options for outputting an Event Grid message from a function:
693693
- **Return value**: Set the `name` property in *function.json* to `$return`. With this configuration, the function's return value is persisted as an Event Grid message.
694694
- **Imperative**: Pass a value to the [set](/python/api/azure-functions/azure.functions.out#set-val--t-----none) method of the parameter declared as an [Out](/python/api/azure-functions/azure.functions.out) type. The value passed to `set` is persisted as an Event Grid message.
695695

696+
697+
The output function parameter must be defined as `func.Out[str]`, `func.Out[bytes]`, `func.Out[func.EventGridOutputEvent]`, or `func.Out[List[func.EventGridOutputEvent]]`. Refer to the [output example](#example) for details.
698+
696699
::: zone-end
697700

698701
## Connections

articles/azure-functions/functions-bindings-event-hubs-output.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ There are two options for outputting an Event Hubs message from a function:
458458

459459
- **Imperative**: Pass a value to the [set](/python/api/azure-functions/azure.functions.out#set-val--t-----none) method of the parameter declared as an [Out](/python/api/azure-functions/azure.functions.out) type. The value passed to `set` is persisted as an Event Hubs message.
460460

461+
The output function parameter must be defined as `func.Out[func.EventHubEvent]` or `func.Out[List[func.EventHubEvent]]`. Refer to the [output example](#example) for details.
462+
461463
::: zone-end
462464

463465
[!INCLUDE [functions-event-hubs-connections](../../includes/functions-event-hubs-connections.md)]

articles/azure-functions/functions-bindings-service-bus-output.md

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,10 @@ Push-OutputBinding -Name outputSbMsg -Value @{
269269
::: zone-end
270270
::: zone pivot="programming-language-python"
271271

272-
The following example demonstrates how to write out to a Service Bus queue in Python. The example depends on whether you use the [v1 or v2 Python programming model](functions-reference-python.md).
272+
The following example demonstrates how to write out to a Service Bus topics and Service Bus queues in Python. The example depends on whether you use the [v1 or v2 Python programming model](functions-reference-python.md).
273273

274274
# [v2](#tab/python-v2)
275+
This example shows how to write out to a Service Bus topic.
275276

276277
```python
277278
import logging
@@ -281,17 +282,33 @@ app = func.FunctionApp()
281282

282283
@app.route(route="put_message")
283284
@app.service_bus_topic_output(arg_name="message",
284-
connection="<CONNECTION_SETTING>",
285-
topic_name="<TOPIC_NAME>")
285+
connection="AzureServiceBusConnectionString",
286+
topic_name="outTopic")
286287
def main(req: func.HttpRequest, message: func.Out[str]) -> func.HttpResponse:
287288
input_msg = req.params.get('message')
288289
message.set(input_msg)
289290
return 'OK'
290291
```
291292

292-
# [v1](#tab/python-v1)
293+
This example shows how to write out to a Service Bus queue.
294+
295+
```python
296+
import azure.functions as func
297+
298+
app = func.FunctionApp()
299+
300+
@app.route(route="put_message")
301+
@app.service_bus_queue_output(
302+
arg_name="msg",
303+
connection="AzureServiceBusConnectionString",
304+
queue_name="outqueue")
305+
def put_message(req: func.HttpRequest, msg: func.Out[str]):
306+
msg.set(req.get_body().decode('utf-8'))
307+
return 'OK'
308+
```
293309

294-
A Service Bus binding definition is defined in *function.json* where *type* is set to `serviceBus`.
310+
# [v1](#tab/python-v1)
311+
A Service Bus binding definition is defined in *function.json* where *type* is set to `serviceBus`. This example shows how to write out to a Service Bus topic.
295312

296313
```json
297314
{
@@ -317,7 +334,7 @@ A Service Bus binding definition is defined in *function.json* where *type* is s
317334
"direction": "out",
318335
"connection": "AzureServiceBusConnectionString",
319336
"name": "msg",
320-
"queueName": "outqueue"
337+
"topicName": "outTopic"
321338
}
322339
]
323340
}
@@ -328,6 +345,47 @@ In *_\_init_\_.py*, you can write out a message to the queue by passing a value
328345
```python
329346
import azure.functions as func
330347

348+
def main(req: azf.HttpRequest, msg: azf.Out[str]):
349+
msg.set(req.get_body().decode('utf-8'))
350+
351+
return 'OK'
352+
```
353+
354+
This example shows how to write out to a Service Bus queue.
355+
356+
```json
357+
{
358+
"scriptFile": "__init__.py",
359+
"bindings": [
360+
{
361+
"authLevel": "function",
362+
"type": "httpTrigger",
363+
"direction": "in",
364+
"name": "req",
365+
"methods": [
366+
"get",
367+
"post"
368+
]
369+
},
370+
{
371+
"type": "http",
372+
"direction": "out",
373+
"name": "$return"
374+
},
375+
{
376+
"type": "serviceBus",
377+
"direction": "out",
378+
"connection": "AzureServiceBusConnectionString",
379+
"name": "msg",
380+
"queueName": "outqueue"
381+
}
382+
]
383+
}
384+
```
385+
386+
```python
387+
import azure.functions as func
388+
331389
def main(req: func.HttpRequest, msg: func.Out[str]) -> func.HttpResponse:
332390

333391
input_msg = req.params.get('message')
@@ -564,8 +622,9 @@ Access the output message by using `context.bindings.<name>` where `<name>` is t
564622
::: zone pivot="programming-language-powershell"
565623
Output to the Service Bus is available via the `Push-OutputBinding` cmdlet where you pass arguments that match the name designated by binding's name parameter in the *function.json* file.
566624
::: zone-end
567-
::: zone pivot="programming-language-python"
568-
Use the [Azure Service Bus SDK](../service-bus-messaging/index.yml) rather than the built-in output binding.
625+
::: zone pivot="programming-language-python"
626+
The output function parameter must be defined as `func.Out[str]` or `func.Out[bytes]`. Refer to the [output example](#example) for details.
627+
Alternatively, you can use the [Azure Service Bus SDK](../service-bus-messaging/index.yml) rather than the built-in output binding.
569628
::: zone-end
570629
For a complete example, see [the examples section](#example).
571630

0 commit comments

Comments
 (0)