โš™๏ธ API

Run a notebook by calling an URL.

Add

Send in production this notebook and get URL to run it when opened.

naas.api.add()
screenshot-api-add

Other file

You can also give a path to the function and that will deploy this one instead of the current one.

naas.api.add(path="path/to/my/super/notebook.ipynb")

Parameters

notif_down : Receive an email when the notebook run fails.

notif_up : Receive an email when the notebook runs well.

next_url : Redirect users to another URL after the notebook as loaded.

inline : Get a response in your web browser instead of downloading the result.

params = {"notif_down": "[email protected]", "notif_up": "[email protected]"}
โ€‹
naas.api.add(params=params)

Dynamic Parameters

When you notebook url is called in get or post, it accept parameters with mimetype :

application/x-www-form-urlencoded

application/json

They will be inject in new cell in first position in your notebook.

If You want them to be added at specific place add tag parameters in a cell and your dynamic parameters will be add in a new cell next to it.

In this screenshot you can see variables from our notebook in first cell and in the second cell the dynamic parameters injected .

If your dynamic parameters doesn't follow python naming convention, they will be convert to exemple Full Name become Full_Name.

Debug

Need to understand why somethings go bad?

naas.api.add(debug=True)

Response

Run a Notebook by API is not enough, you can return a result :

Html

Send back any HTML payload.

Don't be a fool and put Facebook code source here, the idea is to stay simple!

naas.api.respond_html("<h1>Check my super html !</h1>")

To confirm that is well loaded you should see it in the cell output.

JSON

naas.api.respond_json({"foo": "bar"})

Image

# exemple of getting a payload from google
import urllib, cStringIO
from PIL import Image
url='https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png'
file = cStringIO.StringIO(urllib.urlopen(url).read())
img = Image.open(file) # image as byte here
filename = "googlelogo.png"
โ€‹
naas.api.respond_image(img, filename)

SVG

# Exemple Svg
jupyterlogo = '<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Jupyter icon</title><path d="M7.157 22.201A1.784 1.799 0 0 1 5.374 24a1.784 1.799 0 0 1-1.784-1.799 1.784 1.799 0 0 1 1.784-1.799 1.784 1.799 0 0 1 1.783 1.799zM20.582 1.427a1.415 1.427 0 0 1-1.415 1.428 1.415 1.427 0 0 1-1.416-1.428A1.415 1.427 0 0 1 19.167 0a1.415 1.427 0 0 1 1.415 1.427zM4.992 3.336A1.047 1.056 0 0 1 3.946 4.39a1.047 1.056 0 0 1-1.047-1.055A1.047 1.056 0 0 1 3.946 2.28a1.047 1.056 0 0 1 1.046 1.056zm7.336 1.517c3.769 0 7.06 1.38 8.768 3.424a9.363 9.363 0 0 0-3.393-4.547 9.238 9.238 0 0 0-5.377-1.728A9.238 9.238 0 0 0 6.95 3.73a9.363 9.363 0 0 0-3.394 4.547c1.713-2.04 5.004-3.424 8.772-3.424zm.001 13.295c-3.768 0-7.06-1.381-8.768-3.425a9.363 9.363 0 0 0 3.394 4.547A9.238 9.238 0 0 0 12.33 21a9.238 9.238 0 0 0 5.377-1.729 9.363 9.363 0 0 0 3.393-4.547c-1.712 2.044-5.003 3.425-8.772 3.425Z"/></svg>'
โ€‹
naas.api.respond_svg(jupyterlogo)

Markdown

# Exemple Svg
markdown_text = "# Hello Naas friends<br /> Do you love markdown as us ?"
โ€‹
naas.api.respond_markdown(markdown_text)

CSV

Again, this is not intended to host a 6Go CSV file, it will break your notebook and you will lose access to it, use the "File" option instead.

# Exemple Svg
csv_text = ```
Sally Whittaker,2018,McCarren House,312,3.75
Belinda Jameson,2017,Cushing House,148,3.52
Jeff Smith,2018,Prescott House,17-D,3.20
Sandy Allen,2019,Oliver House,108,3.48
```
โ€‹
naas.api.respond_csv(csv_text)

File

Send big file not embed in the notebook :

naas.api.respond_file("test.csv")

If your file is not generated by a notebook, add it as Dependency, otherwise, it will be missing in the production folder.

Notebook

Respond the notebook output with:

Use naas.secret for secret who shouldn't be in clear, otherwise anyone with the link will see it !

naas.api.respond_notebook()

List

You can list all version of a file pushed into the production:

Current file

naas.api.list()

Other file

naas.api.list(path="path/to/my/super/notebook.ipynb")

Get

You can get a version of a file pushed into the production:

Get the last one

naas.api.get()

With a file path

naas.api.get(path="path/to/my/super/notebook.ipynb")

With history id

naas.api.get(histo="20201008101221879662")

Combined

naas.api.get(path="path/to/my/super/notebook.ipynb", histo="20201008101221879662")

Clear

You can clear the previous version of a file pushed into the production:

One

naas.api.clear(histo="20201008101221879662")

Other Notebook

naas.api.clear(path="path/to/my/super/notebook.ipynb", histo="20201008101221879662")

All

naas.api.clear()

All for filepath

naas.api.clear(path="path/to/my/super/notebook.ipynb")

Get output

You can get the output of the production file:

Get the last one

naas.api.get_output()

With a file path

naas.api.get_output(path="path/to/my/super/notebook.ipynb")

Clear output

You can clear the previous output of a file pushed into the production:

One

naas.api.clear_output()

Other Notebook

naas.api.clear_output(path="path/to/my/super/notebook.ipynb")

Delete

You can remove any scheduler capability like that, it takes optionally a path.

Current

naas.api.delete()

Other file

naas.api.delete(path="path/to/my/super/notebook.ipynb")

Debug

naas.api.delete(debug=True)

List APIs

You don't remember how many API notebooks you have?

Simple

naas.api.currents()

Raw result

naas.api.currents(raw=True)

โ€‹