Setting parameters via JSON

For all the examples in this notebook to work, launch the notebook server using:

PARAMNB_INIT='{"p1":5}' \
 TARGETED='{"Target1":{"p1":3}, "Target2":{"s":"test"}}' \
 CUSTOM='{"custom":{"val":99}}' jupyter notebook

In [ ]:
import os
import param
import paramnb

Example 1

The first minimal example will work if the notebook server is launched as follows:

PARAMNB_INIT='{"p1":5}' jupyter notebook

First let's show that the 'PARAMNB_INIT' environment is defined:


In [ ]:
os.environ['PARAMNB_INIT']

This string is JSON and the 'PARAMNB_INIT' is the default environment variable name to set parameters via the commandline. Lets make a simple parameterized class with a p1 parameter:


In [ ]:
class Test(param.Parameterized):
    
    p1 = param.Number(default=1, bounds=(0,10))

Now if we supply paramnb.JSONInit as an initializer, the p1 parameter is set from the default of 1 to the value of 5 specified by the environment variable:


In [ ]:
paramnb.Widgets(Test, initializer=paramnb.JSONInit())

Example 2

The second example will work if the notebook server is launched as follows:

TARGETED='{"Target1":{"p1":3}, "Target2":{"s":"test"}}' jupyter notebook

In this example, we show how you can target parameters to different classes using a different environment variable called TARGETED:


In [ ]:
os.environ['TARGETED']

Here the keys are class names and the corresponding dictionary values are the parameter values to override. Let's defined classes Target1 and Target2 with parameters p1 and s respectively:


In [ ]:
class Target1(param.Parameterized):
    
    p1 = param.Number(default=1, bounds=(0,10))

class Target2(param.Parameterized):
    
    s = param.String(default="default")

Now lets use paramnb.Widgets on Target1:


In [ ]:
paramnb.Widgets(Target1, initializer=paramnb.JSONInit(varname='TARGETED'))

The value of p1 is now 3 as requested.

Now lets use paramnb.Widgets on Target2:


In [ ]:
paramnb.Widgets(Target2, initializer=paramnb.JSONInit(varname='TARGETED'))

Example 3

The third example will work if the notebook server is launched as follows:

CUSTOM='{"custom":{"val":99}}' jupyter notebook

In this example, we show how you can target a specific instance using an environment variable called CUSTOM:


In [ ]:
os.environ['CUSTOM']

In [ ]:
class Example(param.Parameterized):
    
    val = param.Number(default=1, bounds=(0,100))
    
instance = Example()
paramnb.Widgets(instance, initializer=paramnb.JSONInit(varname='CUSTOM', target='custom'))

Example 4

You can also use a JSON file ending with the '.json' extension. For instance, if you execute:


In [ ]:
import json
json.dump({"p1":5}, open('param_init.json', 'w'))

You cam specify the full path or relative path to the JSON file with:

PARAMNB_INIT=param_init.json jupyter notebook

In [ ]:
os.environ['PARAMNB_INIT']

In [ ]:
class Test(param.Parameterized):
    
    p1 = param.Number(default=1, bounds=(0,10))
    
paramnb.Widgets(Test, initializer=paramnb.JSONInit())

Note that you can use JSONInit without setting any environment variables by specifying the JSON file directly:


In [ ]:
paramnb.Widgets(Test, initializer=paramnb.JSONInit(json_file='param_init.json'))

Tips

  • It is recommended that you target at the class or instance level if you ever intend to use JSONInit to customize different sets of parameters.

  • It is recommended that you validate (and pretty print) the JSON at the commandline using json.tool. For instance, you can validate the JSON for the first example before launching the server as follows:

PARAMNB_INIT=`echo '{"p1":5}' | python -mjson.tool` jupyter notebook