Skip to content

Commit 414ff9f

Browse files
authored
Add commented out templates for Python SDK-type Bindings (#1668)
* Add commented out templates for Python SDK-type Bindings * Add templates to fusnction_body.py * remove extra parenthesis
1 parent 12f4658 commit 414ff9f

File tree

23 files changed

+248
-14
lines changed

23 files changed

+248
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*.nupkg
55
templates.json
66
bin
7+
.idea
78

89
obj
910
Publish

Functions.Templates/Templates-v2/BlobTrigger-Python/blob_trigger_template.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The Blob storage trigger starts a function when a new or updated blob is detecte
66

77
## Using the Template
88

9-
Following is an example code snippet for Blob Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview).
9+
Following is an example code snippet for Blob Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel).
1010

1111
```python
1212
import logging
@@ -23,13 +23,33 @@ def test_function(myblob: func.InputStream):
2323
f"Blob Size: {myblob.length} bytes")
2424
```
2525

26+
This example uses SDK types to directly access the underlying BlobClient object provided by the Blob storage trigger:
27+
28+
```python
29+
import logging
30+
import azure.functions as func
31+
import azurefunctions.extensions.bindings.blob as blob
32+
33+
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
34+
35+
@app.blob_trigger(arg_name="client", path="samples-workitems/{name}",
36+
connection="BlobStorageConnection")
37+
def blob_trigger(client: blob.BlobClient):
38+
logging.info(
39+
f"Python blob trigger function processed blob \n"
40+
f"Properties: {client.get_blob_properties()}\n"
41+
f"Blob content head: {client.download_blob().read(size=1)}"
42+
)
43+
```
44+
2645
To run the code snippet generated through the command palette, note the following:
2746

2847
- The function application is defined and named `app`.
2948
- Confirm that the parameters within the trigger reflect values that correspond with your storage account.
3049
- The name of the file must be `function_app.py`.
50+
- If you are using SDK-Type Bindings, make sure to include `azurefunctions-extensions-bindings-blob` in your `requirements.txt` file.
3151

32-
Note that Blob input and output bindings are also supported in Azure Functions. To learn more, see [Azure Blob storage bindings overview](https://aka.ms/azure-function-binding-storage-blob)
52+
Note that Blob input and output bindings are also supported in Azure Functions. To learn more, see [Azure Blob storage bindings overview](https://aka.ms/azure-function-binding-storage-blob).
3353

3454
## Programming Model V2
3555

Functions.Templates/Templates-v2/BlobTrigger-Python/function_app.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,16 @@ def $(FUNCTION_NAME_INPUT)(myblob: func.InputStream):
99
logging.info(f"Python blob trigger function processed blob"
1010
f"Name: {myblob.name}"
1111
f"Blob Size: {myblob.length} bytes")
12+
13+
14+
# This example uses SDK types to directly access the underlying BlobClient object provided by the Blob storage trigger.
15+
# To use, uncomment the section below and add azurefunctions-extensions-bindings-blob to your requirements.txt file
16+
# import azurefunctions.extensions.bindings.blob as blob
17+
# @app.blob_trigger(arg_name="client", path="$(PATH_TO_BLOB_INPUT)",
18+
# connection="$(CONNECTION_STRING_INPUT)")
19+
# def $(FUNCTION_NAME_INPUT)(client: blob.BlobClient):
20+
# logging.info(
21+
# f"Python blob trigger function processed blob \n"
22+
# f"Properties: {client.get_blob_properties()}\n"
23+
# f"Blob content head: {client.download_blob().read(size=1)}"
24+
# )

Functions.Templates/Templates-v2/BlobTrigger-Python/function_body.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@ def $(FUNCTION_NAME_INPUT)(myblob: func.InputStream):
55
logging.info(f"Python blob trigger function processed blob"
66
f"Name: {myblob.name}"
77
f"Blob Size: {myblob.length} bytes")
8+
9+
10+
# This example uses SDK types to directly access the underlying BlobClient object provided by the Blob storage trigger.
11+
# To use, uncomment the section below and add azurefunctions-extensions-bindings-blob to your requirements.txt file
12+
# import azurefunctions.extensions.bindings.blob as blob
13+
# @app.blob_trigger(arg_name="client", path="$(PATH_TO_BLOB_INPUT)",
14+
# connection="$(CONNECTION_STRING_INPUT)")
15+
# def $(FUNCTION_NAME_INPUT)(client: blob.BlobClient):
16+
# logging.info(
17+
# f"Python blob trigger function processed blob \n"
18+
# f"Properties: {client.get_blob_properties()}\n"
19+
# f"Blob content head: {client.download_blob().read(size=1)}"
20+
# )

Functions.Templates/Templates-v2/CosmosDbTrigger-Python/cosmosdb_trigger_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The Azure Cosmos DB Trigger uses the Azure Cosmos DB Change Feed to listen for i
66

77
## Using the Template
88

9-
Following is an example code snippet for Cosmos DB Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview).
9+
Following is an example code snippet for Cosmos DB Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel).
1010

