Easily entering arguments from dbutils.notebook.run when using a notebook directly

1.7k views Asked by At

I'm calling a notebook like this:

dbutils.notebook.run(path, timeout, arguments)

where arguments is a dictionary containing many fields for the notebook's widgets.

I want to debug called notebook interactively: copy/pasting the widget parameters takes time and can cause hard-to-spot errors not done perfectly.

It would be nice to just take the arguments dictionary and use it directly. Perhaps copying it, then populating the widgets from the dictionary.

How can I do this, or something like it?

2

There are 2 answers

0
Michael Grazebrook On

If we get some variables like this:

dbutils.widgets.text('myvar', '-1', '')
myvar = dbutils.widgets.get('myvar')

I can override them like this:

config = {'myvar': '42'}

import sys
module = sys.modules[__name__]
for k, v in config.items():
  setattr(module, k, v)

Which means all the overriding happens in a cell I can later delete, leaving no edits in the real code.

0
Deva On

Pass the values as a json like below in the widget let it be "Filters"

{"Type":"Fruit","Item":"Apple"]

To read the json you need to make use of json library

import json
filters = dbutils.widgets.get("Filters")
jsonfilter = json.loads(filters)

Now you can access individual items by

jsonfilter["Item"]