|
1 |
| -# python-flask-template |
| 1 | +OpenFaaS Python Flask Templates |
| 2 | +============================================= |
2 | 3 |
|
3 |
| -Python OpenFaaS template with Flask |
| 4 | +The Python Flask templates make use of the incubator project [of-watchdog](https://github.com/openfaas-incubator/of-watchdog) to handle higher throughput than the [classic watchdog](https://github.com/openfaas/faas/tree/master/watchdog) due to the process being kept warm. |
4 | 5 |
|
5 |
| -To try this out with either Python 2.7 or Python 3.6: |
6 |
| - |
7 |
| -```bash |
8 |
| -faas template pull https://github.com/openfaas-incubator/python-flask-template |
9 |
| -faas new --list |
10 |
| -Languages available as templates: |
| 6 | +Templates available under this repository: |
11 | 7 | - python27-flask
|
12 | 8 | - python3-flask
|
13 |
| -``` |
| 9 | +- python3-flask-armhf |
| 10 | +- python3-http* |
| 11 | +- python3-http-armhf* |
14 | 12 |
|
15 |
| -Generate a function with one of the languages: |
| 13 | +Notes: |
| 14 | +- The templates listed with an asterik are the only ones that support additional control over the HTTP response and provide access to HTTP request details. |
| 15 | +- To build and deploy a function for Raspberry Pi or ARMv7 in general, use the language templates ending in *-armhf* |
16 | 16 |
|
17 |
| -```bash |
18 |
| -faas new --lang python3-flask myfunction |
19 |
| -mv myfunction.yml stack.yml |
| 17 | +## Downloading the templates |
| 18 | +``` |
| 19 | +$ faas template pull https://github.com/openfaas-incubator/python-flask-template |
20 | 20 | ```
|
21 | 21 |
|
22 |
| -Followed by the usual flow: |
| 22 | +# Using the python27-flask/python3-flask templates |
| 23 | +Create a new function |
| 24 | +``` |
| 25 | +$ faas new --lang python27-flask <fn-name> |
| 26 | +``` |
| 27 | +Build, push, and deploy |
| 28 | +``` |
| 29 | +$ faas up -f <fn-name>.yml |
| 30 | +``` |
| 31 | +Test the new function |
| 32 | +``` |
| 33 | +$ echo -n content | faas invoke <fn-name> |
| 34 | +``` |
23 | 35 |
|
| 36 | +# Using the python3-http templates |
| 37 | +Create a new function |
| 38 | +``` |
| 39 | +$ faas new --lang python3-http <fn-name> |
| 40 | +``` |
| 41 | +Build, push, and deploy |
| 42 | +``` |
| 43 | +$ faas up -f <fn-name>.yml |
| 44 | +``` |
| 45 | +Set your OpenFaaS gateway URL. For example: |
24 | 46 | ```
|
25 |
| -faas build \ |
26 |
| - && faas deploy |
27 |
| - && faas list --verbose |
| 47 | +$ OPENFAAS_URL=http://127.0.0.1:8080 |
| 48 | +``` |
| 49 | +Test the new function |
| 50 | +``` |
| 51 | +$ curl -i $OPENFAAS_URL/function/<fn-name> |
| 52 | +``` |
| 53 | + |
| 54 | +## Event and Context Data |
| 55 | +The function handler is passed two arguments, *event* and *context*. |
| 56 | + |
| 57 | +*event* contains data about the request, including: |
| 58 | +- body |
| 59 | +- headers |
| 60 | +- method |
| 61 | +- query |
| 62 | +- path |
| 63 | + |
| 64 | +*context* contains basic information about the function, including: |
| 65 | +- hostname |
| 66 | + |
| 67 | +## Response Bodies |
| 68 | +By default, flask will automatically attempt to set the correct Content-Type header for you based on the type of response. |
28 | 69 |
|
29 |
| -# Wait a couple of seconds then: |
| 70 | +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. |
30 | 71 |
|
31 |
| -echo -n content | faas invoke myfunction |
| 72 | +## Example usage |
| 73 | +### Custom Status Codes and Response Bodies |
| 74 | +Successful response status code and JSON response body |
| 75 | +```python |
| 76 | +def handle(event, context): |
| 77 | + return { |
| 78 | + "statusCode": 200, |
| 79 | + "body": { |
| 80 | + "key": "value" |
| 81 | + } |
| 82 | + } |
| 83 | +``` |
| 84 | +Successful response status code and string response body |
| 85 | +```python |
| 86 | +def handle(event, context): |
| 87 | + return { |
| 88 | + "statusCode": 201, |
| 89 | + "body": "Object successfully created" |
| 90 | + } |
| 91 | +``` |
| 92 | +Failure response status code and JSON error message |
| 93 | +```python |
| 94 | +def handle(event, context): |
| 95 | + return { |
| 96 | + "statusCode": 400, |
| 97 | + "body": { |
| 98 | + "error": "Bad request" |
| 99 | + } |
| 100 | + } |
| 101 | +``` |
| 102 | +### Custom Response Headers |
| 103 | +Setting custom response headers |
| 104 | +```python |
| 105 | +def handle(event, context): |
| 106 | + return { |
| 107 | + "statusCode": 200, |
| 108 | + "body": { |
| 109 | + "key": "value" |
| 110 | + }, |
| 111 | + "headers": { |
| 112 | + "Location": "https://www.example.com/" |
| 113 | + } |
| 114 | + } |
| 115 | +``` |
| 116 | +### Accessing Event Data |
| 117 | +Accessing request body |
| 118 | +```python |
| 119 | +def handle(event, context): |
| 120 | + return { |
| 121 | + "statusCode": 200, |
| 122 | + "body": "You said: " + str(event.body) |
| 123 | + } |
| 124 | +``` |
| 125 | +Accessing request method |
| 126 | +```python |
| 127 | +def handle(event, context): |
| 128 | + if event.method == 'GET': |
| 129 | + return { |
| 130 | + "statusCode": 200, |
| 131 | + "body": "GET request" |
| 132 | + } |
| 133 | + else: |
| 134 | + return { |
| 135 | + "statusCode": 405, |
| 136 | + "body": "Method not allowed" |
| 137 | + } |
| 138 | +``` |
| 139 | +Accessing request query string arguments |
| 140 | +```python |
| 141 | +def handle(event, context): |
| 142 | + return { |
| 143 | + "statusCode": 200, |
| 144 | + "body": { |
| 145 | + "name": event.query['name'] |
| 146 | + } |
| 147 | + } |
32 | 148 | ```
|
| 149 | +Accessing request headers |
| 150 | +```python |
| 151 | +def handle(event, context): |
| 152 | + return { |
| 153 | + "statusCode": 200, |
| 154 | + "body": { |
| 155 | + "content-type-received": event.headers['Content-Type'] |
| 156 | + } |
| 157 | + } |
| 158 | +``` |
0 commit comments