Skip to content

Commit 566fcea

Browse files
committed
test pivots
1 parent 298f451 commit 566fcea

File tree

3 files changed

+291
-3
lines changed

3 files changed

+291
-3
lines changed

articles/azure-functions/durable/TOC.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,14 @@
2626
- name: Create durable function - JavaScript
2727
displayName: get started, chaining
2828
href: quickstart-js-vscode.md
29-
- name: Create durable function - Python
30-
displayName: get started, chaining
31-
href: quickstart-python-vscode.md
29+
- name: CReate durable function - Python
30+
items:
31+
- name: Create durable function - Python (V1)
32+
displayName: get started, chaining
33+
href: quickstart-python-vscode.md
34+
- name: Create durable function - Python (V2)
35+
displayName: get started, chaining
36+
href: quickstart-python-vscode-v2.md
3237
- name: Create durable function - PowerShell
3338
displayName: get started, chaining
3439
href: quickstart-powershell-vscode.md
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
---
2+
title: Create your first durable function in Azure using Python
3+
description: Create and publish an Azure Durable Function in Python using Visual Studio Code.
4+
author: anthonychu
5+
ms.topic: quickstart
6+
ms.date: 06/15/2022
7+
ms.reviewer: azfuncdf, antchu
8+
ms.devlang: python
9+
ms.custom: mode-api, devdivchpfy22, vscode-azure-extension-update-complete
10+
---
11+
12+
# Create your first durable function in Python
13+
14+
Durable Functions is an extension of [Azure Functions](../functions-overview.md) that lets you write stateful functions in a serverless environment. The extension manages state, checkpoints, and restarts for you.
15+
16+
In this article, you learn how to use the Visual Studio Code Azure Functions extension to locally create and test a "hello world" durable function. This function will orchestrate and chains together calls to other functions. You can then publish the function code to Azure.
17+
18+
:::image type="content" source="./media/quickstart-python-vscode/functions-vs-code-complete.png" alt-text="Screenshot of the running durable function in Azure.":::
19+
20+
## Prerequisites
21+
22+
To complete this tutorial:
23+
24+
* Install [Visual Studio Code](https://code.visualstudio.com/download).
25+
26+
* Install the [Azure Functions](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions) Visual Studio Code extension.
27+
28+
* Make sure that you have the latest version of the [Azure Functions Core Tools](../functions-run-local.md).
29+
30+
* Durable Functions require an Azure storage account. You need an Azure subscription.
31+
32+
* Make sure that you have version 3.7, 3.8, or 3.9 of [Python](https://www.python.org/) installed.
33+
34+
[!INCLUDE [quickstarts-free-trial-note](../../../includes/quickstarts-free-trial-note.md)]
35+
36+
## <a name="create-an-azure-functions-project"></a>Create your local project
37+
38+
In this section, you use Visual Studio Code to create a local Azure Functions project.
39+
40+
1. In Visual Studio Code, press <kbd>F1</kbd> (or <kbd>Ctrl/Cmd+Shift+P</kbd>) to open the command palette. In the command palette, search for and select `Azure Functions: Create New Project...`.
41+
42+
:::image type="content" source="media/quickstart-python-vscode/functions-create-project.png" alt-text="Screenshot of Create function window.":::
43+
44+
1. Choose an empty folder location for your project and choose **Select**.
45+
46+
1. Follow the prompts and provide the following information:
47+
48+
| Prompt | Value | Description |
49+
| ------ | ----- | ----------- |
50+
| Select a language for your function app project | Python | Create a local Python Functions project. |
51+
| Select a version | Azure Functions v4 | You only see this option when the Core Tools aren't already installed. In this case, Core Tools are installed the first time you run the app. |
52+
| Python version | Python 3.7, 3.8, or 3.9 | Visual Studio Code will create a virtual environment with the version you select. |
53+
| Select a template for your project's first function | Skip for now | |
54+
| Select how you would like to open your project | Open in current window | Reopens Visual Studio Code in the folder you selected. |
55+
56+
Visual Studio Code installs the Azure Functions Core Tools if needed. It also creates a function app project in a folder. This project contains the [host.json](../functions-host-json.md) and [local.settings.json](../functions-develop-local.md#local-settings-file) configuration files.
57+
58+
A *requirements.txt* file is also created in the root folder. It specifies the Python packages required to run your function app.
59+
60+
## Install azure-functions-durable from PyPI
61+
62+
When you've created the project, the Azure Functions Visual Studio Code extension automatically creates a virtual environment with your selected Python version. You then need to activate the virtual environment in a terminal and install some dependencies required by Azure Functions and Durable Functions.
63+
64+
1. Open the *requirements.txt* in the editor and change its content to the following code:
65+
66+
```
67+
azure-functions
68+
azure-functions-durable
69+
```
70+
71+
1. Open the editor's integrated terminal in the current folder (<kbd>Ctrl+Shift+`</kbd>).
72+
73+
1. In the integrated terminal, activate the virtual environment in the current folder, depending on your operating system:
74+
75+
# [Linux](#tab/linux)
76+
77+
```bash
78+
source .venv/bin/activate
79+
```
80+
# [MacOS](#tab/macos)
81+
82+
```bash
83+
source .venv/bin/activate
84+
```
85+
86+
# [Windows](#tab/windows)
87+
88+
```powershell
89+
.venv\scripts\activate
90+
```
91+
92+
---
93+
94+
1. In the integrated terminal where the virtual environment is activated, use pip to install the packages you defined.
95+
96+
```bash
97+
python -m pip install -r requirements.txt
98+
```
99+
100+
## Create your functions
101+
102+
A basic Durable Functions app contains three functions:
103+
104+
* *Orchestrator function*: Describes a workflow that orchestrates other functions.
105+
* *Activity function*: It's called by the orchestrator function, performs work, and optionally returns a value.
106+
* *Client function*: It's a regular Azure Function that starts an orchestrator function. This example uses an HTTP triggered function.
107+
108+
### Orchestrator function
109+
110+
You use a template to create the durable function code in your project.
111+
112+
1. In the command palette, search for and select `Azure Functions: Create Function...`.
113+
114+
1. Follow the prompts and provide the following information:
115+
116+
| Prompt | Value | Description |
117+
| ------ | ----- | ----------- |
118+
| Select a template for your function | Durable Functions orchestrator | Create a Durable Functions orchestration |
119+
| Provide a function name | HelloOrchestrator | Name of your durable function |
120+
121+
You've added an orchestrator to coordinate activity functions. Open *HelloOrchestrator/\_\_init__.py* to see the orchestrator function. Each call to `context.call_activity` invokes an activity function named `Hello`.
122+
123+
Next, you'll add the referenced `Hello` activity function.
124+
125+
### Activity function
126+
127+
1. In the command palette, search for and select `Azure Functions: Create Function...`.
128+
129+
1. Follow the prompts and provide the following information:
130+
131+
| Prompt | Value | Description |
132+
| ------ | ----- | ----------- |
133+
| Select a template for your function | Durable Functions activity | Create an activity function |
134+
| Provide a function name | Hello | Name of your activity function |
135+
136+
You've added the `Hello` activity function that is invoked by the orchestrator. Open *Hello/\_\_init__.py* to see that it takes a name as input and returns a greeting. An activity function is where you'll perform actions such as making a database call or performing a computation.
137+
138+
Finally, you'll add an HTTP triggered function that starts the orchestration.
139+
140+
### Client function (HTTP starter)
141+
142+
1. In the command palette, search for and select `Azure Functions: Create Function...`.
143+
144+
1. Follow the prompts and provide the following information:
145+
146+
| Prompt | Value | Description |
147+
| ------ | ----- | ----------- |
148+
| Select a template for your function | Durable Functions HTTP starter | Create an HTTP starter function |
149+
| Provide a function name | DurableFunctionsHttpStart | Name of your client function |
150+
| Authorization level | Anonymous | For demo purposes, allow the function to be called without authentication |
151+
152+
You've added an HTTP triggered function that starts an orchestration. Open *DurableFunctionsHttpStart/\_\_init__.py* to see that it uses `client.start_new` to start a new orchestration. Then it uses `client.create_check_status_response` to return an HTTP response containing URLs that can be used to monitor and manage the new orchestration.
153+
154+
You now have a Durable Functions app that can be run locally and deployed to Azure.
155+
156+
## Test the function locally
157+
158+
Azure Functions Core Tools lets you run an Azure Functions project on your local development computer. If you don't have it installed, you're prompted to install these tools the first time you start a function from Visual Studio Code.
159+
160+
1. To test your function, set a breakpoint in the `Hello` activity function code (*Hello/\_\_init__.py*). Press <kbd>F5</kbd> or select `Debug: Start Debugging` from the command palette to start the function app project. Output from Core Tools is displayed in the **Terminal** panel.
161+
162+
> [!NOTE]
163+
> For more information on debugging, see [Durable Functions Diagnostics](durable-functions-diagnostics.md#debugging).
164+
165+
1. Durable Functions require an Azure storage account to run. When Visual Studio Code prompts you to select a storage account, select **Select storage account**.
166+
167+
:::image type="content" source="media/quickstart-python-vscode/functions-select-storage.png" alt-text="Screenshot of how to create a storage account.":::
168+
169+
1. Follow the prompts and provide the following information to create a new storage account in Azure:
170+
171+
| Prompt | Value | Description |
172+
| ------ | ----- | ----------- |
173+
| Select subscription | *name of your subscription* | Select your Azure subscription |
174+
| Select a storage account | Create a new storage account | |
175+
| Enter the name of the new storage account | *unique name* | Name of the storage account to create |
176+
| Select a resource group | *unique name* | Name of the resource group to create |
177+
| Select a location | *region* | Select a region close to you |
178+
179+
1. In the **Terminal** panel, copy the URL endpoint of your HTTP-triggered function.
180+
181+
:::image type="content" source="media/quickstart-python-vscode/functions-f5.png" alt-text="Screenshot of Azure local output.":::
182+
183+
1. Use your browser, or a tool like [Postman](https://www.getpostman.com/) or [cURL](https://curl.haxx.se/), send an HTTP request to the URL endpoint. Replace the last segment with the name of the orchestrator function (`HelloOrchestrator`). The URL must be similar to `http://localhost:7071/api/orchestrators/HelloOrchestrator`.
184+
185+
The response is the initial result from the HTTP function letting you know the durable orchestration has started successfully. It isn't yet the end result of the orchestration. The response includes a few useful URLs. For now, let's query the status of the orchestration.
186+
187+
1. Copy the URL value for `statusQueryGetUri`, paste it in the browser's address bar, and execute the request. Alternatively, you can also continue to use Postman to issue the GET request.
188+
189+
The request will query the orchestration instance for the status. You must get an eventual response, which shows the instance has completed and includes the outputs or results of the durable function. It looks like:
190+
191+
```json
192+
{
193+
"name": "HelloOrchestrator",
194+
"instanceId": "9a528a9e926f4b46b7d3deaa134b7e8a",
195+
"runtimeStatus": "Completed",
196+
"input": null,
197+
"customStatus": null,
198+
"output": [
199+
"Hello Tokyo!",
200+
"Hello Seattle!",
201+
"Hello London!"
202+
],
203+
"createdTime": "2020-03-18T21:54:49Z",
204+
"lastUpdatedTime": "2020-03-18T21:54:54Z"
205+
}
206+
```
207+
208+
1. To stop debugging, press <kbd>Shift+F5</kbd> in Visual Studio Code.
209+
210+
After you've verified that the function runs correctly on your local computer, it's time to publish the project to Azure.
211+
212+
[!INCLUDE [functions-create-function-app-vs-code](../../../includes/functions-sign-in-vs-code.md)]
213+
214+
[!INCLUDE [functions-publish-project-vscode](../../../includes/functions-publish-project-vscode.md)]
215+
216+
## Test your function in Azure
217+
218+
1. Copy the URL of the HTTP trigger from the **Output** panel. The URL that calls your HTTP-triggered function must be in this format: `http://<functionappname>.azurewebsites.net/api/orchestrators/HelloOrchestrator`
219+
220+
1. Paste this new URL for the HTTP request in your browser's address bar. You must get the same status response as before when using the published app.
221+
222+
## Next steps
223+
224+
You have used Visual Studio Code to create and publish a Python durable function app.
225+
226+
> [!div class="nextstepaction"]
227+
> [Learn about common durable function patterns](durable-functions-overview.md#application-patterns)

articles/azure-functions/durable/quickstart-python-vscode.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ ms.date: 06/15/2022
77
ms.reviewer: azfuncdf, antchu
88
ms.devlang: python
99
ms.custom: mode-api, devdivchpfy22, vscode-azure-extension-update-complete
10+
zone_pivot_groups: python-mode-functions
1011
---
1112

1213
# Create your first durable function in Python
@@ -43,6 +44,8 @@ In this section, you use Visual Studio Code to create a local Azure Functions pr
4344

4445
1. Choose an empty folder location for your project and choose **Select**.
4546

47+
::: zone pivot="python-mode-configuration"
48+
4649
1. Follow the prompts and provide the following information:
4750

4851
| Prompt | Value | Description |
@@ -53,6 +56,21 @@ In this section, you use Visual Studio Code to create a local Azure Functions pr
5356
| Select a template for your project's first function | Skip for now | |
5457
| Select how you would like to open your project | Open in current window | Reopens Visual Studio Code in the folder you selected. |
5558

59+
::: zone pivot="python-mode-decorators"
60+
61+
1. Follow the prompts and provide the following information:
62+
63+
| Prompt | Value | Description |
64+
| ------ | ----- | ----------- |
65+
| Select a language | Python (Programming Model V2) | Create a local Python Functions project using the V2 programming model. |
66+
| Select a version | Azure Functions v4 | You only see this option when the Core Tools aren't already installed. In this case, Core Tools are installed the first time you run the app. |
67+
| Python version | Python 3.7, 3.8, or 3.9 | Visual Studio Code will create a virtual environment with the version you select. |
68+
| Select a template for your project's first function | Skip for now | |
69+
| Select how you would like to open your project | Open in current window | Reopens Visual Studio Code in the folder you selected. |
70+
71+
::: zone-end
72+
73+
5674
Visual Studio Code installs the Azure Functions Core Tools if needed. It also creates a function app project in a folder. This project contains the [host.json](../functions-host-json.md) and [local.settings.json](../functions-develop-local.md#local-settings-file) configuration files.
5775

5876
A *requirements.txt* file is also created in the root folder. It specifies the Python packages required to run your function app.
@@ -105,6 +123,8 @@ A basic Durable Functions app contains three functions:
105123
* *Activity function*: It's called by the orchestrator function, performs work, and optionally returns a value.
106124
* *Client function*: It's a regular Azure Function that starts an orchestrator function. This example uses an HTTP triggered function.
107125
126+
::: zone pivot="python-mode-configuration"
127+
108128
### Orchestrator function
109129
110130
You use a template to create the durable function code in your project.
@@ -153,6 +173,42 @@ You've added an HTTP triggered function that starts an orchestration. Open *Dura
153173
154174
You now have a Durable Functions app that can be run locally and deployed to Azure.
155175
176+
::: zone pivot="python-mode-decorators"
177+
178+
Using the V2 Python programming model, all these functions can be placed in a single file. To do this, replace the contents of `function_app.py` with the following code.
179+
180+
``Python
181+
import azure.functions as func
182+
import azure.durable_functions as df
183+
184+
myApp = df.DFApp(http_auth_level=func.AuthLevel.ANONYMOUS)
185+
186+
# An HTTP-Triggered Function with a Durable Functions Client binding
187+
@myApp.route(route="orchestrators/{functionName}")
188+
@myApp.durable_client_input(client_name="client")
189+
async def durable_trigger(req: func.HttpRequest, client):
190+
function_name = req.route_params.get('functionName')
191+
instance_id = await client.start_new(function_name)
192+
response = client.create_check_status_response(req, instance_id)
193+
return response
194+
195+
# Orchestrator
196+
@myApp.orchestration_trigger(context_name="context")
197+
def my_orchestrator(context):
198+
result1 = yield context.call_activity("hello", "Seattle")
199+
result2 = yield context.call_activity("hello", "Tokyo")
200+
result3 = yield context.call_activity("hello", "London")
201+
202+
return [result1, result2, result3]
203+
204+
# Activity
205+
@myApp.activity_trigger(input_name="myInput")
206+
def hello(myInput: str):
207+
return "Hello " + myInput
208+
```
209+
210+
::: zone-end
211+
156212
## Test the function locally
157213

158214
Azure Functions Core Tools lets you run an Azure Functions project on your local development computer. If you don't have it installed, you're prompted to install these tools the first time you start a function from Visual Studio Code.

0 commit comments

Comments
 (0)