Skip to content

Commit ff1f07f

Browse files
committed
Add build options examples
This adds an example of how to practically use `build-option` with pillow for Python Signed-off-by: Ivana Yovcheva (VMware) <[email protected]>
1 parent 10d5dd6 commit ff1f07f

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

docs/cli/build.md

+67
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,73 @@ faas-cli build --lang python3 --build-option dev --build-arg ADDITIONAL_PACKAGE=
8686

8787
The entries in the template's Dockerfile described in 1.0 above need to be present for this mode of operation.
8888

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+
89156
## 3.0 Pass custom build arguments
90157

91158
You can pass `ARG` values to Docker via the CLI.

0 commit comments

Comments
 (0)