Skip to content

Commit e0683d7

Browse files
Merge pull request #230186 from vrdmr/vameru/updating-linespacing-examples
Updating PyStein examples in docs
2 parents 8d2068d + 076e568 commit e0683d7

File tree

1 file changed

+57
-71
lines changed

1 file changed

+57
-71
lines changed

articles/azure-functions/functions-reference-python.md

Lines changed: 57 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ You can also explicitly declare the attribute types and return type in the funct
8080
```python
8181
import azure.functions
8282

83-
8483
def main(req: azure.functions.HttpRequest) -> str:
8584
user = req.params.get('user')
8685
return f'Hello, {user}!'
@@ -97,7 +96,6 @@ Triggers and bindings can be declared and used in a function in a decorator base
9796
```python
9897
@app.function_name(name="HttpTrigger1")
9998
@app.route(route="req")
100-
10199
def main(req):
102100
user = req.params.get('user')
103101
return f'Hello, {user}!'
@@ -108,9 +106,10 @@ You can also explicitly declare the attribute types and return type in the funct
108106
```python
109107
import azure.functions
110108

109+
app = func.FunctionApp()
110+
111111
@app.function_name(name="HttpTrigger1")
112112
@app.route(route="req")
113-
114113
def main(req: azure.functions.HttpRequest) -> str:
115114
user = req.params.get('user')
116115
return f'Hello, {user}!'
@@ -335,7 +334,7 @@ For example, the following code demonstrates the difference between the two inpu
335334
"direction": "in",
336335
"type": "blob",
337336
"path": "samples/{id}",
338-
"connection": "AzureWebJobsStorage"
337+
"connection": "STORAGE_CONNECTION_STRING"
339338
}
340339
]
341340
}
@@ -347,6 +346,7 @@ For example, the following code demonstrates the difference between the two inpu
347346
"IsEncrypted": false,
348347
"Values": {
349348
"FUNCTIONS_WORKER_RUNTIME": "python",
349+
"STORAGE_CONNECTION_STRING": "<AZURE_STORAGE_CONNECTION_STRING>",
350350
"AzureWebJobsStorage": "<azure-storage-connection-string>"
351351
}
352352
}
@@ -357,14 +357,11 @@ For example, the following code demonstrates the difference between the two inpu
357357
import azure.functions as func
358358
import logging
359359

360-
361-
def main(req: func.HttpRequest,
362-
obj: func.InputStream):
363-
360+
def main(req: func.HttpRequest, obj: func.InputStream):
364361
logging.info(f'Python HTTP-triggered function processed: {obj.read()}')
365362
```
366363

367-
When the function is invoked, the HTTP request is passed to the function as `req`. An entry will be retrieved from the Azure Blob Storage account based on the _ID_ in the route URL and made available as `obj` in the function body. Here, the specified storage account is the connection string that's found in the `AzureWebJobsStorage` app setting, which is the same storage account that's used by the function app.
364+
When the function is invoked, the HTTP request is passed to the function as `req`. An entry will be retrieved from the Azure Blob Storage account based on the _ID_ in the route URL and made available as `obj` in the function body. Here, the specified storage account is the connection string that's found in the `CONNECTION_STRING` app setting.
368365
::: zone-end
369366
::: zone pivot="python-mode-decorators"
370367
Inputs are divided into two categories in Azure Functions: trigger input and other input. Although they're defined using different decorators, their usage is similar in Python code. Connection strings or secrets for trigger and input sources map to values in the *local.settings.json* file when they're running locally, and they map to the application settings when they're running in Azure.
@@ -377,6 +374,7 @@ As an example, the following code demonstrates how to define a Blob Storage inpu
377374
"IsEncrypted": false,
378375
"Values": {
379376
"FUNCTIONS_WORKER_RUNTIME": "python",
377+
"STORAGE_CONNECTION_STRING": "<AZURE_STORAGE_CONNECTION_STRING>",
380378
"AzureWebJobsStorage": "<azure-storage-connection-string>",
381379
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing"
382380
}
@@ -391,14 +389,13 @@ import logging
391389
app = func.FunctionApp()
392390

