Skip to content

Commit fde7673

Browse files
authored
Merge pull request #214216 from ggailey777/shreya
[Functions] Pystein with pivots
2 parents a1067ea + fd1acc0 commit fde7673

11 files changed

+1160
-249
lines changed

articles/azure-functions/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@
229229
href: functions-bindings-return-value.md
230230
- name: Handle binding errors
231231
href: functions-bindings-errors.md
232+
- name: Binding definitions for Python v2
233+
href: functions-bindings-triggers-python.md
232234
- name: Frameworks
233235
items:
234236
- name: Express.js

articles/azure-functions/create-first-function-cli-python.md

Lines changed: 104 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ ms.date: 06/15/2022
55
ms.topic: quickstart
66
ms.devlang: python
77
ms.custom: devx-track-python, devx-track-azurecli, devx-track-azurepowershell, mode-api, devdivchpfy22
8-
adobe-target: true
9-
adobe-target-activity: DocsExp–386541–A/B–Enhanced-Readability-Quickstarts–2.19.2021
10-
adobe-target-experience: Experience B
11-
adobe-target-content: ./create-first-function-cli-python-uiex
8+
zone_pivot_groups: python-mode-functions
9+
1210
---
1311

1412
# Quickstart: Create a Python function in Azure from the command line
1513

16-
[!INCLUDE [functions-language-selector-quickstart-cli](../../includes/functions-language-selector-quickstart-cli.md)]
14+
In this article, you use command-line tools to create a Python function that responds to HTTP requests. After testing the code locally, you deploy it to the serverless environment of Azure Functions.
15+
16+
This article covers both Python programming models supported by Azure Functions. Use the selector at the top to choose your programming model.
1717

18-
In this article, you use command-line tools to create a Python function that responds to HTTP requests. After testing the code locally, you deploy it to the serverless environment of Azure Functions.
18+
>[!NOTE]
19+
>The Python v2 programming model for Functions is currently in Preview. To learn more about the Python v2 programming model, see the [Developer Reference Guide](functions-reference-python.md).
1920
2021
Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account.
2122

@@ -27,24 +28,35 @@ Before you begin, you must have the following requirements in place:
2728

2829
+ An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio).
2930

