Skip to content

Commit 09d40c7

Browse files
Merge pull request #273533 from shreyabatra4/shbatr/signalr
Added Python v2 example for signalr
2 parents a7ed2c4 + 39b8b27 commit 09d40c7

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ms.topic: quickstart
88
ms.service: signalr
99
ms.devlang: python
1010
ms.custom: devx-track-python, mode-api
11+
zone_pivot_groups: python-mode-functions
1112
---
1213
# Quickstart: Create a serverless app with Azure Functions and Azure SignalR Service in Python
1314

@@ -33,6 +34,7 @@ This quickstart can be run on macOS, Windows, or Linux. You will need the follo
3334

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

37+
::: zone pivot="python-mode-decorators"
3638
## Create the Azure Function project
3739

3840
Create a local Azure Function project.
@@ -54,6 +56,102 @@ After you initialize a project, you need to create functions. This project requi
5456
- `negotiate`: Allows a client to get an access token.
5557
- `broadcast`: Uses a time trigger to periodically broadcast messages to all clients.
5658

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+
::: zone-end
132+
133+
::: zone pivot="python-mode-configuration"
134+
## Create the Azure Function project
135+
136+
Create a local Azure Function project.
137+
138+
1. From a command line, create a directory for your project.
139+
1. Change to the project directory.
140+
1. Use the Azure Functions `func init` command to initialize your function project.
141+
142+
```bash
143+
# Initialize a function project
144+
func init --worker-runtime python --model v1
145+
```
146+
147+
## Create the functions
148+
149+
After you initialize a project, you need to create functions. This project requires three functions:
150+
151+
- `index`: Hosts a web page for a client.
152+
- `negotiate`: Allows a client to get an access token.
153+
- `broadcast`: Uses a time trigger to periodically broadcast messages to all clients.
154+
57155
When you run the `func new` command from the root directory of the project, the Azure Functions Core Tools creates default function source files and stores them in a folder named after the function. You'll edit the files as necessary replacing the default code with the app code.
58156

59157
### Create the index function
@@ -212,6 +310,7 @@ You can use this sample function as a template for your own functions.
212310
'arguments': [ 'Current star count of https://github.com/Azure/azure-signalr is: ' + str(start_count) ]
213311
}))
214312
```
313+
::: zone-end
215314

216315
### Create the index.html file
217316

0 commit comments

Comments
 (0)