Skip to content

Commit 0d49389

Browse files
authored
switched order of v1 and v2
1 parent e34d4f1 commit 0d49389

File tree

1 file changed

+96
-96
lines changed

1 file changed

+96
-96
lines changed

articles/azure-signalr/signalr-quickstart-azure-functions-python.md

Lines changed: 96 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,102 @@ This quickstart can be run on macOS, Windows, or Linux. You will need the follo
3333

3434
[!INCLUDE [Create instance](includes/signalr-quickstart-create-instance.md)]
3535

36+
#### [Python v2 model](#tab/python-v2)
37+
38+
## Create the Azure Function project
39+
40+
Create a local Azure Function project.
41+
42+
1. From a command line, create a directory for your project.
43+
1. Change to the project directory.
44+
1. Use the Azure Functions `func init` command to initialize your function project.
45+
46+
```bash
47+
# Initialize a function project
48+
func init --worker-runtime python
49+
```
50+
51+
## Create the functions
52+
53+
After you initialize a project, you need to create functions. This project requires three functions:
54+
55+
- `index`: Hosts a web page for a client.
56+
- `negotiate`: Allows a client to get an access token.
57+
- `broadcast`: Uses a time trigger to periodically broadcast messages to all clients.
58+
59+
When you run the `func new` command from the root directory of the project, the Azure Functions Core Tools appends the function code in the `function_app.py` file. You'll edit the parameters ad content as necessary by replacing the default code with the app code.
60+
61+
### Create the index function
62+
63+
You can use this sample function as a template for your own functions.
64+
65+
Open the file `function_app.py`. This file will contain your functions. First, modify the file to include the neccessary import statements, and define global variables that we will be using in the following functions.
66+
67+
```python
68+
import azure.functions as func
69+
import os
70+
import requests
71+
import json
72+
73+
app = func.FunctionApp()
74+
75+
etag = ''
76+
start_count = 0
77+
```
78+
79+
2. Add the function `index` by adding the following code
80+
81+
```python
82+
@app.route(route="index", auth_level=func.AuthLevel.ANONYMOUS)
83+
def index(req: func.HttpRequest) -> func.HttpResponse:
84+
f = open(os.path.dirname(os.path.realpath(__file__)) + '/content/index.html')
85+
return func.HttpResponse(f.read(), mimetype='text/html')
86+
```
87+
88+
This function hosts a web page for a client.
89+
90+
### Create the negotiate function
91+
92+
Add the function `negotiate` by adding the following code
93+
94+
```python
95+
@app.route(route="negotiate", auth_level=func.AuthLevel.ANONYMOUS, methods=["POST"])
96+
@app.generic_input_binding(arg_name="connectionInfo", type="signalRConnectionInfo", hubName="serverless", connectionStringSetting="AzureSignalRConnectionString")
97+
def negotiate(req: func.HttpRequest, connectionInfo) -> func.HttpResponse:
98+
return func.HttpResponse(connectionInfo)
99+
```
100+
101+
This function allows a client to get an access token.
102+
103+
### Create a broadcast function.
104+
105+
Add the function `broadcast` by adding the following code
106+
107+
```python
108+
@app.timer_trigger(schedule="*/1 * * * *", arg_name="myTimer",
109+
run_on_startup=False,
110+
use_monitor=False)
111+
@app.generic_output_binding(arg_name="signalRMessages", type="signalR", hubName="serverless", connectionStringSetting="AzureSignalRConnectionString")
112+
def broadcast(myTimer: func.TimerRequest, signalRMessages: func.Out[str]) -> None:
113+
global etag
114+
global start_count
115+
headers = {'User-Agent': 'serverless', 'If-None-Match': etag}
116+
res = requests.get('https://api.github.com/repos/azure/azure-functions-python-worker', headers=headers)
117+
if res.headers.get('ETag'):
118+
etag = res.headers.get('ETag')
119+
120+
if res.status_code == 200:
121+
jres = res.json()
122+
start_count = jres['stargazers_count']
123+
124+
signalRMessages.set(json.dumps({
125+
'target': 'newMessage',
126+
'arguments': [ 'Current star count of https://api.github.com/repos/azure/azure-functions-python-worker is: ' + str(start_count) ]
127+
}))
128+
```
129+
130+
This function uses a time trigger to periodically broadcast messages to all clients.
131+
36132
#### [Python v1 model](#tab/python-v1)
37133

