Notebook Context

Notebook Context allows you to change the Losant Notebook’s configuration at run time.

Notebook Context

Notebook Context can be useful for the following reasons:

Providing Context

There are two ways that you may provide the optional context to your notebook’s execution:

Notebook: Execute Node

The Notebook: Execute Node allows a Workflow to pass in payload data as your notebook’s context.

Execute Node Context

The example above uses the MongoDB Node to query a MongoDB database for additional data to provide as context to be used in notebook processing.

Manual Execution

When manually executing a Notebook you will have the option to provide JSON to use as context:

Manual Execution Context

Accessing Context

If context was provided to the notebook execution via one of the above methods, it will be available for use in two ways:

Configuration Template

Context are accessible by using templates in the configuration for Notebook Inputs and Notebook Outputs:

Notebook Config Context

Notebook Input

During Notebook execution, context will be available at INPUT_DIR/context.json.

Given the following context:

{
  "nested":{
    "data":{
      "users":[{
        "email":"peter@losant.com",
        "id":"5d65841f1883530006f8b513"
      },
      {
        "email":"test@losant.com",
        "id":"5d65841f1883530006f8b514"
      }]
    }
  }
}

Particular properties can be accessed like so:

import os
import json
input_dir = os.path.join(os.environ['INPUT_DIR'])

context = json.load(os.path.join(input_dir, "context.json"))
users = context['nested']['data']['users']
userOneEmail = users[0]['email']

More information about accessing generated input files can be found here.