Skip to content

Commit cb2657b

Browse files
committed
Clarify use and purpose of templates in this repo
Clarifies expectations for usage of the templates in this repo along with an update to the python3-flask template so that it provides a better upgrade path. Examples added for python3-flask to show how to return a HTTP error or status code. Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent e815593 commit cb2657b

File tree

2 files changed

+103
-13
lines changed

2 files changed

+103
-13
lines changed

README.md

Lines changed: 95 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,135 @@ OpenFaaS Python Flask Templates
44
The Python Flask templates that make use of the incubator project [of-watchdog](https://github.com/openfaas-incubator/of-watchdog).
55

66
Templates available in this repository:
7+
78
- python27-flask
89
- python3-flask
910
- python3-flask-debian
1011
- python3-flask-armhf
12+
1113
- python3-http
1214
- python3-http-debian
1315
- python3-http-armhf
1416

1517
Notes:
1618
- To build and deploy a function for Raspberry Pi or ARMv7 in general, use the language templates ending in *-armhf*
1719

20+
## Picking your template
21+
22+
The templates named `python*-flask*` are designed as a drop-in replacement for the classic `python3` template, but using the more efficient of-watchdog. The move to use flask as an underlying framework allows for greater control over the HTTP request and response.
23+
24+
Those templates named `python*-http*` are designed to offer full control over the HTTP request and response. Flask is used as an underlying framework.
25+
26+
The `witness` HTTP server is used along with Flask for all templates.
27+
28+
Are you referencing pip modules which require a native build toolchain? It's advisable to use the template with a `-debian` suffix in this case. The Debian images are larger, however they are usually more efficient for use with modules like `numpy` and `pandas`.
29+
1830
## Downloading the templates
31+
32+
Using template pull:
33+
34+
```bash
35+
faas template pull https://github.com/openfaas-incubator/python-flask-template
1936
```
20-
$ faas template pull https://github.com/openfaas-incubator/python-flask-template
37+
38+
Using template store:
39+
40+
```bash
41+
faas template store pull python3-flask
2142
```
2243

23-
# Using the python27-flask/python3-flask templates
44+
# Using the python3-flask template
45+
2446
Create a new function
47+
2548
```
26-
$ faas new --lang python27-flask <fn-name>
49+
export OPENFAAS_PREFIX=alexellis2
50+
export FN="tester"
51+
faas new --lang python3-flask $FN
2752
```
53+
2854
Build, push, and deploy
55+
2956
```
30-
$ faas up -f <fn-name>.yml
57+
faas up -f $FN.yml
3158
```
59+
3260
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+
"""handle a request to the function
83+
Args:
84+
req (str): request body
85+
"""
86+
87+
return 201, "request accepted"
88+
```
89+
90+
## Example of accepting raw bytes in the request
91+
92+
Update stack.yml:
93+
94+
```yaml
95+
environment:
96+
RAW_BODY: True
3397
```
34-
$ echo -n content | faas invoke <fn-name>
98+
99+
> Note: the value for `RAW_BODY` is case-sensitive.
100+
101+
```python
102+
def handle(req):
103+
"""handle a request to the function
104+
Args:
105+
req (str): request body
106+
"""
107+
108+
# req is bytes, so an input of "hello" returns i.e. b'hello'
109+
return string(req)
35110
```
36111

37112
# Using the python3-http templates
113+
38114
Create a new function
115+
39116
```
40-
$ faas new --lang python3-http <fn-name>
117+
export OPENFAAS_PREFIX=alexellis2
118+
export FN="tester"
119+
faas new --lang python3-http $FN
41120
```
121+
42122
Build, push, and deploy
123+
43124
```
44-
$ faas up -f <fn-name>.yml
45-
```
46-
Set your OpenFaaS gateway URL. For example:
47-
```
48-
$ OPENFAAS_URL=http://127.0.0.1:8080
125+
faas up -f $FN.yml
49126
```
127+
50128
Test the new function
129+
51130
```
52-
$ curl -i $OPENFAAS_URL/function/<fn-name>
131+
echo -n content | faas invoke $FN
53132
```
54133
55134
## Event and Context Data
135+
56136
The function handler is passed two arguments, *event* and *context*.
57137
58138
*event* contains data about the request, including:
@@ -66,12 +146,15 @@ The function handler is passed two arguments, *event* and *context*.
66146
- hostname
67147
68148
## Response Bodies
149+
69150
By default, the template will automatically attempt to set the correct Content-Type header for you based on the type of response.
70151
71152
For example, returning a dict object type will automatically attach the header `Content-Type: application/json` and returning a string type will automatically attach the `Content-Type: text/html, charset=utf-8` for you.
72153
73154
## Example usage
155+
74156
### Custom status codes and response bodies
157+
75158
Successful response status code and JSON response body
76159
```python
77160
def handle(event, context):

template/python3-flask/index.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from flask import Flask, request
55
from function import handler
66
from waitress import serve
7+
import os
78

89
app = Flask(__name__)
910

@@ -22,7 +23,13 @@ def fix_transfer_encoding():
2223
@app.route("/", defaults={"path": ""}, methods=["POST", "GET"])
2324
@app.route("/<path:path>", methods=["POST", "GET"])
2425
def main_route(path):
25-
ret = handler.handle(request.get_data())
26+
raw_body = os.getenv("RAW_BODY")
27+
28+
as_text = True
29+
if raw_body == "True":
30+
as_text = False
31+
32+
ret = handler.handle(request.get_data(as_text=as_text))
2633
return ret
2734

2835
if __name__ == '__main__':

0 commit comments

Comments
 (0)