393391
@app.route(route="req")
394-
@app.read_blob(arg_name="obj", path="samples/{id}", connection="AzureWebJobsStorage")
395-
396-
def main(req: func.HttpRequest,
397-
obj: func.InputStream):
392+
@app.read_blob(arg_name="obj", path="samples/{id}",
393+
connection="STORAGE_CONNECTION_STRING")
394+
def main(req: func.HttpRequest, obj: func.InputStream):
398395
logging.info(f'Python HTTP-triggered function processed: {obj.read()}')
399396
```
400397

401-
When the function is invoked, the HTTP request is passed to the function as `req`. An entry will be retrieved from the Azure Blob Storage account based on the _ID_ in the route URL and made available as `obj` in the function body. Here, the specified storage account is the connection string that's found in the AzureWebJobsStorage app setting, which is the same storage account that's used by the function app.
398+
When the function is invoked, the HTTP request is passed to the function as `req`. An entry will be retrieved from the Azure Blob Storage account based on the _ID_ in the route URL and made available as `obj` in the function body. Here, the specified storage account is the connection string that's found in the `STORAGE_CONNECTION_STRING` app setting.
402399
::: zone-end
403400

404401
For data intensive binding operations, you may want to use a separate storage account. For more information, see [Storage account guidance](storage-considerations.md#storage-account-guidance).
@@ -445,7 +442,7 @@ To produce multiple outputs, use the `set()` method provided by the [`azure.func
445442
"direction": "out",
446443
"type": "queue",
447444
"queueName": "outqueue",
448-
"connection": "AzureWebJobsStorage"
445+
"connection": "STORAGE_CONNECTION_STRING"
449446
},
450447
{
451448
"name": "$return",
@@ -459,7 +456,6 @@ To produce multiple outputs, use the `set()` method provided by the [`azure.func
459456
```python
460457
import azure.functions as func
461458

462-
463459
def main(req: func.HttpRequest,
464460
msg: func.Out[func.QueueMessage]) -> str:
465461

@@ -478,10 +474,10 @@ To produce multiple outputs, use the `set()` method provided by the [`azure.func
478474
# function_app.py
479475
import azure.functions as func
480476

477+
app = func.FunctionApp()
481478

482479
@app.write_blob(arg_name="msg", path="output-container/{name}",
483-
connection="AzureWebJobsStorage")
484-
480+
connection="CONNECTION_STRING")
485481
def test_function(req: func.HttpRequest,
486482
msg: func.Out[str]) -> str:
487483

@@ -500,7 +496,6 @@ The following example logs an info message when the function is invoked via an H
500496
```python
501497
import logging
502498

503-
504499
def main(req):
505500
logging.info('Python HTTP trigger function processed a request.')
506501
```
@@ -639,7 +634,6 @@ The following example is from the HTTP trigger template for the Python v2 progra
639634
```python
640635
@app.function_name(name="HttpTrigger1")
641636
@app.route(route="hello")
642-
643637
def test_function(req: func.HttpRequest) -> func.HttpResponse:
644638
logging.info('Python HTTP trigger function processed a request.')
645639

@@ -909,13 +903,13 @@ The following example uses `os.environ["myAppSetting"]` to get the [application
909903
```python
910904
import logging
911905
import os
906+
912907
import azure.functions as func
913908

914909
def main(req: func.HttpRequest) -> func.HttpResponse:
915-
916-
# Get the setting named 'myAppSetting'
917-
my_app_setting_value = os.environ["myAppSetting"]
918-
logging.info(f'My app setting value:{my_app_setting_value}')
910+
# Get the setting named 'myAppSetting'
911+
my_app_setting_value = os.environ["myAppSetting"]
912+
logging.info(f'My app setting value:{my_app_setting_value}')
919913
```
920914

921915
For local development, application settings are [maintained in the *local.settings.json* file](functions-develop-local.md#local-settings-file).
@@ -936,17 +930,17 @@ The following example uses `os.environ["myAppSetting"]` to get the [application
936930
```python
937931
import logging
938932
import os
933+
939934
import azure.functions as func
940935

936+
app = func.FunctionApp()
937+
941938
@app.function_name(name="HttpTrigger1")
942939
@app.route(route="req")
943-
944940
def main(req: func.HttpRequest) -> func.HttpResponse:
945-
946-
947-
# Get the setting named 'myAppSetting'
948-
my_app_setting_value = os.environ["myAppSetting"]
949-
logging.info(f'My app setting value:{my_app_setting_value}')
941+
# Get the setting named 'myAppSetting'
942+
my_app_setting_value = os.environ["myAppSetting"]
943+
logging.info(f'My app setting value:{my_app_setting_value}')
950944
```
951945

952946
For local development, application settings are [maintained in the *local.settings.json* file](functions-develop-local.md#local-settings-file).
@@ -1112,14 +1106,14 @@ from shared_code import my_second_helper_function
11121106
# Define an HTTP trigger that accepts the ?value=<int> query parameter
11131107
# Double the value and return the result in HttpResponse
11141108
def main(req: func.HttpRequest) -> func.HttpResponse:
1115-
logging.info('Executing my_second_function.')
1109+
logging.info('Executing my_second_function.')
11161110

1117-
initial_value: int = int(req.params.get('value'))
1118-
doubled_value: int = my_second_helper_function.double(initial_value)
1111+
initial_value: int = int(req.params.get('value'))
1112+
doubled_value: int = my_second_helper_function.double(initial_value)
11191113

1120-
return func.HttpResponse(
1121-
body=f"{initial_value} * 2 = {doubled_value}",
1122-
status_code=200
1114+
return func.HttpResponse(
1115+
body=f"{initial_value} * 2 = {doubled_value}",
1116+
status_code=200
11231117
)
11241118
```
11251119

@@ -1145,22 +1139,17 @@ import azure.functions as func
11451139
from my_second_function import main
11461140

11471141
class TestFunction(unittest.TestCase):
1148-
def test_my_second_function(self):
1149-
# Construct a mock HTTP request.
1150-
req = func.HttpRequest(
1151-
method='GET',
1152-
body=None,
1153-
url='/api/my_second_function',
1154-
params={'value': '21'})
1155-
1156-
# Call the function.
1157-
resp = main(req)
1158-
1159-
# Check the output.
1160-
self.assertEqual(
1161-
resp.get_body(),
1162-
b'21 * 2 = 42',
1163-
)
1142+
def test_my_second_function(self):
1143+
# Construct a mock HTTP request.
1144+
req = func.HttpRequest(method='GET',
1145+
body=None,
1146+
url='/api/my_second_function',
1147+
params={'value': '21'})
1148+
# Call the function.
1149+
resp = main(req)
1150+
1151+
# Check the output.
1152+
self.assertEqual(resp.get_body(), b'21 * 2 = 42',)
11641153
```
11651154

11661155
Inside your *.venv* Python virtual environment folder, install your favorite Python test framework, such as `pip install pytest`. Then run `pytest tests` to check the test result.
@@ -1180,7 +1169,6 @@ from shared_code import my_second_helper_function
11801169

11811170
app = func.FunctionApp()
11821171

1183-
11841172
# Define the HTTP trigger that accepts the ?value=<int> query parameter
11851173
# Double the value and return the result in HttpResponse
11861174
@app.function_name(name="my_second_function")
@@ -1215,27 +1203,24 @@ You can start writing test cases for your HTTP trigger.
12151203
# <project_root>/tests/test_my_second_function.py
12161204
import unittest
12171205
import azure.functions as func
1218-
from function_app import main
12191206

1207+
from function_app import main
12201208

12211209
class TestFunction(unittest.TestCase):
1222-
def test_my_second_function(self):
1223-
# Construct a mock HTTP request.
1224-
req = func.HttpRequest(
1225-
method='GET',
1226-
body=None,
1227-
url='/api/my_second_function',
1228-
params={'value': '21'})
1229-
1230-
# Call the function.
1231-
func_call = main.build().get_user_function()
1232-
resp = func_call(req)
1233-
1234-
# Check the output.
1235-
self.assertEqual(
1236-
resp.get_body(),
1237-
b'21 * 2 = 42',
1238-
)
1210+
def test_my_second_function(self):
1211+
# Construct a mock HTTP request.
1212+
req = func.HttpRequest(method='GET',
1213+
body=None,
1214+
url='/api/my_second_function',
1215+
params={'value': '21'})
1216+
# Call the function.
1217+
func_call = main.build().get_user_function()
1218+
resp = func_call(req)
1219+
# Check the output.
1220+
self.assertEqual(
1221+
resp.get_body(),
1222+
b'21 * 2 = 42',
1223+
)
12391224
```
12401225

12411226
Inside your *.venv* Python virtual environment folder, install your favorite Python test framework, such as `pip install pytest`. Then run `pytest tests` to check the test result.
@@ -1255,6 +1240,7 @@ The following example creates a named temporary file in the temporary directory
12551240
import logging
12561241
import azure.functions as func
12571242
import tempfile
1243+
12581244
from os import listdir
12591245

12601246
#---

0 commit comments

Comments
 (0)