Skip to content

Commit 5211d98

Browse files
committed
Adding missing Python V2 samples to docs'
1 parent 145a045 commit 5211d98

10 files changed

+167
-14
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,6 @@ The following is sample python code for the function_app.py file:
824824
import json
825825
import logging
826826
import azure.functions as func
827-
from azure.functions.decorators.core import DataType
828827

829828
app = func.FunctionApp()
830829

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

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-service-bus-output.md

Lines changed: 62 additions & 4 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
@@ -289,9 +290,25 @@ def main(req: func.HttpRequest, message: func.Out[str]) -> func.HttpResponse:
289290
return 'OK'
290291
```
291292

292-
# [v1](#tab/python-v1)
293+
This example shows how to write out to a Service Bus queue.
293294

294-
A Service Bus binding definition is defined in *function.json* where *type* is set to `serviceBus`.
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="<CONNECTION_SETTING>",
304+
queue_name="<QUEUE_NAME>")
305+
def put_message(req: func.HttpRequest, msg: func.Out[str]):
306+
msg.set(req.get_body().decode('utf-8'))
307+
return 'OK'
308+
```
309+
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')

articles/azure-functions/functions-bindings-storage-blob-input.md

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,25 @@ Write-Host "PowerShell Blob trigger: Name: $($TriggerMetadata.Name) Size: $($Inp
219219

220220
This example uses SDK types to directly access the underlying `BlobClient` object provided by the Blob storage input binding:
221221

222-
:::code language="python" source="~/functions-python-extensions/azurefunctions-extensions-bindings-blob/samples/blob_samples_blobclient/function_app.py" range="9-14,42-52":::
222+
```python
223+
import logging
224+
import azure.functions as func
225+
import azurefunctions.extensions.bindings.blob as blob
226+
227+
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
228+
229+
@app.route(route="file")
230+
@app.blob_input(
231+
arg_name="client", path="PATH/TO/BLOB", connection="CONNECTION_SETTING"
232+
)
233+
def blob_input(req: func.HttpRequest, client: blob.BlobClient):
234+
logging.info(
235+
f"Python blob input function processed blob \n"
236+
f"Properties: {client.get_blob_properties()}\n"
237+
f"Blob content head: {client.download_blob().read(size=1)}"
238+
)
239+
return "ok"
240+
```
223241

224242
For examples of using other SDK types, see the [`ContainerClient`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-blob/samples/blob_samples_containerclient/function_app.py) and [`StorageStreamDownloader`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-blob/samples/blob_samples_storagestreamdownloader/function_app.py) samples.
225243

@@ -236,11 +254,11 @@ app = func.FunctionApp()
236254
@app.function_name(name="BlobOutput1")
237255
@app.route(route="file")
238256
@app.blob_input(arg_name="inputblob",
239-
path="sample-workitems/test.txt",
240-
connection="<BLOB_CONNECTION_SETTING>")
257+
path="PATH/TO/BLOB",
258+
connection="CONNECTION_SETTING")
241259
@app.blob_output(arg_name="outputblob",
242-
path="newblob/test.txt",
243-
connection="<BLOB_CONNECTION_SETTING>")
260+
path="PATH/TO/NEW/BLOB",
261+
connection="CONNECTION_SETTING")
244262
def main(req: func.HttpRequest, inputblob: str, outputblob: func.Out[str]):
245263
logging.info(f'Python Queue trigger function processed {len(inputblob)} bytes')
246264
outputblob.set(inputblob)

articles/azure-functions/functions-bindings-storage-blob-trigger.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,23 @@ Write-Host "PowerShell Blob trigger: Name: $($TriggerMetadata.Name) Size: $($Inp
176176

177177
This example uses SDK types to directly access the underlying [`BlobClient`](/python/api/azure-storage-blob/azure.storage.blob.blobclient) object provided by the Blob storage trigger:
178178

179-
:::code language="python" source="~/functions-python-extensions/azurefunctions-extensions-bindings-blob/samples/blob_samples_blobclient/function_app.py" range="9-14,31-39":::
179+
```python
180+
import logging
181+
import azure.functions as func
182+
import azurefunctions.extensions.bindings.blob as blob
183+
184+
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
185+
186+
@app.blob_trigger(
187+
arg_name="client", path="PATH/TO/BLOB", connection="CONNECTION_SETTING"
188+
)
189+
def blob_trigger(client: blob.BlobClient):
190+
logging.info(
191+
f"Python blob trigger function processed blob \n"
192+
f"Properties: {client.get_blob_properties()}\n"
193+
f"Blob content head: {client.download_blob().read(size=1)}"
194+
)
195+
```
180196

