├── .gitignore ├── BlobExample ├── __init__.py └── function.json ├── CosmosDBExample ├── __init__.py └── function.json ├── CosmosDBMultiOutputExample ├── __init__.py └── function.json ├── HttpExample ├── __init__.py └── function.json ├── QueueExample ├── __init__.py └── function.json ├── README.md ├── TimerExample ├── __init__.py └── function.json ├── host.json └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | bin 2 | obj 3 | csx 4 | .vs 5 | .DS_Store 6 | edge 7 | Publish 8 | 9 | *.user 10 | *.suo 11 | *.cscfg 12 | *.Cache 13 | project.lock.json 14 | 15 | /packages 16 | /TestResults 17 | 18 | /tools/NuGet.exe 19 | /App_Data 20 | /secrets 21 | /data 22 | .secrets 23 | appsettings.json 24 | local.settings.json 25 | 26 | node_modules 27 | dist 28 | 29 | # Local python packages 30 | .python_packages/ 31 | 32 | # Python Environments 33 | .env 34 | .venv 35 | env/ 36 | venv/ 37 | ENV/ 38 | env.bak/ 39 | venv.bak/ 40 | 41 | # Byte-compiled / optimized / DLL files 42 | __pycache__/ 43 | *.py[cod] 44 | *$py.class -------------------------------------------------------------------------------- /BlobExample/__init__.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import azure.functions as func 3 | 4 | def main(inputBlob: func.InputStream, outputBlob: func.Out[str]): 5 | 6 | logging.info(f"Blob trigger executed!") 7 | logging.info(f"Blob Name: {inputBlob.name} ({inputBlob.length}) bytes") 8 | logging.info(f"Full Blob URI: {inputBlob.uri}") 9 | 10 | output = "Hello World!" 11 | outputBlob.set(output) 12 | -------------------------------------------------------------------------------- /BlobExample/function.json: -------------------------------------------------------------------------------- 1 | { 2 | "scriptFile": "__init__.py", 3 | "bindings": [ 4 | { 5 | "name": "inputBlob", 6 | "type": "blobTrigger", 7 | "direction": "in", 8 | "path": "myblobcontainer/input/{name}", 9 | "connection": "MyStorageConnectionAppSetting" 10 | }, 11 | { 12 | "name": "outputBlob", 13 | "type": "blob", 14 | "direction": "out", 15 | "path": "myblobcontainer/output/{name}", 16 | "connection": "MyStorageConnectionAppSetting" 17 | } 18 | ] 19 | } -------------------------------------------------------------------------------- /CosmosDBExample/__init__.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import azure.functions as func 3 | 4 | def main(documents: func.DocumentList) -> func.Document: 5 | 6 | logging.info(f"CosmosDB trigger executed!") 7 | 8 | for doc in documents: 9 | logging.info(f"Document: {doc.to_json()}") 10 | 11 | returnDoc = func.Document.from_dict({"text": "Hello World", "foo": "bar"}) 12 | return returnDoc 13 | -------------------------------------------------------------------------------- /CosmosDBExample/function.json: -------------------------------------------------------------------------------- 1 | { 2 | "scriptFile": "__init__.py", 3 | "bindings": [ 4 | { 5 | "name": "documents", 6 | "type": "cosmosDBTrigger", 7 | "direction": "in", 8 | "databaseName": "cosmostest", 9 | "collectionName": "mycollection", 10 | "connectionStringSetting": "MyCosmosDBConnectionAppSetting", 11 | "leaseCollectionName": "leases", 12 | "createLeaseCollectionIfNotExists": true 13 | }, 14 | { 15 | "name": "$return", 16 | "type": "cosmosDB", 17 | "direction": "out", 18 | "databaseName": "cosmostest", 19 | "collectionName": "mycollection2", 20 | "connectionStringSetting": "MyCosmosDBConnectionAppSetting" 21 | } 22 | ] 23 | } -------------------------------------------------------------------------------- /CosmosDBMultiOutputExample/__init__.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import azure.functions as func 3 | 4 | def main(documents: func.DocumentList) -> func.DocumentList: 5 | 6 | logging.info(f"CosmosDB trigger executed!") 7 | 8 | for doc in documents: 9 | logging.info(f"Document: {doc.to_json()}") 10 | 11 | returnDocs = [] 12 | for x in range(0, 3): 13 | newDoc = func.Document.from_dict({"text": str(x), "foo": "bar"}) 14 | returnDocs.append(newDoc) 15 | 16 | return func.DocumentList(returnDocs) 17 | -------------------------------------------------------------------------------- /CosmosDBMultiOutputExample/function.json: -------------------------------------------------------------------------------- 1 | { 2 | "scriptFile": "__init__.py", 3 | "bindings": [ 4 | { 5 | "name": "documents", 6 | "type": "cosmosDBTrigger", 7 | "direction": "in", 8 | "databaseName": "cosmostest", 9 | "collectionName": "mycollection", 10 | "connectionStringSetting": "MyCosmosDBConnectionAppSetting", 11 | "leaseCollectionName": "leases", 12 | "createLeaseCollectionIfNotExists": true 13 | }, 14 | { 15 | "name": "$return", 16 | "type": "cosmosDB", 17 | "direction": "out", 18 | "databaseName": "cosmostest", 19 | "collectionName": "mycollection2", 20 | "connectionStringSetting": "MyCosmosDBConnectionAppSetting" 21 | } 22 | ] 23 | } -------------------------------------------------------------------------------- /HttpExample/__init__.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import azure.functions as func 3 | 4 | def main(request: func.HttpRequest) -> func.HttpResponse: 5 | 6 | logging.info(f"HTTP trigger executed!") 7 | 8 | logging.info(f"Headers: {request.headers}") 9 | logging.info(f"Params: {request.params}") 10 | logging.info(f"Route Params: {request.route_params}") 11 | logging.info(f"Body: {request.get_body()}") 12 | 13 | try: 14 | logging.info(f"Body JSON: {request.get_json()}") 15 | except ValueError: 16 | pass 17 | 18 | 19 | return func.HttpResponse(f"Hello World!", status_code=200) 20 | -------------------------------------------------------------------------------- /HttpExample/function.json: -------------------------------------------------------------------------------- 1 | { 2 | "scriptFile": "__init__.py", 3 | "bindings": [ 4 | { 5 | "name": "request", 6 | "type": "httpTrigger", 7 | "direction": "in", 8 | "authLevel": "function", 9 | "methods": [ 10 | "get", 11 | "post" 12 | ], 13 | "route": "HttpExample/{user:alpha?}/{id:int?}" 14 | }, 15 | { 16 | "name": "$return", 17 | "type": "http", 18 | "direction": "out" 19 | } 20 | ] 21 | } -------------------------------------------------------------------------------- /QueueExample/__init__.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import json 3 | import azure.functions as func 4 | 5 | def main(inMessage: func.QueueMessage, outMessage: func.Out[func.QueueMessage]): 6 | 7 | logging.info(f"Queue trigger executed!") 8 | logging.info(f"Message Id: {inMessage.id}") 9 | logging.info(f"Message Body: {inMessage.get_body()}") 10 | logging.info(f"Message Body as JSON: {inMessage.get_json()}") 11 | logging.info(f"Dequeue Count: {inMessage.dequeue_count}") 12 | logging.info(f"Insertion Time: {inMessage.insertion_time}") 13 | logging.info(f"Expiration Time: {inMessage.expiration_time}") 14 | logging.info(f"Time Next Visible: {inMessage.time_next_visible}") 15 | 16 | output = {'text': 'abc'} 17 | outMessage.set(json.dumps(output)) 18 | -------------------------------------------------------------------------------- /QueueExample/function.json: -------------------------------------------------------------------------------- 1 | { 2 | "scriptFile": "__init__.py", 3 | "bindings": [ 4 | { 5 | "name": "inMessage", 6 | "type": "queueTrigger", 7 | "direction": "in", 8 | "queueName": "my-input-queue", 9 | "connection": "MyStorageConnectionAppSetting" 10 | }, 11 | { 12 | "name": "outMessage", 13 | "type": "queue", 14 | "direction": "out", 15 | "queueName": "my-output-queue", 16 | "connection": "MyStorageConnectionAppSetting" 17 | } 18 | ] 19 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Azure Functions (v2) in Python Examples 2 | 3 | This repository contains several examples for writing Azure Functions v2 in Python. 4 | 5 | * HttpExample --> HTTP Trigger and output binding 6 | * BlobExample --> Blob trigger and output binding 7 | * QueueExample --> Queue trigger and output binding 8 | * CosmosDBExample --> CosmosDB trigger and single document output binding 9 | * CosmosDBMultiOutputExample --> CosmosDB trigger and multiple document output binding 10 | * TimerExample --> Timer trigger 11 | 12 | Full details can be found in [this blog post](https://clemenssiebler.com/quickstart-writing-azure-functions-in-python-v2/). -------------------------------------------------------------------------------- /TimerExample/__init__.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import logging 3 | import azure.functions as func 4 | 5 | def main(myTimer: func.TimerRequest) -> None: 6 | utc_timestamp = datetime.datetime.utcnow().replace( 7 | tzinfo=datetime.timezone.utc).isoformat() 8 | 9 | if myTimer.past_due: 10 | logging.info('The timer is past due!') 11 | 12 | logging.info(f"Python timer trigger function ran at {utc_timestamp}") 13 | -------------------------------------------------------------------------------- /TimerExample/function.json: -------------------------------------------------------------------------------- 1 | { 2 | "scriptFile": "__init__.py", 3 | "bindings": [ 4 | { 5 | "name": "myTimer", 6 | "type": "timerTrigger", 7 | "direction": "in", 8 | "schedule": "* */10 * * * *" 9 | } 10 | ] 11 | } -------------------------------------------------------------------------------- /host.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "2.0" 3 | } -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | azure-functions==1.0.0b3 2 | azure-functions-worker==1.0.0b4 3 | grpcio==1.14.2 4 | grpcio-tools==1.14.2 5 | protobuf==3.7.0 6 | six==1.12.0 7 | --------------------------------------------------------------------------------