Skip to content

Add commented out templates for Python SDK-type Bindings #1668

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*.nupkg
templates.json
bin
.idea

obj
Publish
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The Blob storage trigger starts a function when a new or updated blob is detecte

## Using the Template

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

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

This example uses SDK types to directly access the underlying BlobClient object provided by the Blob storage trigger:

```python
import logging
import azure.functions as func
import azurefunctions.extensions.bindings.blob as blob

app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)

@app.blob_trigger(arg_name="client", path="samples-workitems/{name}",
connection="BlobStorageConnection")
def blob_trigger(client: blob.BlobClient):
logging.info(
f"Python blob trigger function processed blob \n"
f"Properties: {client.get_blob_properties()}\n"
f"Blob content head: {client.download_blob().read(size=1)}"
)
```

To run the code snippet generated through the command palette, note the following:

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

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)
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).

## Programming Model V2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,16 @@ def $(FUNCTION_NAME_INPUT)(myblob: func.InputStream):
logging.info(f"Python blob trigger function processed blob"
f"Name: {myblob.name}"
f"Blob Size: {myblob.length} bytes")


# This example uses SDK types to directly access the underlying BlobClient object provided by the Blob storage trigger.
# To use, uncomment the section below and add azurefunctions-extensions-bindings-blob to your requirements.txt file
# import azurefunctions.extensions.bindings.blob as blob
# @app.blob_trigger(arg_name="client", path="$(PATH_TO_BLOB_INPUT)",
# connection="$(CONNECTION_STRING_INPUT)")
# def $(FUNCTION_NAME_INPUT)(client: blob.BlobClient):
# logging.info(
# f"Python blob trigger function processed blob \n"
# f"Properties: {client.get_blob_properties()}\n"
# f"Blob content head: {client.download_blob().read(size=1)}"
# )
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@ def $(FUNCTION_NAME_INPUT)(myblob: func.InputStream):
logging.info(f"Python blob trigger function processed blob"
f"Name: {myblob.name}"
f"Blob Size: {myblob.length} bytes")


# This example uses SDK types to directly access the underlying BlobClient object provided by the Blob storage trigger.
# To use, uncomment the section below and add azurefunctions-extensions-bindings-blob to your requirements.txt file
# import azurefunctions.extensions.bindings.blob as blob
# @app.blob_trigger(arg_name="client", path="$(PATH_TO_BLOB_INPUT)",
# connection="$(CONNECTION_STRING_INPUT)")
# def $(FUNCTION_NAME_INPUT)(client: blob.BlobClient):
# logging.info(
# f"Python blob trigger function processed blob \n"
# f"Properties: {client.get_blob_properties()}\n"
# f"Blob content head: {client.download_blob().read(size=1)}"
# )
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The Azure Cosmos DB Trigger uses the Azure Cosmos DB Change Feed to listen for i

## Using the Template

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

```python
import logging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ With Dapr output binding, you can invoke external resources. An optional payload

## Using the Template

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

```python
import datetime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Using service invocation, your application can reliably and securely communicate

## Using the Template

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

```python
import json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Using `Dapr Topic Trigger`, your azure functions can react to a message publishe

## Using the Template

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

```python
import json
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Azure Functions: Blob Trigger (using Event Grid) in Python

## Blob Trigger (using Event Grid)

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

## Using the Template

Following is an example code snippet for Blob Trigger (using Event Grid) using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel).

```python
import logging
import azure.functions as func

app = func.FunctionApp()

@app.function_name(name="BlobTrigger1")
@app.blob_trigger(arg_name="myblob", path="samples-workitems/{name}",
source="EventGrid", connection="BlobStorageConnection")
def test_function(myblob: func.InputStream):
logging.info("Python blob trigger (using Event Grid) function processed blob \n"
f"Name: {myblob.name}\n"
f"Blob Size: {myblob.length} bytes")
```

To run the code snippet generated through the command palette, note the following:

