Python

Python

Napkin Python Module

Every Napkin editor has the napkin module pre-installed, which provides access to incoming request and outgoing response data.

The API for the napkin is based on Flask's global request and response modules: https://flask.palletsprojects.com/en/1.1.x/api/#flask.request

Incoming Request Data

napkin.request

Use the global request object to access incoming request data. Napkin parses incoming requests to your endpoint and stores the data in the request object. The object has the following fields.

args

type: dict

The parsed URL parameters (the part in the URL after the question mark).

body

type: str

cookies

type: dict

Contents of all cookies transmitted with the request.

data

type: str

Contains the incoming request data as string in case it came with a mimetype Werkzeug does not handle.

files

Coming Soon

form

type: dict

The form parameters

full_path

type: str

Requested path, including the query string

headers

type: dict

Dictionary of incoming request headers.

is_json

type: bool

Whether or not request payload is in JSON format. Determined by the request mimetype (either application/json or application/*+json).

method

type: str

The incoming HTTP method. Current supported methods: GET, POST, PUT, DELETE

path

type: str

Requested path

path_params

type: dict

Dictionary of path parameter values based on the path variables defined in "Run Options" of the Napkin UI.

Response Object

napkin.response

body

type: str or dict

A string or JSON object to return as a response payload.

headers

type: dict

A dictionary of headers to return in the response.

status_code

type: int

HTTP status code to return in the response.

Installing Modules

Once you've created an account, the Napkin editor allows you to install any external Python module, including those available in the PyPi repository.

Currently, all installed Python modules are shared across a user's functions. Just install a module once and it will be there ready to use in any of your other functions. Currently, combined module storage space is limited to 250MB.

To install or uninstall a module, and to list all the current modules you've downloaded, go to the Modules tab in the Napkin editor.

Environment Variables

As a signed in user, you can add environment variables to your user functions:

  1. Go to Other tab > Environment Variables
  2. Add key and value

Using Environment Variables

Then, to use environment variables in Python, use the built-in os.environ object. For example, you can print all environment variables in your function:

import os

print(os.environ)

Files

You can upload files to use in your Napkin functions. Go to the Other tab in the Napkin editor and scroll down to Files. You can upload any file as long as its size is less than 1MB.

Once uploaded, your function can access your files via the /opt/files directory. You can access your files using the usual builtin modules for both Javascript and Python. For example, assuming you've uploaded a file called file.txt, here's how you can print the contents of the file:

with open('/opt/files/file.txt', 'r') as f:
  contents = f.read()

print(contents)

Key Value Store

The store module lets allows you to persist any valid JSON data type across your function runs:

  • a string
  • a number
  • an object (JSON object)
  • an array
  • a boolean
  • null
from napkin import store

store.get(key)

Returns: dict

Retrieve the value associated with the key key . The value is return in a dictionary with a single key, data

store.put('myKey', 42)
x = store.get('myKey')
print(x)
# {'data': 42}

store.has(key)

Returns: bool

Return whether or not the key-value store contains a key-value pair with key key

store.put('myKey', 42)
print(store.has('myKey'))
# True

store.delete('myKey')
print(store.has('myKey'))
# False

print(store.get('myKey'))
# {'data': None}

store.put(key, value)

Returns: None

Save the given key-value pair in the Store. If the key-value pair already exists in the store, the value is updated accordingly.

store.put('myKey', 42)
print(store.has('myKey'))
# True

store.delete(key)

Delete the value stored under the key key

store.put('myKey', 42)
print(store.has('myKey'))
# True

store.delete('myKey')
print(store.has('myKey'))
# False

print(store.get('myKey'))
# {'data': None}

store.size()

Returns: int

Return the current size in bytes of the key-value store. Maximum size is 400 KB.

print(store.size())
# 128