Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 16ad0d4

Browse files
committedNov 26, 2018
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) <iyovcheva@vmware.com>
1 parent 34c42cd commit 16ad0d4

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed
 

‎docs/cli/build.md

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,72 @@ Remeber to add any `ARG` values to the template's Dockerfile:
101101
ARG ARGNAME2
102102
```
103103

104-
For more information about passing build arguments to Docker, please visit the [Docker documentation](https://docs.docker.com/engine/reference/commandline/build/)
104+
For more information about passing build arguments to Docker, please visit the [Docker documentation](https://docs.docker.com/engine/reference/commandline/build/)
105+
106+
## 2.1 Build options examples
107+
108+
Let's see some practical examples with the `build-option` flag.
109+
110+
* Use [Pillow](https://pillow.readthedocs.io/en/5.2.x/) for image processing in your Python function
111+
112+
Create function with
113+
114+
```
115+
$ faas-cli new faas-black-and-white --lang python3 --prefix <your-docker-namespace>
116+
```
117+
118+
Add `pillow`, `requests` and `validators` to `requirements.txt` .
119+
120+
Edit `handler.py`:
121+
122+
```python
123+
import os, io, requests, validators
124+
from PIL import Image
125+
126+
def handle(req):
127+
defaultUrl = "https://pbs.twimg.com/media/DsXDzALW0AAkz2I.jpg:large"
128+
if len(req) > 0 and validators.url(req):
129+
url = req
130+
else:
131+
url = defaultUrl
132+
133+
errMsg = "Failed to read given URL"
134+
try:
135+
img = Image.open(requests.get(url, stream=True).raw)
136+
except requests.exceptions.SSLError:
137+
return errMsg
138+
except urllib3.exceptions.MaxRetryError:
139+
return errMsg
140+
141+
blackAndWhite = img.convert('1')
142+
143+
byteArr = io.BytesIO()
144+
blackAndWhite.save(byteArr, format='JPEG')
145+
res = byteArr.getvalue()
146+
os.write(1, res)
147+
148+
return res
149+
```
150+
151+
What the code does is to open an image from url and convert it to black and white.
152+
153+
You can build the function with build options (`--build-option dev --build-option pillow`) or add them to the `faas-black-and-white.yml`:
154+
```
155+
build_options:
156+
- dev
157+
- pillow
158+
```
159+
160+
Build push and deploy with:
161+
```
162+
faas up -f faas-black-and-white.yml
163+
```
164+
165+
Test the function with:
166+
167+
```bash
168+
echo "" | faas invoke faas-black-and-white > blackAndWhite.jpg
169+
```
170+
171+
Or in your web browser by opening http://127.0.0.1:8080/function/pillow-func
172+

0 commit comments

Comments
 (0)
Please sign in to comment.