How to Use DataSet for Dropdown List Component With Scripting

Hey guys,

So in one of my scripts, using Python, I created a variable called engUnits that is a dataset, and it holds the units used (first image).
engUnits1

What I wanted to know or what I was having trouble with was trying to use that data set that I created solely on my script to be used as the options to select in my drop down menu/list component. The second image shows my attempt in trying to insert my dataset I created into my drop down component but I was unsuccessful.

engUnits2

Thank you in advance, I greatly appreciate anyone’s help!!

https://docs.inductiveautomation.com/display/DOC79/system.dataset.toDataSet

If you’re making a dataset from scratch with system.dataset.toDataSet() then it requires two arguments, headers and the data. I am assuming that your engUnits are the headers. So should look more like

unitDataSet = system.dataset.toDataSet(engUnits, [['row1col1valule', 'row1col2value', 'row1col3value'], ['row2col1valule', 'row2col2value', 'row2col3value']])

My dataset is a single row, and when I print out my engUnits, it only displays the info inside. How would I find out the name of the Header?

I also wanted to mention that my dataset update or dynamically updates based on what was used. So it can be more than 3 inside the dataset. And as the dataset changes, I wanted the drop down component to update as well.

Just wanted to point out that this [u'PSIG',u'',u'F'] isn’t a dataset, it is a list.

To create a dataset, you need to make a list of headers and a list of data. For it to work with the dropdown, you need have specific headers as expected by the component. If you look at the data property of a fresh dropdown component you can find what is needed.

You will need something like this:

values = [[0,'PSIG'],[1,''],[2,'F']]
headers = ['Value','Label']

engUnitsDS = system.dataset.toDataSet(headers,values)
dropdown.data = engUnits

Notice that values is a list of list’s. Each item in values must have the same number of items as there are headers.

2 Likes

Would if work if I converted my list as so:

engUnits = [u’PSIG’, u’’, u’F’]

to

engUnits = [[PSIG], , [F] ]

or would I need to add those iteration numbers (0,1,2…)?

The dropdown requires a Value and Label for each item. So you need to provide both, the component expects the value to be an integer.

engUnitsDS = system.dataset.toDataSet(headers, [list(pair) for pair in enumerate(values)]))

You could use enumerate to automatically…enumerate your original list and add the numbers in sequence.

2 Likes

Just to clarify, my engUnits data is not my headers but the values that I wanted to appear in my dropdown. I think I was able to now create it into a dataset with this.
image

The output was: Dataset [3RR ? 2C]

Does this mean I was able to successfully complete it into a data set? Again, thank you all for the help!

engUnits3
Here is a better image.

Yes, that means that engUnitsDS is a dataset with 3 rows and 2 columns.

Thank you guys!! I was able to successfully update my Drop Down dynamically! Im grateful for the help and this community. I really appreciate it @lrose and @PGriffith !!!