31+
::: zone pivot="python-mode-configuration"
3032
+ The [Azure Functions Core Tools](functions-run-local.md#v2) version 4.x.
31-
33+
::: zone-end
34+
::: zone pivot="python-mode-decorators"
35+
+ The [Azure Functions Core Tools](functions-run-local.md#v2) version 4.0.4785 or later.
36+
::: zone-end
3237
+ One of the following tools for creating Azure resources:
3338

3439
+ [Azure CLI](/cli/azure/install-azure-cli) version 2.4 or later.
3540

3641
+ The Azure [Az PowerShell module](/powershell/azure/install-az-ps) version 5.9.0 or later.
3742

3843
+ [Python versions that are supported by Azure Functions](supported-languages.md#languages-by-runtime-version).
44+
::: zone pivot="python-mode-decorators"
45+
+ The [Azurite storage emulator](../storage/common/storage-use-azurite.md?tabs=npm#install-azurite). While you can also use an actual Azure Storage account, the article assumes you're using this emulator.
46+
::: zone-end
3947

4048
### Prerequisite check
4149

4250
Verify your prerequisites, which depend on whether you're using Azure CLI or Azure PowerShell for creating Azure resources.
4351

4452
# [Azure CLI](#tab/azure-cli)
4553

54+
::: zone pivot="python-mode-configuration"
4655
+ In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools version is 4.x.
47-
56+
::: zone-end
57+
::: zone pivot="python-mode-decorators"
58+
+ In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools version is 4.0.4785 or later.
59+
::: zone-end
4860
+ Run `az --version` to check that the Azure CLI version is 2.4 or later.
4961

5062
+ Run `az login` to sign in to Azure and verify an active subscription.
@@ -111,6 +123,7 @@ You run all subsequent commands in this activated virtual environment.
111123

112124
In Azure Functions, a function project is a container for one or more individual functions that each responds to a specific trigger. All functions in a project share the same local and hosting configurations. In this section, you create a function project that contains a single function.
113125

126+
::: zone pivot="python-mode-configuration"
114127
1. Run the `func init` command as follows to create a functions project in a folder named *LocalFunctionProj* with the specified runtime.
115128

116129
```console
@@ -138,11 +151,41 @@ In Azure Functions, a function project is a container for one or more individual
138151
```console
139152
func templates list -l python
140153
```
154+
::: zone-end
155+
::: zone pivot="python-mode-decorators"
156+
1. Run the `func init` command as follows to create a functions project in a folder named *LocalFunctionProj* with the specified runtime and the specified programming model version.
157+
158+
```console
159+
func init LocalFunctionProj --python -m V2
160+
```
161+
162+
1. Go to the project folder.
163+
164+
```console
165+
cd LocalFunctionProj
166+
```
167+
168+
This folder contains various files for the project, including configuration files named *[local.settings.json]*(functions-develop-local.md#local-settings-file) and *[host.json]*(functions-host-json.md). Because *local.settings.json* can contain secrets downloaded from Azure, the file is excluded from source control by default in the *.gitignore* file.
169+
170+
1. The file `function_app.py` can include all functions within your project. To start with, there's already an HTTP function stored in the file.
171+
172+
```python
173+
import azure.functions as func
174+
175+
app = func.FunctionApp()
176+
177+
@app.function_name(name="HttpTrigger1")
178+
@app.route(route="hello")
179+
def test_function(req: func.HttpRequest) -> func.HttpResponse:
180+
return func.HttpResponse("HttpTrigger1 function processed a request!")%
181+
```
182+
::: zone-end
141183

142184
### (Optional) Examine the file contents
143185

144186
If desired, you can skip to [Run the function locally](#run-the-function-locally) and examine the file contents later.
145187

188+
::: zone pivot="python-mode-configuration"
146189
#### \_\_init\_\_.py
147190

148191
*\_\_init\_\_.py* contains a `main()` Python function that's triggered according to the configuration in *function.json*.
@@ -160,6 +203,24 @@ If desired, you can change `scriptFile` to invoke a different Python file.
160203
:::code language="json" source="~/functions-quickstart-templates/Functions.Templates/Templates/HttpTrigger-Python/function.json":::
161204

162205
Each binding requires a direction, a type, and a unique name. The HTTP trigger has an input binding of type [`httpTrigger`](functions-bindings-http-webhook-trigger.md) and output binding of type [`http`](functions-bindings-http-webhook-output.md).
206+
::: zone-end
207+
::: zone pivot="python-mode-decorators"
208+
`function_app.py` is the entry point to the function and where functions will be stored and/or referenced. This file will include configuration of triggers and bindings through decorators, and the function content itself.
209+
210+
For more information, see [Azure Functions HTTP triggers and bindings](./functions-bindings-http-webhook.md?tabs=python).
211+
212+
## Start the storage emulator
213+
214+
Before running the function locally, you must start the local Azurite storage emulator. You can skip this step if the `AzureWebJobsStorage` setting in the local.settings.json file is set to the connection string for an Azure Storage account.
215+
216+
Use the following command to start the Azurite storage emulator:
217+
218+
```cmd
219+
azurite
220+
```
221+
222+
For more information, see [Run Azurite](../storage/common/storage-use-azurite.md?tabs=npm#run-azurite)
223+
::: zone-end
163224

164225
[!INCLUDE [functions-run-function-test-local-cli](../../includes/functions-run-function-test-local-cli.md)]
165226

@@ -180,7 +241,7 @@ Use the following commands to create these items. Both Azure CLI and PowerShell
180241
az login
181242
```
182243
183-
The [az login](/cli/azure/reference-index#az-login) command signs you into your Azure account.
244+
The [`az login`](/cli/azure/reference-index#az-login) command signs you into your Azure account.
184245
185246
# [Azure PowerShell](#tab/azure-powershell)
186247
```azurepowershell
@@ -224,6 +285,10 @@ Use the following commands to create these items. Both Azure CLI and PowerShell
224285
225286
---
226287
288+
::: zone pivot="python-mode-decorators"
289+
In the current v2 programming model preview, choose a region from one of the following locations: France Central, West Central US, North Europe, China East, East US, or North Central US.
290+
::: zone-end
291+
227292
> [!NOTE]
228293
> You can't host Linux and Windows apps in the same resource group. If you have an existing resource group named `AzureFunctionsQuickstart-rg` with a Windows function app or web app, you must use a different resource group.
229294
@@ -273,11 +338,35 @@ Use the following commands to create these items. Both Azure CLI and PowerShell
273338
274339
In the previous example, replace `<APP_NAME>` with a globally unique name appropriate to you. The `<APP_NAME>` is also the default DNS domain for the function app.
275340
276-
This command creates a function app running in your specified language runtime under the [Azure Functions Consumption Plan](consumption-plan.md), which is free for the amount of usage you incur here. The command also provisions an associated Azure Application Insights instance in the same resource group, with which you can monitor your function app and view logs. For more information, see [Monitor Azure Functions](functions-monitoring.md). The instance incurs no costs until you activate it.
341+
This command creates a function app running in your specified language runtime under the [Azure Functions Consumption Plan](consumption-plan.md), which is free for the amount of usage you incur here. The command also creates an associated Azure Application Insights instance in the same resource group, with which you can monitor your function app and view logs. For more information, see [Monitor Azure Functions](functions-monitoring.md). The instance incurs no costs until you activate it.
277342
278343
[!INCLUDE [functions-publish-project-cli](../../includes/functions-publish-project-cli.md)]
279344
280-
[!INCLUDE [functions-run-remote-azure-cli](../../includes/functions-run-remote-azure-cli.md)]
345+
::: zone pivot="python-mode-decorators"
346+
## Update app settings
347+
348+
To use the Python v2 model in your function app, you need to add a new application setting in Azure named `AzureWebJobsFeatureFlags` with a value of `EnableWorkerIndexing`. This setting is already in your local.settings.json file.
349+
350+
Run the following command to add this setting to your new function app in Azure.
351+
352+
# [Azure CLI](#tab/azure-cli)
353+
354+
```azurecli
355+
az functionapp config appsettings set --name <FUNCTION_APP_NAME> --resource-group <RESOURCE_GROUP_NAME> --settings AzureWebJobsFeatureFlags=EnableWorkerIndexing
356+
```
357+
358+
# [Azure PowerShell](#tab/azure-powershell)
359+
360+
```azurepowershell
361+
Update-AzFunctionAppSetting -Name <FUNCTION_APP_NAME> -ResourceGroupName <RESOURCE_GROUP_NAME> -AppSetting @{"AzureWebJobsFeatureFlags" = "EnableWorkerIndexing"}
362+
```
363+
364+
---
365+
366+
In the previous example, replace `<FUNCTION_APP_NAME>` and `<RESOURCE_GROUP_NAME>` with the name of your function app and resource group, respectively. This setting is already in your local.settings.json file.
367+
::: zone-end
368+
369+
## Verify in Azure
281370

282371
Run the following command to view near real-time [streaming logs](functions-run-local.md#enable-streaming-logs) in Application Insights in the Azure portal.
283372

@@ -294,4 +383,7 @@ In a separate terminal window or in the browser, call the remote function again.
294383
> [!div class="nextstepaction"]
295384
> [Connect to an Azure Storage queue](functions-add-output-binding-storage-queue-cli.md?pivots=programming-language-python)
296385
297-
[Having issues? Let us know.](https://aka.ms/python-functions-qs-survey)
386+
Having issues with this article?
387+
388+
+ [Troubleshoot Python function apps in Azure Functions](recover-python-functions.md)
389+
+ [Let us know](https://aka.ms/python-functions-qs-survey)

0 commit comments

Comments
 (0)