Package: run_jnb
run_jnbrun_jnbis a python package and command line tool for parametrising (python3 only) and executing Jupyter notebooks.Source:https://github.com/hz-inova/run_jnbPlatform: IndependentDevelopment Status: AlphaGetting Started:Installationpipinstallrun_jnbShort DescriptionThe package contains two public functionspossible_parameterandrun_jnb(see the docstring).>>>fromrun_jnbimportpossible_parameter,run_jnbrun_jnbcan be used also as a command line tool and its documentation is available viarun_jnb-hSimple ExampleConsider thenotebook.possible_parameterreturns alistof possible parameters with their name, value and cell index.
The list is alphabetically sorted by the name of the possible parameters.>>>possible_parameter('./Power_function.ipynb')[PossibleParameter(name='exponent',value=2,cell_index=7),PossibleParameter(name='np_arange_args',value={'start':-10,'stop':10,'step':0.01},cell_index=4)]run_jnballows one to easily parametrise and execute a notebook.# Parametrise the noteboook and not execute the notebook>>>run_jnb('./Power_function.ipynb',return_mode='parametrised_only',exponent=1)# Parametrise and execute the notebook>>>run_jnb('./Power_function.ipynb',return_mode=True,exponent=1)Output(output_nb_path='.../_run_jnb/Power_function-output.ipynb',error_prompt_number=None,error_type=None,error_value=None,error_traceback=None)Please see the exported notebook byonly parametrisingand byparametrising and executingthe initial notebook.
Same output can be obtained by usingargparameter>>>run_jnb('.../Power_function.ipynb',return_mode=True,arg='{"exponent":1}')or using the command line tool (the output is returned only in verbose mode and the tuple is serialised as a csv)# " can be escaped by \"$run_jnb./Power_function.ipynb-mtrue-a"{\"exponent\":1}"-vvv".../_run_jnb/Power_function-output.ipynb",,,,np_arange_argsandexponentcan be parametrised# parametrise using keyword arguments>>>run_jnb('./Power_function.ipynb',return_mode=True,exponent=3,np_arange_args={'start':-20,'stop':20,'step':0.1})# parametrise mixing keyword arguments and arg parameter>>>run_jnb('./Power_function.ipynb',return_mode=True,arg='{"exponent":3}',np_arange_args={'start':-20,'stop':20,'step':0.1})# parametrise using arg parameter with a json file>>>run_jnb('./Power_function.ipynb',return_mode=True,arg='./power_function_arg.json')Output(output_nb_path='.../_run_jnb/Power_function-output (1).ipynb',error_prompt_number=None,error_type=None,error_value=None,error_traceback=None)where in the last examplepower_function_arg.jsoncontains{"exponent":3,"np_arange_args":{"start":-20,"stop":20,"step":0.1}}Please see thegenerated notebook.If an error is detected during the execution of the generated notebook>>>run_jnb('./Power_function.ipynb',return_mode=True,exponent=1,np_arange_args={'step':0.1})Output(output_nb_path='.../_run_jnb/Power_function-output (2).ipynb',error_prompt_number=3,error_type='TypeError',error_value="Required argument 'start' (pos 1) not found",error_traceback=...)the output provides also the prompt number of the cell where the error was caught and details about the error (please see thegenerated notebook).How it worksFor a notebook written in python one can find the possible parameters. This is achieved by parsing the abstract syntax tree of the code cells. A variable can be a possible parameter if:it is defined in a cell that contains only comments or assignments,its name is not used as a global variable in the current cell (beside the assignment) nor previously.One can pass arguments as keyword arguments or in a json format (file or string). For safety reasons, in order to avoid any code injection, only json serializable keywords arguments are available. The keyword arguments are firstly encoded in json format using the standardjson encoder. The json content is decoded into python objects using the standardjson decoderand it is mapped to a variable assignment by unpacking it. The assignments are appended at the end of the cell where they are initially defined.For ajsonable parameter, i.e. a parameter for which its value can be recovered from its json representation using the standard decoder, the value of the parameter is returned as well. The value is determined in two steps: firstly the assignment is safely evaluated usingast.literal_evaland next it is checked if it is a jsonable parameter.The generated notebook (parametrised or not) can be easily executed. The implementation relies onnbconvert Executing notebooks.Dependenciespython: 3.5 or highernbconvert: 4.2 or higherLicenseBSD 3Acknowledgmentsnbrunandnbparameterisewere a source of inspiration.