181197
For examples of using other SDK types, see the [`ContainerClient`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-blob/samples/blob_samples_containerclient/function_app.py) and [`StorageStreamDownloader`](https://github.com/Azure/azure-functions-python-extensions/blob/dev/azurefunctions-extensions-bindings-blob/samples/blob_samples_storagestreamdownloader/function_app.py) samples.
182198

articles/azure-functions/functions-bindings-storage-table-input.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,27 @@ The following function uses an HTTP trigger to read a single table row as input
514514

515515
In this example, binding configuration specifies an explicit value for the table's `partitionKey` and uses an expression to pass to the `rowKey`. The `rowKey` expression, `{id}` indicates that the row key comes from the `{id}` part of the route in the request.
516516

517+
# [v2](#tab/python-v2)
518+
```python
519+
import json
520+
import azure.functions as func
521+
522+
app = func.FunctionApp()
523+
524+
@app.route(route="messages/{id}")
525+
@app.table_input(arg_name="messageJSON",
526+
connection="AzureWebJobsStorage",
527+
table_name="messages",
528+
row_key='{id}',
529+
partition_key="message")
530+
def table_in_binding(req: func.HttpRequest, messageJSON):
531+
message = json.loads(messageJSON)
532+
return func.HttpResponse(f"Table row: {messageJSON}")
533+
```
534+
535+
With this simple binding, you can't programmatically handle a case in which no row that has a row key ID is found. For more fine-grained data selection, use the [storage SDK](/azure/developer/python/sdk/examples/azure-sdk-example-storage-use?tabs=cmd).
536+
537+
# [v1](#tab/python-v1)
517538
Binding configuration in the _function.json_ file:
518539

519540
```json

articles/azure-functions/functions-bindings-storage-table-output.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,33 @@ foreach ($i in 1..10) {
290290

291291
The following example demonstrates how to use the Table storage output binding. Configure the `table` binding in the *function.json* by assigning values to `name`, `tableName`, `partitionKey`, and `connection`:
292292

293+
# [v2](#tab/python-v2)
294+
The following function generates a unique UUI for the `rowKey` value and persists the message into Table storage.
295+
296+
```python
297+
import logging
298+
import uuid
299+
import json
300+
import azure.functions as func
301+
302+
app = func.FunctionApp()
303+
304+
@app.route(route="table_out_binding", binding_arg_name="message")
305+
@app.table_output(arg_name="$return",
306+
connection="AzureWebJobsStorage",
307+
table_name="messages")
308+
def table_out_binding(req: func.HttpRequest, message: func.Out[func.HttpResponse]):
309+
rowKey = str(uuid.uuid4())
310+
data = {
311+
"Name": "Output binding message",
312+
"PartitionKey": "message",
313+
"RowKey": rowKey
314+
}
315+
message.set(json.dumps(data))
316+
return func.HttpResponse(f"Message created with the rowKey: {rowKey}")
317+
```
318+
319+
# [v1](#tab/python-v1)
293320
```json
294321
{
295322
"scriptFile": "__init__.py",

articles/azure-functions/functions-bindings-timer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ app = func.FunctionApp()
9595
@app.function_name(name="mytimer")
9696
@app.timer_trigger(schedule="0 */5 * * * *",
9797
arg_name="mytimer",
98-
run_on_startup=True)
98+
run_on_startup=False)
9999
def test_function(mytimer: func.TimerRequest) -> None:
100100
utc_timestamp = datetime.datetime.utcnow().replace(
101101
tzinfo=datetime.timezone.utc).isoformat()

articles/azure-functions/functions-bindings-warmup.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,23 @@ PowerShell example code pending.
160160
The following example shows a warmup trigger in a *function.json* file and a [Python function](functions-reference-python.md) that runs on each new instance when it'is added to your app.
161161

162162
Your function must be named `warmup` (case-insensitive) and there can only be one warmup function per app.
163+
# [v2](#tab/python-v2)
163164

165+
```python
166+
import logging
167+
import azure.functions as func
168+
169+
app = func.FunctionApp()
170+
171+
172+
@app.warm_up_trigger('warmup')
173+
def warmup(warmup) -> None:
174+
logging.info('Function App instance is warm')
175+
```
176+
177+
For more information, see [Configuration](#configuration).
178+
179+
# [v1](#tab/python-v1)
164180
Here's the *function.json* file:
165181

166182
```json

0 commit comments

Comments
 (0)