38134
## Create the Azure Function project
@@ -215,102 +311,6 @@ You can use this sample function as a template for your own functions.
215311
}))
216312
```
217313

218-
#### [Python v2 model](#tab/python-v2)
219-
220-
## Create the Azure Function project
221-
222-
Create a local Azure Function project.
223-
224-
1. From a command line, create a directory for your project.
225-
1. Change to the project directory.
226-
1. Use the Azure Functions `func init` command to initialize your function project.
227-
228-
```bash
229-
# Initialize a function project
230-
func init --worker-runtime python
231-
```
232-
233-
## Create the functions
234-
235-
After you initialize a project, you need to create functions. This project requires three functions:
236-
237-
- `index`: Hosts a web page for a client.
238-
- `negotiate`: Allows a client to get an access token.
239-
- `broadcast`: Uses a time trigger to periodically broadcast messages to all clients.
240-
241-
When you run the `func new` command from the root directory of the project, the Azure Functions Core Tools appends the function code in the `function_app.py` file. You'll edit the parameters ad content as necessary by replacing the default code with the app code.
242-
243-
### Create the index function
244-
245-
You can use this sample function as a template for your own functions.
246-
247-
Open the file `function_app.py`. This file will contain your functions. First, modify the file to include the neccessary import statements, and define global variables that we will be using in the following functions.
248-
249-
```python
250-
import azure.functions as func
251-
import os
252-
import requests
253-
import json
254-
255-
app = func.FunctionApp()
256-
257-
etag = ''
258-
start_count = 0
259-
```
260-
261-
2. Add the function `index` by adding the following code
262-
263-
```python
264-
@app.route(route="index", auth_level=func.AuthLevel.ANONYMOUS)
265-
def index(req: func.HttpRequest) -> func.HttpResponse:
266-
f = open(os.path.dirname(os.path.realpath(__file__)) + '/content/index.html')
267-
return func.HttpResponse(f.read(), mimetype='text/html')
268-
```
269-
270-
This function hosts a web page for a client.
271-
272-
### Create the negotiate function
273-
274-
Add the function `negotiate` by adding the following code
275-
276-
```python
277-
@app.route(route="negotiate", auth_level=func.AuthLevel.ANONYMOUS, methods=["POST"])
278-
@app.generic_input_binding(arg_name="connectionInfo", type="signalRConnectionInfo", hubName="serverless", connectionStringSetting="AzureSignalRConnectionString")
279-
def negotiate(req: func.HttpRequest, connectionInfo) -> func.HttpResponse:
280-
return func.HttpResponse(connectionInfo)
281-
```
282-
283-
This function allows a client to get an access token.
284-
285-
### Create a broadcast function.
286-
287-
Add the function `broadcast` by adding the following code
288-
289-
```python
290-
@app.timer_trigger(schedule="*/1 * * * *", arg_name="myTimer",
291-
run_on_startup=False,
292-
use_monitor=False)
293-
@app.generic_output_binding(arg_name="signalRMessages", type="signalR", hubName="serverless", connectionStringSetting="AzureSignalRConnectionString")
294-
def broadcast(myTimer: func.TimerRequest, signalRMessages: func.Out[str]) -> None:
295-
global etag
296-
global start_count
297-
headers = {'User-Agent': 'serverless', 'If-None-Match': etag}
298-
res = requests.get('https://api.github.com/repos/azure/azure-functions-python-worker', headers=headers)
299-
if res.headers.get('ETag'):
300-
etag = res.headers.get('ETag')
301-
302-
if res.status_code == 200:
303-
jres = res.json()
304-
start_count = jres['stargazers_count']
305-
306-
signalRMessages.set(json.dumps({
307-
'target': 'newMessage',
308-
'arguments': [ 'Current star count of https://api.github.com/repos/azure/azure-functions-python-worker is: ' + str(start_count) ]
309-
}))
310-
```
311-
312-
This function uses a time trigger to periodically broadcast messages to all clients.
313-
314314
---
315315

316316
### Create the index.html file

0 commit comments

Comments
 (0)