- The function application is defined and named `app`.
- Confirm that the parameters within the trigger reflect values that correspond with your storage account.
- The name of the file must be `function_app.py`.

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).
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)

## Programming Model V2

The new programming model in Azure Functions Python delivers an experience that aligns with Python development principles, and subsequently with commonly used Python frameworks.

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.

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.

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).
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@
{
"name": "ShowMarkdownPreview",
"type": "ShowMarkdownPreview",
"filePath" : "blob_trigger_template.md"
"filePath" : "eventgrid_blob_trigger_template.md"
},
{
"name": "readFileContent_BlueprintFile",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
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.

## Using the Template
Following is an example code snippet for Event Grid Trigger using the Python programming model V2 (currently in Preview).
Following is an example code snippet for Event Grid Trigger using the [Python programming model V2](https://aka.ms/pythonprogrammingmodel).
```python
import azure.functions as func
import logging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The Event Hub function trigger can be used to respond to an event sent to an eve

## Using the Template

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

```python
import logging
Expand All @@ -22,11 +22,31 @@ def test_function(myhub: func.EventHubEvent):
myhub.get_body().decode('utf-8'))
```

This example uses SDK types to directly access the underlying EventData object provided by the Event Hubs trigger (currently in Preview):

```python
import logging
import azure.functions as func
import azurefunctions.extensions.bindings.eventhub as eh

app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)

@app.event_hub_message_trigger(
arg_name="event", event_hub_name="EVENTHUB_NAME", connection="EventHubConnection"
)
def eventhub_trigger(event: eh.EventData):
logging.info(
"Python EventHub trigger processed an event %s",
event.body_as_str()
)
```

To run the code snippet generated through the command palette, note the following:

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

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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,16 @@
def $(FUNCTION_NAME_INPUT)(azeventhub: func.EventHubEvent):
logging.info('Python EventHub trigger processed an event: %s',
azeventhub.get_body().decode('utf-8'))


# This example uses SDK types to directly access the underlying EventData object provided by the Event Hubs trigger.
# To use, uncomment the section below and add azurefunctions-extensions-bindings-eventhub to your requirements.txt file
# import azurefunctions.extensions.bindings.eventhub as eh
# @app.event_hub_message_trigger(
# arg_name="event", event_hub_name="$(EVENTHUB_NAME_INPUT)", connection="$(CONNECTION_STRING_INPUT)"
# )
# def $(FUNCTION_NAME_INPUT)(event: eh.EventData):
# logging.info(
# "Python EventHub trigger processed an event %s",
# event.body_as_str()
# )
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,16 @@
def $(FUNCTION_NAME_INPUT)(azeventhub: func.EventHubEvent):
logging.info('Python EventHub trigger processed an event: %s',
azeventhub.get_body().decode('utf-8'))


# This example uses SDK types to directly access the underlying EventData object provided by the Event Hubs trigger.
# To use, uncomment the section below and add azurefunctions-extensions-bindings-eventhub to your requirements.txt file
# import azurefunctions.extensions.bindings.eventhub as eh
# @app.event_hub_message_trigger(
# arg_name="event", event_hub_name="$(EVENTHUB_NAME_INPUT)", connection="$(CONNECTION_STRING_INPUT)"
# )
# def $(FUNCTION_NAME_INPUT)(event: eh.EventData):
# logging.info(
# "Python EventHub trigger processed an event %s",
# event.body_as_str()
# )
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The HTTP trigger lets you invoke a function with an HTTP request. You can use an

## Using the Template

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

```python
import azure.functions as func
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The queue storage trigger runs a function as messages are added to Azure Queue s

## Using the Template

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

