@@ -42,6 +42,16 @@ In this tutorial, you learn how to:
42
42
43
43
* The [ Azure CLI] ( /cli/azure ) to manage Azure resources.
44
44
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
+
45
55
---
46
56
47
57
[ !INCLUDE [ quickstarts-free-trial-note] ( ../../includes/quickstarts-free-trial-note.md )]
@@ -61,6 +71,11 @@ In this tutorial, you learn how to:
61
71
` ` ` bash
62
72
func init --worker-runtime dotnet
63
73
` ` `
74
+
75
+ # [Python](#tab/python)
76
+ ` ` ` bash
77
+ func init --worker-runtime python
78
+ ` ` `
64
79
65
80
2. Install ` Microsoft.Azure.WebJobs.Extensions.WebPubSub` .
66
81
@@ -81,6 +96,18 @@ In this tutorial, you learn how to:
81
96
dotnet add package Microsoft.Azure.WebJobs.Extensions.WebPubSub
82
97
```
83
98
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
+
84
111
3. Create an ` index` function to read and host a static web page for clients.
85
112
` ` ` bash
86
113
func new -n index -t HttpTrigger
@@ -89,23 +116,23 @@ In this tutorial, you learn how to:
89
116
- Update ` index/function.json` and copy following json codes.
90
117
` ` ` json
91
118
{
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
+ ]
109
136
}
110
137
` ` `
111
138
- Update ` index/index.js` and copy following codes.
@@ -157,6 +184,41 @@ In this tutorial, you learn how to:
157
184
}
158
185
` ` `
159
186
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
+
160
222
4. Create a ` negotiate` function to help clients get service connection url with access token.
161
223
` ` ` bash
162
224
func new -n negotiate -t HttpTrigger
@@ -165,25 +227,25 @@ In this tutorial, you learn how to:
165
227
- Update ` negotiate/function.json` and copy following json codes.
166
228
` ` ` json
167
229
{
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
+ ]
187
249
}
188
250
` ` `
189
251
- Update ` negotiate/index.js` and copy following codes.
@@ -211,32 +273,68 @@ In this tutorial, you learn how to:
211
273
` ` ` c#
212
274
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;
213
275
` ` `
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
+ ` ` `
214
312
215
313
5. Create a ` notification` function to generate notifications with ` TimerTrigger` .
216
- ```bash
314
+ ` ` ` bash
217
315
func new -n notification -t TimerTrigger
218
316
` ` `
219
317
# [JavaScript](#tab/javascript)
220
- - Update `notification/function.json` and copy following json codes.
318
+ - Update ` notification/function.json` and copy following json codes.
221
319
` ` ` json
222
320
{
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
+ ]
237
335
}
238
336
` ` `
239
- - Update `notification/index.js` and copy following codes.
337
+ - Update ` notification/index.js` and copy following codes.
240
338
` ` ` js
241
339
module.exports = function (context, myTimer) {
242
340
context.bindings.actions = {
@@ -251,8 +349,8 @@ In this tutorial, you learn how to:
251
349
return (baseNum + 2 * floatNum * (Math.random() - 0.5)).toFixed(3);
252
350
}
253
351
` ` `
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.
256
354
` ` ` c#
257
355
[FunctionName(" notification" )]
258
356
public static async Task Run([TimerTrigger(" */10 * * * * *" )]TimerInfo myTimer, ILogger log,
@@ -272,11 +370,48 @@ In this tutorial, you learn how to:
272
370
return value.ToString(" 0.000" );
273
371
}
274
372
` ` `
275
- - Add below `using` statements in header to resolve required dependencies.
373
+ - Add below ` using` statements in header to resolve required dependencies.
276
374
` ` ` c#
277
375
using Microsoft.Azure.WebJobs.Extensions.WebPubSub;
278
376
using Microsoft.Azure.WebPubSub.Common;
279
377
` ` `
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
+ ` ` `
280
415
281
416
6. Add the client single page ` index.html` in the project root folder and copy content as below.
282
417
` ` ` html
@@ -302,7 +437,7 @@ In this tutorial, you learn how to:
302
437
< /body>
303
438
< /html>
304
439
` ` `
305
-
440
+
306
441
# [JavaScript](#tab/javascript)
307
442
308
443
# [C#](#tab/csharp)
@@ -314,6 +449,8 @@ In this tutorial, you learn how to:
314
449
< /None>
315
450
< /ItemGroup>
316
451
` ` `
452
+
453
+ # [Python](#tab/python)
317
454
318
455
7. Configure and run the Azure Function app
319
456
@@ -337,7 +474,7 @@ In this tutorial, you learn how to:
337
474
func start
338
475
` ` `
339
476
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` .
341
478
342
479
# # Deploy Function App to Azure
343
480
@@ -382,6 +519,12 @@ Use the following commands to create these item.
382
519
az functionapp create --resource-group WebPubSubFunction --consumption-plan-location < REGION> --runtime dotnet --functions-version 3 --name < FUNCIONAPP_NAME> --storage-account < STORAGE_NAME>
383
520
` ` `
384
521
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
+
385
528
1. Deploy the function project to Azure:
386
529
387
530
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