OpenWhisk on IBM Bluemix: Post your code. We host it. We scale it up. Pay only for what you use.
OpenWhisk on IBM Bluemix supports Dockerized actions. Jupyter Kernel Gateway can turn annotated Jupyter notebooks into microservices that run in Docker.
Let's put them together and create Jupyter notebook-powered Whisk actions! 🎈
Here's a dirt simple hello world web API. It uses annotations supported by the Jupyter Kernel Gateway to define the API endpoints. Here it is written in Python. It could be written in any language supported by Jupyter.
import json
def tojson(data):
'''Shorten the code to respond a little bit.'''
print(json.dumps(data))
REQUEST = json.dumps({
'body': {},
'headers': {}
})
OpenWhisk requires an init
endpoint.
# POST /init
tojson({})
{}
OpenWhisk will POST
to the run
endpoint on every action invocation.
# POST /run
req = json.loads(REQUEST)
name = req.get('body', {}).get('value', {}).get('name', 'world')
tojson(data = {
'msg': 'Hello {} from a Jupyter Notebook!'.format(name)
})
{"msg": "Hello world from a Jupyter Notebook!"}
Publish this notebook to the web somewhere. For simplicity, I've made this one a GitHub gist: https://gist.github.com/parente/bd0b71f15ba0b97139e5.
Create a Dockerfile:
# Use a barebones image from jupyter/docker-stacks for this
# simple example. Replace with another docker-stacks image
# if the notebook has more complex dependencies.
FROM jupyter/minimal-kernel
# Put the jupyter kernel gateway into microservice mode
ENV KG_API notebook-http
# Point to our gist as the API definition.
ENV KG_SEED_URI https://gist.githubusercontent.com/parente/bd0b71f15ba0b97139e5/raw/nbwhisk.ipynb
# Listen on the port OpenWhisk wants.
ENV KG_PORT 8080
EXPOSE 8080
Build and push the image:
docker build --rm -t parente/nbwhisk
docker push parente/nbwhisk
Create the whisk action:
wsk action create --docker nbwhisk parente/nbwhisk
Invoke it:
wsk action invoke --blocking nbwhisk
Update the notebook and republish it to the web. Then update the action:
wsk action update --docker nbwhisk parente/nbwhisk
There's no need to rebuild / repush the Docker image in this example because the notebook lives on the open web. If it resided directly in the Docker image, a rebuild/repush would be in order.
More intriguing demo coming soon ...? 🙈🙉🙊
Copyright (c) IBM Corp. 2016 under the MIT License