```python
import logging
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@
def $(FUNCTION_NAME_INPUT)(azservicebus: func.ServiceBusMessage):
logging.info('Python ServiceBus Queue trigger processed a message: %s',
azservicebus.get_body().decode('utf-8'))


# This example uses SDK types to directly access the underlying ServiceBusReceivedMessage object provided by the Service Bus trigger.
# To use, uncomment the section below and add azurefunctions-extensions-bindings-servicebus to your requirements.txt file
# import azurefunctions.extensions.bindings.servicebus as servicebus
# @app.service_bus_queue_trigger(arg_name="receivedmessage",
# queue_name="$(SERVICEBUS_NAME_INPUT)",
# connection="$(CONNECTION_STRING_INPUT)")
# def $(FUNCTION_NAME_INPUT)(receivedmessage: servicebus.ServiceBusReceivedMessage):
# logging.info("Python ServiceBus queue trigger processed message.")
# logging.info("Receiving: %s\n"
# "Body: %s\n",
# receivedmessage,
# receivedmessage.body)
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,17 @@
def $(FUNCTION_NAME_INPUT)(azservicebus: func.ServiceBusMessage):
logging.info('Python ServiceBus Queue trigger processed a message: %s',
azservicebus.get_body().decode('utf-8'))


# This example uses SDK types to directly access the underlying ServiceBusReceivedMessage object provided by the Service Bus trigger.
# To use, uncomment the section below and add azurefunctions-extensions-bindings-servicebus to your requirements.txt file
# import azurefunctions.extensions.bindings.servicebus as servicebus
# @app.service_bus_queue_trigger(arg_name="receivedmessage",
# queue_name="$(SERVICEBUS_NAME_INPUT)",
# connection="$(CONNECTION_STRING_INPUT)")
# def $(FUNCTION_NAME_INPUT)(receivedmessage: servicebus.ServiceBusReceivedMessage):
# logging.info("Python ServiceBus queue trigger processed message.")
# logging.info("Receiving: %s\n"
# "Body: %s\n",
# receivedmessage,
# receivedmessage.body)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Use the Service Bus Queue trigger to respond to messages from a Service Bus queu

## Using the Template

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

```python
import logging
Expand All @@ -21,11 +21,28 @@ def test_function(msg: func.ServiceBusMessage):
msg.get_body().decode('utf-8'))
```

This example uses SDK types to directly access the underlying ServiceBusReceivedMessage object provided by the Service Bus trigger (currently in Preview):

```python
import azurefunctions.extensions.bindings.servicebus as servicebus

@app.service_bus_queue_trigger(arg_name="receivedmessage",
queue_name="QUEUE_NAME",
connection="SERVICEBUS_CONNECTION")
def servicebus_queue_trigger(receivedmessage: servicebus.ServiceBusReceivedMessage):
logging.info("Python ServiceBus queue trigger processed message.")
logging.info("Receiving: %s\n"
"Body: %s\n",
receivedmessage,
receivedmessage.body)
```

To run the code snippet generated through the command palette, note the following:

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

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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,18 @@
def $(FUNCTION_NAME_INPUT)(azservicebus: func.ServiceBusMessage):
logging.info('Python ServiceBus Topic trigger processed a message: %s',
azservicebus.get_body().decode('utf-8'))


# This example uses SDK types to directly access the underlying ServiceBusReceivedMessage object provided by the Service Bus trigger.
# To use, uncomment the section below and add azurefunctions-extensions-bindings-servicebus to your requirements.txt file
# import azurefunctions.extensions.bindings.servicebus as servicebus
# @app.service_bus_topic_trigger(arg_name="receivedmessage",
# topic_name="$(SERVICEBUS_NAME_INPUT)",
# connection="$(CONNECTION_STRING_INPUT)",
# subscription_name="$(SERVICEBUS_SUBSCRIPTION_NAME_INPUT)")
# def $(FUNCTION_NAME_INPUT)(receivedmessage: servicebus.ServiceBusReceivedMessage):
# logging.info("Python ServiceBus topic trigger processed message.")
# logging.info("Receiving: %s\n"
# "Body: %s\n",
# receivedmessage,
# receivedmessage.body)
Loading