Skip to content

Commit 076e568

Browse files
authored
Update functions-bindings-triggers-python.md
1 parent 5e161e1 commit 076e568

File tree

1 file changed

+47
-80
lines changed

1 file changed

+47
-80
lines changed

articles/azure-functions/functions-bindings-triggers-python.md

Lines changed: 47 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,10 @@ The following code snippet defines a function triggered from Azure Blob Storage:
3333
```python
3434
import logging
3535
import azure.functions as func
36-
3736
app = func.FunctionApp()
38-
3937
@app.function_name(name="BlobTrigger1")
40-
@app.blob_trigger(arg_name="myblob",
41-
path="PATH/TO/BLOB",
42-
connection="CONNECTION_SETTING")
38+
@app.blob_trigger(arg_name="myblob", path="samples-workitems/{name}",
39+
connection="AzureWebJobsStorage")
4340
def test_function(myblob: func.InputStream):
4441
logging.info(f"Python blob trigger function processed blob \n"
4542
f"Name: {myblob.name}\n"
@@ -51,14 +48,12 @@ def test_function(myblob: func.InputStream):
5148
```python
5249
import logging
5350
import azure.functions as func
54-
5551
app = func.FunctionApp()
56-
5752
@app.function_name(name="BlobInput1")
5853
@app.route(route="file")
5954
@app.blob_input(arg_name="inputblob",
60-
path="PATH/TO/BLOB",
61-
connection="CONNECTION_SETTING")
55+
path="sample-workitems/{name}",
56+
connection="AzureWebJobsStorage")
6257
def test(req: func.HttpRequest, inputblob: bytes) -> func.HttpResponse:
6358
logging.info(f'Python Queue trigger function processed {len(inputblob)} bytes')
6459
return inputblob
@@ -69,17 +64,15 @@ def test(req: func.HttpRequest, inputblob: bytes) -> func.HttpResponse:
6964
```python
7065
import logging
7166
import azure.functions as func
72-
7367
app = func.FunctionApp()
74-
7568
@app.function_name(name="BlobOutput1")
7669
@app.route(route="file")
7770
@app.blob_input(arg_name="inputblob",
78-
path="PATH/TO/BLOB",
79-
connection="CONNECTION_SETTING")
71+
path="sample-workitems/test.txt",
72+
connection="AzureWebJobsStorage")
8073
@app.blob_output(arg_name="outputblob",
81-
path="PATH/TO/BLOB",
82-
connection="CONNECTION_SETTING")
74+
path="newblob/test.txt",
75+
connection="AzureWebJobsStorage")
8376
def main(req: func.HttpRequest, inputblob: str, outputblob: func.Out[str]):
8477
logging.info(f'Python Queue trigger function processed {len(inputblob)} bytes')
8578
outputblob.set(inputblob)
@@ -93,14 +86,9 @@ The following code snippet defines a function triggered from an Azure Cosmos DB
9386
```python
9487
import logging
9588
import azure.functions as func
96-
9789
app = func.FunctionApp()
98-
9990
@app.function_name(name="CosmosDBTrigger1")
100-
@app.cosmos_db_trigger(arg_name="documents",
101-
database_name="DB_NAME",
102-
collection_name="COLLECTION_NAME",
103-
connection_string_setting="CONNECTION_SETTING",
91+
@app.cosmos_db_trigger(arg_name="documents", database_name="<DB_NAME>", collection_name="<COLLECTION_NAME>", connection_string_setting=""AzureWebJobsStorage"",
10492
lease_collection_name="leases", create_lease_collection_if_not_exists="true")
10593
def test_function(documents: func.DocumentList) -> str:
10694
if documents:
@@ -112,14 +100,12 @@ def test_function(documents: func.DocumentList) -> str:
112100
```python
113101
import logging
114102
import azure.functions as func
115-
116103
app = func.FunctionApp()
117-
118104
@app.route()
119-
@app.cosmos_db_input(arg_name="documents",
120-
database_name="DB_NAME",
121-
collection_name="COLLECTION_NAME",
122-
connection_string_setting="CONNECTION_SETTING")
105+
@app.cosmos_db_input(
106+
arg_name="documents", database_name="<DB_NAME>",
107+
collection_name="<COLLECTION_NAME>",
108+
connection_string_setting="CONNECTION_SETTING")
123109
def cosmosdb_input(req: func.HttpRequest, documents: func.DocumentList) -> str:
124110
return func.HttpResponse(documents[0].to_json())
125111
```
@@ -129,15 +115,12 @@ def cosmosdb_input(req: func.HttpRequest, documents: func.DocumentList) -> str:
129115
```python
130116
import logging
131117
import azure.functions as func
132-
133-
app = func.FunctionApp()
134-
135118
@app.route()
136-
@app.cosmos_db_output(arg_name="documents",
137-
database_name="DB_NAME",
138-
collection_name="COLLECTION_NAME",
139-
create_if_not_exists=True,
140-
connection_string_setting="CONNECTION_SETTING")
119+
@app.cosmos_db_output(
120+
arg_name="documents", database_name="<DB_NAME>",
121+
collection_name="<COLLECTION_NAME>",
122+
create_if_not_exists=True,
123+
connection_string_setting="CONNECTION_SETTING")
141124
def main(req: func.HttpRequest, documents: func.Out[func.Document]) -> func.HttpResponse:
142125
request_body = req.get_body()
143126
documents.set(func.Document.from_json(request_body))
@@ -151,13 +134,10 @@ The following code snippet defines a function triggered from an event hub instan
151134
```python
152135
import logging
153136
import azure.functions as func
154-
155137
app = func.FunctionApp()
156-
157138
@app.function_name(name="EventHubTrigger1")
158-
@app.event_hub_message_trigger(arg_name="myhub",
159-
event_hub_name="EVENT_HUB_NAME",
160-
connection="CONNECTION_SETTING")
139+
@app.event_hub_message_trigger(arg_name="myhub", event_hub_name="samples-workitems",
140+
connection=""CONNECTION_SETTING"")
161141
def test_function(myhub: func.EventHubEvent):
162142
logging.info('Python EventHub trigger processed an event: %s',
163143
myhub.get_body().decode('utf-8'))
@@ -168,13 +148,11 @@ def test_function(myhub: func.EventHubEvent):
168148
```python
169149
import logging
170150
import azure.functions as func
171-
172151
app = func.FunctionApp()
173-
174152
@app.function_name(name="eventhub_output")
175153
@app.route(route="eventhub_output")
176154
@app.event_hub_output(arg_name="event",
177-
event_hub_name="EVENT_HUB_NAME",
155+
event_hub_name="samples-workitems",
178156
connection="CONNECTION_SETTING")
179157
def eventhub_output(req: func.HttpRequest, event: func.Out[str]):
180158
body = req.get_body()
@@ -192,16 +170,25 @@ The following code snippet defines an HTTP triggered function:
192170
```python
193171
import azure.functions as func
194172
import logging
195-
196173
app = func.FunctionApp(auth_level=func.AuthLevel.ANONYMOUS)
197-
198174
@app.function_name(name="HttpTrigger1")
199175
@app.route(route="hello")
200176
def test_function(req: func.HttpRequest) -> func.HttpResponse:
201-
logging.info('Python HTTP trigger function processed a request.')
202-
return func.HttpResponse(
203-
"This HTTP triggered function executed successfully.",
204-
status_code=200
177+
logging.info('Python HTTP trigger function processed a request.')
178+
name = req.params.get('name')
179+
if not name:
180+
try:
181+
req_body = req.get_json()
182+
except ValueError:
183+
pass
184+
else:
185+
name = req_body.get('name')
186+
if name:
187+
return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
188+
else:
189+
return func.HttpResponse(
190+
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
191+
status_code=200
205192
)
206193
```
207194

@@ -210,13 +197,10 @@ def test_function(req: func.HttpRequest) -> func.HttpResponse:
210197
```python
211198
import logging
212199
import azure.functions as func
213-
214200
app = func.FunctionApp()
215-
216201
@app.function_name(name="QueueTrigger1")
217-
@app.queue_trigger(arg_name="msg",
218-
queue_name="QUEUE_NAME",
219-
connection="CONNECTION_SETTING")
202+
@app.queue_trigger(arg_name="msg", queue_name="python-queue-items",
203+
connection=""AzureWebJobsStorage"")
220204
def test_function(msg: func.QueueMessage):
221205
logging.info('Python EventHub trigger processed an event: %s',
222206
msg.get_body().decode('utf-8'))
@@ -227,14 +211,10 @@ def test_function(msg: func.QueueMessage):
227211
```python
228212
import logging
229213
import azure.functions as func
230-
231214
app = func.FunctionApp()
232-
233215
@app.function_name(name="QueueOutput1")
234216
@app.route(route="message")
235-
@app.queue_output(arg_name="msg",
236-
queue_name="QUEUE_NAME",
237-
connection="CONNECTION_SETTING")
217+
@app.queue_output(arg_name="msg", queue_name="python-queue-items", connection="AzureWebJobsStorage")
238218
def main(req: func.HttpRequest, msg: func.Out[str]) -> func.HttpResponse:
239219
input_msg = req.params.get('name')
240220
msg.set(input_msg)
@@ -248,13 +228,9 @@ def main(req: func.HttpRequest, msg: func.Out[str]) -> func.HttpResponse:
248228
```python
249229
import logging
250230
import azure.functions as func
251-
252231
app = func.FunctionApp()
253-
254232
@app.function_name(name="ServiceBusQueueTrigger1")
255-
@app.service_bus_queue_trigger(arg_name="msg",
256-
queue_name="QUEUE_NAME",
257-
connection="CONNECTION_SETTING")
233+
@app.service_bus_queue_trigger(arg_name="msg", queue_name="myinputqueue", connection="CONNECTION_SETTING")
258234
def test_function(msg: func.ServiceBusMessage):
259235
logging.info('Python ServiceBus queue trigger processed message: %s',
260236
msg.get_body().decode('utf-8'))
@@ -265,14 +241,9 @@ def test_function(msg: func.ServiceBusMessage):
265241
```python
266242
import logging
267243
import azure.functions as func
268-
269244
app = func.FunctionApp()
270-
271245
@app.function_name(name="ServiceBusTopicTrigger1")
272-
@app.service_bus_topic_trigger(arg_name="message",
273-
topic_name="TOPIC_NAME",
274-
connection="CONNECTION_SETTING",
275-
subscription_name="SUBSCRIPTION_NAME")
246+
@app.service_bus_topic_trigger(arg_name="message", topic_name="mytopic", connection="CONNECTION_SETTING", subscription_name="testsub")
276247
def test_function(message: func.ServiceBusMessage):
277248
message_body = message.get_body().decode("utf-8")
278249
logging.info("Python ServiceBus topic trigger processed message.")
@@ -284,13 +255,12 @@ def test_function(message: func.ServiceBusMessage):
284255
```python
285256
import logging
286257
import azure.functions as func
287-
288258
app = func.FunctionApp()
289-
290259
@app.route(route="put_message")
291-
@app.service_bus_topic_output(arg_name="message",
292-
connection="CONNECTION_SETTING",
293-
topic_name="TOPIC_NAME")
260+
@app.service_bus_topic_output(
261+
arg_name="message",
262+
connection="CONNECTION_SETTING",
263+
topic_name="mytopic")
294264
def main(req: func.HttpRequest, message: func.Out[str]) -> func.HttpResponse:
295265
input_msg = req.params.get('message')
296266
message.set(input_msg)
@@ -303,13 +273,10 @@ def main(req: func.HttpRequest, message: func.Out[str]) -> func.HttpResponse:
303273
import datetime
304274
import logging
305275
import azure.functions as func
306-
307276
app = func.FunctionApp()
308-
309277
@app.function_name(name="mytimer")
310-
@app.schedule(schedule="0 */5 * * * *",
311-
arg_name="mytimer",
312-
run_on_startup=True)
278+
@app.schedule(schedule="0 */5 * * * *", arg_name="mytimer", run_on_startup=True,
279+
use_monitor=False)
313280
def test_function(mytimer: func.TimerRequest) -> None:
314281
utc_timestamp = datetime.datetime.utcnow().replace(
315282
tzinfo=datetime.timezone.utc).isoformat()

0 commit comments

Comments
 (0)