@@ -29,86 +29,28 @@ Are you referencing pip modules which require a native build toolchain? It's adv
29
29
30
30
## Downloading the templates
31
31
32
- Using template pull:
32
+ Using template pull with the repository's URL :
33
33
34
34
``` bash
35
- faas template pull https://github.com/openfaas-incubator/python-flask-template
35
+ faas-cli template pull https://github.com/openfaas-incubator/python-flask-template
36
36
```
37
37
38
- Using template store:
38
+ Using the template store:
39
39
40
40
``` bash
41
- faas template store pull python3-flask
42
- ```
43
-
44
- # Using the python3-flask template
45
-
46
- Create a new function
47
-
48
- ```
49
- export OPENFAAS_PREFIX=alexellis2
50
- export FN="tester"
51
- faas new --lang python3-flask $FN
52
- ```
53
-
54
- Build, push, and deploy
55
-
56
- ```
57
- faas up -f $FN.yml
58
- ```
59
-
60
- Test the new function
61
-
62
- ```
63
- echo -n content | faas invoke $FN
64
- ```
65
-
66
- ## Example of returning a string
67
-
68
- ``` python
69
- def handle (req ):
70
- """ handle a request to the function
71
- Args:
72
- req (str): request body
73
- """
74
-
75
- return " Hi" + str (req)
76
- ```
77
-
78
- ## Example of returning a custom HTTP code
79
-
80
- ``` python
81
- def handle (req ):
82
- return " request accepted" , 201
83
- ```
41
+ # Either command downloads both templates
42
+ faas-cli template store pull python3-http
84
43
85
- ## Example of returning a custom HTTP code and content-type
86
-
87
- ``` python
88
- def handle (req ):
89
- return " request accepted" , 201 , {" Content-Type" :" binary/octet-stream" }
44
+ # Or
45
+ faas-cli template store pull python3-flask
90
46
```
91
47
92
- ## Example of accepting raw bytes in the request
93
-
94
- Update stack.yml:
48
+ Using your ` stack.yml ` file:
95
49
96
50
``` yaml
97
- environment :
98
- RAW_BODY : True
99
- ` ` `
100
-
101
- > Note: the value for ` RAW_BODY` is case-sensitive.
102
-
103
- ` ` ` python
104
- def handle(req):
105
- """handle a request to the function
106
- Args:
107
- req (str): request body
108
- """
109
-
110
- # req is bytes, so an input of "hello" returns i.e. b'hello'
111
- return str(req)
51
+ configuration :
52
+ templates :
53
+ - name : python3-http
112
54
` ` `
113
55
114
56
# Using the python3-http templates
@@ -118,19 +60,19 @@ Create a new function
118
60
` ` `
119
61
export OPENFAAS_PREFIX=alexellis2
120
62
export FN="tester"
121
- faas new --lang python3-http $FN
63
+ faas-cli new --lang python3-http $FN
122
64
```
123
65
124
66
Build, push, and deploy
125
67
126
68
```
127
- faas up -f $FN.yml
69
+ faas-cli up -f $FN.yml
128
70
```
129
71
130
72
Test the new function
131
73
132
74
```
133
- echo -n content | faas invoke $FN
75
+ echo -n content | faas-cli invoke $FN
134
76
```
135
77
136
78
## Event and Context Data
@@ -155,6 +97,19 @@ For example, returning a dict object type will automatically attach the header `
155
97
156
98
## Example usage
157
99
100
+ ### Return a JSON body with a Content-Type
101
+
102
+ ``` python
103
+ def handle (event , context ):
104
+ return {
105
+ " statusCode" : 200 ,
106
+ " body" : {" message" : " Hello from OpenFaaS!" },
107
+ " headers" : {
108
+ " Content-Type" : " application/json"
109
+ }
110
+ }
111
+ ```
112
+
158
113
### Custom status codes and response bodies
159
114
160
115
Successful response status code and JSON response body
@@ -243,6 +198,76 @@ def handle(event, context):
243
198
}
244
199
```
245
200
201
+ # Using the python3-flask template
202
+
203
+ Create a new function
204
+
205
+ ```
206
+ export OPENFAAS_PREFIX=alexellis2
207
+ export FN="tester"
208
+ faas-cli new --lang python3-flask $FN
209
+ ```
210
+
211
+ Build, push, and deploy
212
+
213
+ ```
214
+ faas-cli up -f $FN.yml
215
+ ```
216
+
217
+ Test the new function
218
+
219
+ ```
220
+ echo -n content | faas-cli invoke $FN
221
+ ```
222
+
223
+ ## Example of returning a string
224
+
225
+ ``` python
226
+ def handle (req ):
227
+ """ handle a request to the function
228
+ Args:
229
+ req (str): request body
230
+ """
231
+
232
+ return " Hi" + str (req)
233
+ ```
234
+
235
+ ## Example of returning a custom HTTP code
236
+
237
+ ``` python
238
+ def handle (req ):
239
+ return " request accepted" , 201
240
+ ```
241
+
242
+ ## Example of returning a custom HTTP code and content-type
243
+
244
+ ``` python
245
+ def handle (req ):
246
+ return " request accepted" , 201 , {" Content-Type" :" binary/octet-stream" }
247
+ ```
248
+
249
+ ## Example of accepting raw bytes in the request
250
+
251
+ Update stack.yml:
252
+
253
+ ``` yaml
254
+ environment :
255
+ RAW_BODY : True
256
+ ` ` `
257
+
258
+ > Note: the value for ` RAW_BODY` is case-sensitive.
259
+
260
+ ` ` ` python
261
+ def handle(req):
262
+ """handle a request to the function
263
+ Args:
264
+ req (str): request body
265
+ """
266
+
267
+ # req is bytes, so an input of "hello" returns i.e. b'hello'
268
+ return str(req)
269
+ ` ` `
270
+
246
271
247
272
# # Testing
248
273
The `python3` templates will run `pytest` using `tox` during the `faas-cli build`. There are several options for controlling this.
0 commit comments