Skip to content

Commit fed1ee0

Browse files
authored
Merge pull request #221001 from JialinXin/patch-12
[WebPubSub] Add python section.
2 parents 003a825 + 198ecfc commit fed1ee0

File tree

1 file changed

+201
-58
lines changed

1 file changed

+201
-58
lines changed

articles/azure-web-pubsub/tutorial-serverless-notification.md

Lines changed: 201 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ In this tutorial, you learn how to:
4242

4343
* The [Azure CLI](/cli/azure) to manage Azure resources.
4444

45+
# [Python](#tab/python)
46+
47+
* A code editor, such as [Visual Studio Code](https://code.visualstudio.com/).
48+
49+
* [Python](https://www.python.org/downloads/) (v3.6 ~ v3.9). See [supported Python versions](../azure-functions/functions-reference-python.md#python-version).
50+
51+
* [Azure Functions Core Tools](https://github.com/Azure/azure-functions-core-tools#installing) (v3 or higher preferred) to run Azure Function apps locally and deploy to Azure.
52+
53+
* The [Azure CLI](/cli/azure) to manage Azure resources.
54+
4555
---
4656

4757
[!INCLUDE [quickstarts-free-trial-note](../../includes/quickstarts-free-trial-note.md)]
@@ -61,6 +71,11 @@ In this tutorial, you learn how to:
6171
```bash
6272
func init --worker-runtime dotnet
6373
```
74+
75+
# [Python](#tab/python)
76+
```bash
77+
func init --worker-runtime python
78+
```
6479

6580
2. Install `Microsoft.Azure.WebJobs.Extensions.WebPubSub`.
6681

@@ -81,6 +96,18 @@ In this tutorial, you learn how to:
8196
dotnet add package Microsoft.Azure.WebJobs.Extensions.WebPubSub
8297
```
8398
99+
# [Python](#tab/python)
100+
Update `host.json`'s extensionBundle to version _3.3.0_ or later to get Web PubSub support.
101+
```json
102+
{
103+
"version": "2.0",
104+
"extensionBundle": {
105+
"id": "Microsoft.Azure.Functions.ExtensionBundle",
106+
"version": "[3.3.*, 4.0.0)"
107+
}
108+
}
109+
```
110+
84111
3. Create an `index` function to read and host a static web page for clients.
85112
```bash
86113
func new -n index -t HttpTrigger
@@ -89,23 +116,23 @@ In this tutorial, you learn how to:
89116
- Update `index/function.json` and copy following json codes.
90117
```json
91118
{
92-
"bindings": [
93-
{
94-
"authLevel": "anonymous",
95-
"type": "httpTrigger",
96-
"direction": "in",
97-
"name": "req",
98-
"methods": [
99-
"get",
100-
"post"
101-
]
102-
},
103-
{
104-
"type": "http",
105-
"direction": "out",
106-
"name": "res"
107-
}
108-
]
119+
"bindings": [
120+
{
121+
"authLevel": "anonymous",
122+
"type": "httpTrigger",
123+
"direction": "in",
124+
"name": "req",
125+
"methods": [
126+
"get",
127+
"post"
128+
]
129+
},
130+
{
131+
"type": "http",
132+
"direction": "out",
133+
"name": "res"
134+
}
135+
]
109136
}
110137
```
111138
- Update `index/index.js` and copy following codes.
@@ -157,6 +184,41 @@ In this tutorial, you learn how to:
157184
}
158185
```
159186
187+
# [Python](#tab/python)
188+
- Update `index/function.json` and copy following json codes.
189+
```json
190+
{
191+
"scriptFile": "__init__.py",
192+
"bindings": [
193+
{
194+
"authLevel": "anonymous",
195+
"type": "httpTrigger",
196+
"direction": "in",
197+
"name": "req",
198+
"methods": [
199+
"get",
200+
"post"
201+
]
202+
},
203+
{
204+
"type": "http",
205+
"direction": "out",
206+
"name": "$return"
207+
}
208+
]
209+
}
210+
```
211+
- Update `index/__init__.py` and copy following codes.
212+
```py
213+
import os
214+
215+
import azure.functions as func
216+
217+
def main(req: func.HttpRequest) -> func.HttpResponse:
218+
f = open(os.path.dirname(os.path.realpath(__file__)) + '/../index.html')
219+
return func.HttpResponse(f.read(), mimetype='text/html')
220+
```
221+
160222
4. Create a `negotiate` function to help clients get service connection url with access token.
161223
```bash
162224
func new -n negotiate -t HttpTrigger
@@ -165,25 +227,25 @@ In this tutorial, you learn how to:
165227
- Update `negotiate/function.json` and copy following json codes.
166228
```json
167229
{
168-
"bindings": [
169-
{
170-
"authLevel": "anonymous",
171-
"type": "httpTrigger",
172-
"direction": "in",
173-
"name": "req"
174-
},
175-
{
176-
"type": "http",
177-
"direction": "out",
178-
"name": "res"
179-
},
180-
{
181-
"type": "webPubSubConnection",
182-
"name": "connection",
183-
"hub": "notification",
184-
"direction": "in"
185-
}
186-
]
230+
"bindings": [
231+
{
232+
"authLevel": "anonymous",
233+
"type": "httpTrigger",
234+
"direction": "in",
235+
"name": "req"
236+
},
237+
{
238+
"type": "http",
239+
"direction": "out",
240+
"name": "res"
241+
},
242+
{
243+
"type": "webPubSubConnection",
244+
"name": "connection",
245+
"hub": "notification",
246+
"direction": "in"
247+
}
248+
]
187249
}
188250
```
189251
- Update `negotiate/index.js` and copy following codes.
@@ -211,32 +273,68 @@ In this tutorial, you learn how to:
211273
```c#
212274
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;
213275
```
276+
# [Python](#tab/python)
277+
- Update `negotiate/function.json` and copy following json codes.
278+
```json
279+
{
280+
"scriptFile": "__init__.py",
281+
"bindings": [
282+
{
283+
"authLevel": "anonymous",
284+
"type": "httpTrigger",
285+
"direction": "in",
286+
"name": "req"
287+
},
288+
{
289+
"type": "http",
290+
"direction": "out",
291+
"name": "$return"
292+
},
293+
{
294+
"type": "webPubSubConnection",
295+
"name": "connection",
296+
"hub": "notification",
297+
"direction": "in"
298+
}
299+
]
300+
}
301+
```
302+
- Update `negotiate/__init__.py` and copy following codes.
303+
```py
304+
import logging
305+
306+
import azure.functions as func
307+
308+
309+
def main(req: func.HttpRequest, connection) -> func.HttpResponse:
310+
return func.HttpResponse(connection)
311+
```
214312
215313
5. Create a `notification` function to generate notifications with `TimerTrigger`.
216-
```bash
314+
```bash
217315
func new -n notification -t TimerTrigger
218316
```
219317
# [JavaScript](#tab/javascript)
220-
- Update `notification/function.json` and copy following json codes.
318+
- Update `notification/function.json` and copy following json codes.
221319
```json
222320
{
223-
"bindings": [
224-
{
225-
"name": "myTimer",
226-
"type": "timerTrigger",
227-
"direction": "in",
228-
"schedule": "*/10 * * * * *"
229-
},
230-
{
231-
"type": "webPubSub",
232-
"name": "actions",
233-
"hub": "notification",
234-
"direction": "out"
235-
}
236-
]
321+
"bindings": [
322+
{
323+
"name": "myTimer",
324+
"type": "timerTrigger",
325+
"direction": "in",
326+
"schedule": "*/10 * * * * *"
327+
},
328+
{
329+
"type": "webPubSub",
330+
"name": "actions",
331+
"hub": "notification",
332+
"direction": "out"
333+
}
334+
]
237335
}
238336
```
239-
- Update `notification/index.js` and copy following codes.
337+
- Update `notification/index.js` and copy following codes.
240338
```js
241339
module.exports = function (context, myTimer) {
242340
context.bindings.actions = {
@@ -251,8 +349,8 @@ In this tutorial, you learn how to:
251349
return (baseNum + 2 * floatNum * (Math.random() - 0.5)).toFixed(3);
252350
}
253351
```
254-
# [C#](#tab/csharp)
255-
- Update `notification.cs` and replace `Run` function with following codes.
352+
# [C#](#tab/csharp)
353+
- Update `notification.cs` and replace `Run` function with following codes.
256354
```c#
257355
[FunctionName("notification")]
258356
public static async Task Run([TimerTrigger("*/10 * * * * *")]TimerInfo myTimer, ILogger log,
@@ -272,11 +370,48 @@ In this tutorial, you learn how to:
272370
return value.ToString("0.000");
273371
}
274372
```
275-
- Add below `using` statements in header to resolve required dependencies.
373+
- Add below `using` statements in header to resolve required dependencies.
276374
```c#
277375
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;
278376
using Microsoft.Azure.WebPubSub.Common;
279377
```
378+
# [Python](#tab/python)
379+
- Update `notification/function.json` and copy following json codes.
380+
```json
381+
{
382+
"scriptFile": "__init__.py",
383+
"bindings": [
384+
{
385+
"name": "myTimer",
386+
"type": "timerTrigger",
387+
"direction": "in",
388+
"schedule": "*/10 * * * * *"
389+
},
390+
{
391+
"type": "webPubSub",
392+
"name": "actions",
393+
"hub": "notification",
394+
"direction": "out"
395+
}
396+
]
397+
}
398+
```
399+
- Update `notification/__init__.py` and copy following codes.
400+
```py
401+
import datetime
402+
import random
403+
import json
404+
405+
import azure.functions as func
406+
407+
def main(myTimer: func.TimerRequest, actions: func.Out[str]) -> None:
408+
time = datetime.datetime.now().strftime("%A %d-%b-%Y %H:%M:%S")
409+
actions.set(json.dumps({
410+
'actionName': 'sendToAll',
411+
'data': '\x5B DateTime: {0} \x5D Temperature: {1:.3f} \xB0C, Humidity: {2:.3f} \x25'.format(time, 22 + 2 * (random.random() - 0.5), 44 + 4 * (random.random() - 0.5)),
412+
'dataType': 'text'
413+
}))
414+
```
280415
281416
6. Add the client single page `index.html` in the project root folder and copy content as below.
282417
```html
@@ -302,7 +437,7 @@ In this tutorial, you learn how to:
302437
</body>
303438
</html>
304439
```
305-
440+
306441
# [JavaScript](#tab/javascript)
307442
308443
# [C#](#tab/csharp)
@@ -314,6 +449,8 @@ In this tutorial, you learn how to:
314449
</None>
315450
</ItemGroup>
316451
```
452+
453+
# [Python](#tab/python)
317454
318455
7. Configure and run the Azure Function app
319456
@@ -337,7 +474,7 @@ In this tutorial, you learn how to:
337474
func start
338475
```
339476
340-
And checking the running logs, you can visit your local host static page by visiting: `https://localhost:7071/api/index`.
477+
And checking the running logs, you can visit your local host static page by visiting: `http://localhost:7071/api/index`.
341478
342479
## Deploy Function App to Azure
343480
@@ -382,6 +519,12 @@ Use the following commands to create these item.
382519
az functionapp create --resource-group WebPubSubFunction --consumption-plan-location <REGION> --runtime dotnet --functions-version 3 --name <FUNCIONAPP_NAME> --storage-account <STORAGE_NAME>
383520
```
384521
522+
# [Python](#tab/python)
523+
524+
```azurecli
525+
az functionapp create --resource-group WebPubSubFunction --consumption-plan-location <REGION> --runtime python --runtime-version 3.9 --functions-version 3 --name <FUNCIONAPP_NAME> --os-type linux --storage-account <STORAGE_NAME>
526+
```
527+
385528
1. Deploy the function project to Azure:
386529
387530
After you've successfully created your function app in Azure, you're now ready to deploy your local functions project by using the [func azure functionapp publish](../azure-functions/functions-run-local.md) command.

0 commit comments

Comments
 (0)