1111
```python
1212
import logging

Functions.Templates/Templates-v2/DaprPublishOutputBinding-Python/dapr-publish-output_binding_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ With Dapr output binding, you can invoke external resources. An optional payload
66

77
## Using the Template
88

9-
Following is an example code snippet for Dapr Service Invocation Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview).
9+
Following is an example code snippet for Dapr Service Invocation Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel).
1010

1111
```python
1212
import datetime

Functions.Templates/Templates-v2/DaprServiceInvocationTrigger-Python/dapr_service_invocation_trigger_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Using service invocation, your application can reliably and securely communicate
66

77
## Using the Template
88

9-
Following is an example code snippet for Dapr Service Invocation Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview).
9+
Following is an example code snippet for Dapr Service Invocation Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel).
1010

1111
```python
1212
import json

Functions.Templates/Templates-v2/DaprTopicTrigger-Python/dapr-topic_trigger_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Using `Dapr Topic Trigger`, your azure functions can react to a message publishe
66

77
## Using the Template
88

9-
Following is an example code snippet for Dapr Topic Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview).
9+
Following is an example code snippet for Dapr Topic Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel).
1010

1111
```python
1212
import json
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Azure Functions: Blob Trigger (using Event Grid) in Python
2+
3+
## Blob Trigger (using Event Grid)
4+
5+
The Blob storage trigger starts a function when a new or updated blob is detected. The blob contents are provided as input to the function.
6+
Starting with extension version 5.x+, you can use an Event Grid event subscription on the container, which reduces latency. The Azure Blob storage trigger requires a general-purpose storage account. Storage V2 accounts with hierarchical namespaces are also supported.
7+
8+
## Using the Template
9+
10+
Following is an example code snippet for Blob Trigger (using Event Grid) using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel).
11+
12+
```python
13+
import logging
14+
import azure.functions as func
15+
16+
app = func.FunctionApp()
17+
18+
@app.function_name(name="BlobTrigger1")
19+
@app.blob_trigger(arg_name="myblob", path="samples-workitems/{name}",
20+
source="EventGrid", connection="BlobStorageConnection")
21+
def test_function(myblob: func.InputStream):
22+
logging.info("Python blob trigger (using Event Grid) function processed blob \n"
23+
f"Name: {myblob.name}\n"
24+
f"Blob Size: {myblob.length} bytes")
25+
```
26+
27+
To run the code snippet generated through the command palette, note the following:
28+
29+
- The function application is defined and named `app`.
30+
- Confirm that the parameters within the trigger reflect values that correspond with your storage account.
31+
- The name of the file must be `function_app.py`.
32+
33+
Note that Blob input and output bindings are also supported in Azure Functions. To learn more, see [Azure Blob storage bindings overview](https://aka.ms/azure-function-binding-storage-blob).
34+
To learn more about Blob Triggers using Event Grid, see the [Trigger Azure Functions on blob containers using an event subscription Tutorial](https://learn.microsoft.com/en-us/azure/azure-functions/functions-event-grid-blob-trigger?pivots=programming-language-python)
35+
36+
## Programming Model V2
37+
38+
The new programming model in Azure Functions Python delivers an experience that aligns with Python development principles, and subsequently with commonly used Python frameworks.
39+
40+
The improved programming model requires fewer files than the default model, and specifically eliminates the need for a configuration file (`function.json`). Instead, triggers and bindings are represented in the `function_app.py` file as decorators. Moreover, functions can be logically organized with support for multiple functions to be stored in the same file. Functions within the same function application can also be stored in different files, and be referenced as blueprints.
41+
42+
To learn more about using the new Python programming model for Azure Functions, see the [Azure Functions Python developer guide](https://aka.ms/pythondeveloperguide). Note that in addition to the documentation, [hints](https://aka.ms/functions-python-hints) are available in code editors that support type checking with PYI files.
43+
44+
To learn more about the new programming model for Azure Functions in Python, see [Programming Models in Azure Functions](https://aka.ms/functions-programming-models).

Functions.Templates/Templates-v2/EventGridBlobTrigger-Python/template.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
{
202202
"name": "ShowMarkdownPreview",
203203
"type": "ShowMarkdownPreview",
204-
"filePath" : "blob_trigger_template.md"
204+
"filePath" : "eventgrid_blob_trigger_template.md"
205205
},
206206
{
207207
"name": "readFileContent_BlueprintFile",

Functions.Templates/Templates-v2/EventGridTrigger-Python/eventgrid_trigger_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
The Event Grid function trigger can be used to respond to an event sent by an Event Grid source. You must have an event subscription to the source to receive events. When the function is triggered, it converts the event data into a JSON string which is then logged using the Python logging module.
66

77
## Using the Template
8-
Following is an example code snippet for Event Grid Trigger using the Python programming model V2 (currently in Preview).
8+
Following is an example code snippet for Event Grid Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel).
99
```python
1010
import azure.functions as func
1111
import logging

Functions.Templates/Templates-v2/EventHubTrigger-Python/eventhub_trigger_template.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The Event Hub function trigger can be used to respond to an event sent to an eve
66

77
## Using the Template
88

9-
Following is an example code snippet for Event Hub Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview).
9+
Following is an example code snippet for Event Hub Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel).
1010

1111
```python
1212
import logging
@@ -22,11 +22,31 @@ def test_function(myhub: func.EventHubEvent):
2222
myhub.get_body().decode('utf-8'))
2323
```
2424

25+
This example uses SDK types to directly access the underlying EventData object provided by the Event Hubs trigger (currently in Preview):
26+
27+
```python
28+
import logging
29+
import azure.functions as func
30+
import azurefunctions.extensions.bindings.eventhub as eh
31+
32+
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
33+
34+
@app.event_hub_message_trigger(
35+
arg_name="event", event_hub_name="EVENTHUB_NAME", connection="EventHubConnection"
36+
)
37+
def eventhub_trigger(event: eh.EventData):
38+
logging.info(
39+
"Python EventHub trigger processed an event %s",
40+
event.body_as_str()
41+
)
42+
```
43+
2544
To run the code snippet generated through the command palette, note the following:
2645

2746
- The function application is defined and named `app`.
2847
- Confirm that the parameters within the trigger reflect values that correspond with your storage account.
2948
- The name of the file must be `function_app.py`.
49+
- If you are using SDK-Type Bindings, make sure to include `azurefunctions-extensions-bindings-eventhub` in your `requirements.txt` file.
3050

3151
Note that Event Hub output bindings are also supported in Azure Functions. To learn more, see [Azure Event Hubs trigger and bindings for Azure Functions](https://aka.ms/azure-function-binding-event-hubs)
3252

Functions.Templates/Templates-v2/EventHubTrigger-Python/function_app.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,16 @@
88
def $(FUNCTION_NAME_INPUT)(azeventhub: func.EventHubEvent):
99
logging.info('Python EventHub trigger processed an event: %s',
1010
azeventhub.get_body().decode('utf-8'))
11+
12+
13+
# This example uses SDK types to directly access the underlying EventData object provided by the Event Hubs trigger.
14+
# To use, uncomment the section below and add azurefunctions-extensions-bindings-eventhub to your requirements.txt file
15+
# import azurefunctions.extensions.bindings.eventhub as eh
16+
# @app.event_hub_message_trigger(
17+
# arg_name="event", event_hub_name="$(EVENTHUB_NAME_INPUT)", connection="$(CONNECTION_STRING_INPUT)"
18+
# )
19+
# def $(FUNCTION_NAME_INPUT)(event: eh.EventData):
20+
# logging.info(
21+
# "Python EventHub trigger processed an event %s",
22+
# event.body_as_str()
23+
# )

Functions.Templates/Templates-v2/EventHubTrigger-Python/function_body.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,16 @@
44
def $(FUNCTION_NAME_INPUT)(azeventhub: func.EventHubEvent):
55
logging.info('Python EventHub trigger processed an event: %s',
66
azeventhub.get_body().decode('utf-8'))
7+
8+
9+
# This example uses SDK types to directly access the underlying EventData object provided by the Event Hubs trigger.
10+
# To use, uncomment the section below and add azurefunctions-extensions-bindings-eventhub to your requirements.txt file
11+
# import azurefunctions.extensions.bindings.eventhub as eh
12+
# @app.event_hub_message_trigger(
13+
# arg_name="event", event_hub_name="$(EVENTHUB_NAME_INPUT)", connection="$(CONNECTION_STRING_INPUT)"
14+
# )
15+
# def $(FUNCTION_NAME_INPUT)(event: eh.EventData):
16+
# logging.info(
17+
# "Python EventHub trigger processed an event %s",
18+
# event.body_as_str()
19+
# )

Functions.Templates/Templates-v2/HttpTrigger-Python/http_trigger_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The HTTP trigger lets you invoke a function with an HTTP request. You can use an
66

77
## Using the Template
88

9-
Following is an example code snippet for HTTP Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview).
9+
Following is an example code snippet for HTTP Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel).
1010

1111
```python
1212
import azure.functions as func

Functions.Templates/Templates-v2/QueueTrigger-Python/queue_trigger_template.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The queue storage trigger runs a function as messages are added to Azure Queue s
66

77
## Using the Template
88

9-
Following is an example code snippet for Queue Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview).
9+
Following is an example code snippet for Queue Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel).
1010

1111
```python
1212
import logging

Functions.Templates/Templates-v2/ServiceBusQueueTrigger-Python/function_app.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,17 @@
88
def $(FUNCTION_NAME_INPUT)(azservicebus: func.ServiceBusMessage):
99
logging.info('Python ServiceBus Queue trigger processed a message: %s',
1010
azservicebus.get_body().decode('utf-8'))
11+
12+
13+
# This example uses SDK types to directly access the underlying ServiceBusReceivedMessage object provided by the Service Bus trigger.
14+
# To use, uncomment the section below and add azurefunctions-extensions-bindings-servicebus to your requirements.txt file
15+
# import azurefunctions.extensions.bindings.servicebus as servicebus
16+
# @app.service_bus_queue_trigger(arg_name="receivedmessage",
17+
# queue_name="$(SERVICEBUS_NAME_INPUT)",
18+
# connection="$(CONNECTION_STRING_INPUT)")
19+
# def $(FUNCTION_NAME_INPUT)(receivedmessage: servicebus.ServiceBusReceivedMessage):
20+
# logging.info("Python ServiceBus queue trigger processed message.")
21+
# logging.info("Receiving: %s\n"
22+
# "Body: %s\n",
23+
# receivedmessage,
24+
# receivedmessage.body)

Functions.Templates/Templates-v2/ServiceBusQueueTrigger-Python/function_body.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,17 @@
44
def $(FUNCTION_NAME_INPUT)(azservicebus: func.ServiceBusMessage):
55
logging.info('Python ServiceBus Queue trigger processed a message: %s',
66
azservicebus.get_body().decode('utf-8'))
7+
8+
9+
# This example uses SDK types to directly access the underlying ServiceBusReceivedMessage object provided by the Service Bus trigger.
10+
# To use, uncomment the section below and add azurefunctions-extensions-bindings-servicebus to your requirements.txt file
11+
# import azurefunctions.extensions.bindings.servicebus as servicebus
12+
# @app.service_bus_queue_trigger(arg_name="receivedmessage",
13+
# queue_name="$(SERVICEBUS_NAME_INPUT)",
14+
# connection="$(CONNECTION_STRING_INPUT)")
15+
# def $(FUNCTION_NAME_INPUT)(receivedmessage: servicebus.ServiceBusReceivedMessage):
16+
# logging.info("Python ServiceBus queue trigger processed message.")
17+
# logging.info("Receiving: %s\n"
18+
# "Body: %s\n",
19+
# receivedmessage,
20+
# receivedmessage.body)

Functions.Templates/Templates-v2/ServiceBusQueueTrigger-Python/servicebusqueue_trigger_template.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Use the Service Bus Queue trigger to respond to messages from a Service Bus queu
66

77
## Using the Template
88

9-
Following is an example code snippet for Service Bus Queue Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel) (currently in Preview).
9+
Following is an example code snippet for Service Bus Queue Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel).
1010

1111
```python
1212
import logging
@@ -21,11 +21,28 @@ def test_function(msg: func.ServiceBusMessage):
2121
msg.get_body().decode('utf-8'))
2222
```
2323

24+
This example uses SDK types to directly access the underlying ServiceBusReceivedMessage object provided by the Service Bus trigger (currently in Preview):
25+
26+
```python
27+
import azurefunctions.extensions.bindings.servicebus as servicebus
28+
29+
@app.service_bus_queue_trigger(arg_name="receivedmessage",
30+
queue_name="QUEUE_NAME",
31+
connection="SERVICEBUS_CONNECTION")
32+
def servicebus_queue_trigger(receivedmessage: servicebus.ServiceBusReceivedMessage):
33+
logging.info("Python ServiceBus queue trigger processed message.")
34+
logging.info("Receiving: %s\n"
35+
"Body: %s\n",
36+
receivedmessage,
37+
receivedmessage.body)
38+
```
39+
2440
To run the code snippet generated through the command palette, note the following:
2541

2642
- The function application is defined and named `app`.
2743
- Confirm that the parameters within the trigger reflect values that correspond with your storage account.
2844
- The name of the file must be `function_app.py`.
45+
- If you are using SDK-Type Bindings, make sure to include `azurefunctions-extensions-bindings-servicebus` in your `requirements.txt` file.
2946

3047
Note that Service Bus output bindings are also supported in Azure Functions. To learn more, see [Azure Service Bus bindings for Azure Functions](https://aka.ms/azure-function-binding-service-bus)
3148

Functions.Templates/Templates-v2/ServiceBusTopicTrigger-Python/function_app.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,18 @@
88
def $(FUNCTION_NAME_INPUT)(azservicebus: func.ServiceBusMessage):
99
logging.info('Python ServiceBus Topic trigger processed a message: %s',
1010
azservicebus.get_body().decode('utf-8'))
11+
12+
13+
# This example uses SDK types to directly access the underlying ServiceBusReceivedMessage object provided by the Service Bus trigger.
14+
# To use, uncomment the section below and add azurefunctions-extensions-bindings-servicebus to your requirements.txt file
15+
# import azurefunctions.extensions.bindings.servicebus as servicebus
16+
# @app.service_bus_topic_trigger(arg_name="receivedmessage",
17+
# topic_name="$(SERVICEBUS_NAME_INPUT)",
18+
# connection="$(CONNECTION_STRING_INPUT)",
19+
# subscription_name="$(SERVICEBUS_SUBSCRIPTION_NAME_INPUT)")
20+
# def $(FUNCTION_NAME_INPUT)(receivedmessage: servicebus.ServiceBusReceivedMessage):
21+
# logging.info("Python ServiceBus topic trigger processed message.")
22+
# logging.info("Receiving: %s\n"
23+
# "Body: %s\n",
24+
# receivedmessage,
25+
# receivedmessage.body)

0 commit comments

Comments
 (0)