Skip to main content

Convert Jupyter notebook to nuclio

Project description

nuclio Jupyter Export

Build Status License

Convert Jupyter notebook to Python code that can run as nuclio handler

Installing

pip install nuclio-jupyter

Install in Notebook

Run the following in a cell

# nuclio: ignore
!pip install nuclio-jupyter

Usage

When developing, import Event from nucilo and use it to generate a mock request events.

import nuclio

This will inject a context object into the notebook namespace.

Then

# nuclio: ignore
from nuclio import Event

event = Event(body='Hello Nuclio')
# your code goes here

In the cell you'd like to become the handler, added the magic %%nuclio handler If there's a specific line you'd like to be the returned one - added # nuclio:return at the end of it.

Cells containing # nuclio: ignore comment will be omitted in the export process.

Now choose File/Download as/Nuclio in Jupyter notebook (you might need to mark the notebook as Trusted in order for the Nuclio option to show)

Or you can run

jupyter nbconvert --to nuclio example.ipynb

This will create example.zip with your code wrapped in handler function and function.yaml nuclio configuration file.

Example

Will generate a zip file with

handler.py

# Generated by nuclio.export.NuclioExporter on 2018-12-10 08:26:39.086907

import nuclio

def handler(context, event):
   msg = 'Hello ' + event.body
   return msg # nuclio: return

and function.yaml

# Generated by nuclio.export.NuclioExporter on 2018-12-10 08:26:39.095522
apiVersion: nuclio.io/v1
kind: Function
metadata: {}
spec:
 runtime: python:3.6
 handler: handler:handler
 env:
   - name: API_KEY
     value: '1234'
   - name: NUM_JOBS
     value: '2'
   - name: PASSWORD
     value: t0ps3cr3t
   - name: USER
     value: guazio
build:
 commands:
 - apt-get install -y libyaml-dev

Other Notebooks

We currently don't support Jupyter Lab, Google Colaboratory, Kaggle Notebooks and others.

You can use nuclio.print_handler_code or nuclio.save_handler_code to print/save the handler code and then copy&paste it to the nuclio dashboard.

Try It Out

You can build a docker image and try it out

Build

$ docker build -t jupyter-nuclio .

Run

$ docker run -p 8888:8888 jupyter-nuclio

Then open your browser at http://localhost:8888

Developing

We're using pipenv as package manager. To install dependencies run

$ pipenv sync -d

To run the tests run

$ pipenv run python -m pytest -v tests

Licence

Apache 2.0 (see LICENSE.txt)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nuclio-jupyter-0.4.2.tar.gz (31.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page