@@ -86,6 +86,73 @@ faas-cli build --lang python3 --build-option dev --build-arg ADDITIONAL_PACKAGE=
86
86
87
87
The entries in the template's Dockerfile described in 1.0 above need to be present for this mode of operation.
88
88
89
+ ## 2.1 Build options examples
90
+
91
+ Let's see some practical examples with the ` build-option ` flag.
92
+
93
+ * Use [ Pillow] ( https://pillow.readthedocs.io/en/5.2.x/ ) for image processing in your Python function
94
+
95
+ Create function with
96
+
97
+ ```
98
+ $ faas-cli new faas-black-and-white --lang python3 --prefix <your-docker-namespace>
99
+ ```
100
+
101
+ Add ` pillow ` , ` requests ` and ` validators ` to ` requirements.txt ` .
102
+
103
+ Edit ` handler.py ` :
104
+
105
+ ``` python
106
+ import os, io, requests, validators
107
+ from PIL import Image
108
+
109
+ def handle (req ):
110
+ defaultUrl = " https://pbs.twimg.com/media/DsXDzALW0AAkz2I.jpg:large"
111
+ if len (req) > 0 and validators.url(req):
112
+ url = req
113
+ else :
114
+ url = defaultUrl
115
+
116
+ errMsg = " Failed to read given URL"
117
+ try :
118
+ img = Image.open(requests.get(url, stream = True ).raw)
119
+ except requests.exceptions.SSLError:
120
+ return errMsg
121
+ except urllib3.exceptions.MaxRetryError:
122
+ return errMsg
123
+
124
+ blackAndWhite = img.convert(' 1' )
125
+
126
+ byteArr = io.BytesIO()
127
+ blackAndWhite.save(byteArr, format = ' JPEG' )
128
+ res = byteArr.getvalue()
129
+ os.write(1 , res)
130
+
131
+ return res
132
+ ```
133
+
134
+ What the code does is to open an image from url and convert it to black and white.
135
+
136
+ You can build the function with build options (` --build-option dev --build-option pillow ` ) or add them to the ` faas-black-and-white.yml ` :
137
+ ```
138
+ build_options:
139
+ - dev
140
+ - pillow
141
+ ```
142
+
143
+ Build push and deploy with:
144
+ ```
145
+ faas up -f faas-black-and-white.yml
146
+ ```
147
+
148
+ Test the function with:
149
+
150
+ ``` bash
151
+ echo " " | faas invoke faas-black-and-white > blackAndWhite.jpg
152
+ ```
153
+
154
+ Or in your web browser by opening http://127.0.0.1:8080/function/pillow-func
155
+
89
156
## 3.0 Pass custom build arguments
90
157
91
158
You can pass ` ARG ` values to Docker via the CLI.
0 commit comments