Serve any Jupyter notebook as a REST API using Docker using the Kernel-Gateway.
First build the Docker image:
docker build -t jupyter .
Then start the Jupyter server with the local notebooks
directory mapped:
docker run -p 8888:8888 -v $PWD/notebooks:/app/notebooks jupyter jupyter notebook --allow-root --ip=0.0.0.0 --notebook-dir=/app/notebooks
You can then directly develop new notebooks from the Jupyter application by opening the link starting with http://127.0.0.1 in your browser. It's possible to expose each notebook cell as a REST endpoint, see Kernel-Gateway documentation.
Note: If you need specific libraries from PyPi, you can add them to the
requirements.txt
file and rebuild the image. E.g. for addingpandas
, addpandas==1.3.4
to therequirements.txt
file.
After finishing development, you have to rebuild the image, to ensure that the latest changes of the notebook files are included in the image. You can then run the image to serve the notebook as a REST server
Run the following command to run the Jupyter Docker image as a REST server:
docker run -p 8888:8888 -e KG_SEED_URI=notebooks/test.ipynb jupyter
The KG_SEED_URI
environment variable is used to specify the notebook to be served.
After starting up, you can test the API by using curl, e.g.:
curl http://127.0.0.1:8888/test/123