4

Can I iterate through the widgets in a databricks notebook? Something like this pseudocode?

# NB - not valid
inputs = {widget.name: widget.value for widget in dbutils.widgets}
Scarabee
  • 5,437
  • 5
  • 29
  • 55

2 Answers2

4

Yes. You can get all of the widgets returned as a JavaMap. Then you can convert that to a Python dictionary.

%python
my_widgets = dbutils.notebook.entry_point.getCurrentBindings()
{key: my_widgets[key] for key in my_widgets}

Note that "entry_point" is not a supported part of the dbutils API. Databricks could change or remove it without notice.

J. Peach
  • 131
  • 1
  • 5
  • 1
    Excellent. I tried to find a more elegant way to convert the JavaMap to a dict but your solution worked out best. I'd tick your answer as correct if I had enough reputation to do so! – Michael Grazebrook Apr 28 '22 at 16:04
0

i'm not 100% sure but i think so but if not then you might have to manually input the widgets in by them self and not grouped