Setting default values in Value Tables (script tools)

674
3
04-13-2022 06:11 PM
Labels (2)
AlfredBaldenweck
MVP Regular Contributor

Pretty much what it says on the tin. 

How can I set default values for Value Tables in my script tool parameters?

Either having it as a default when the tool is opened, or a contingent value (i.e. if you pick on value in Column 1, column 2 will automatically be populated)?

Value Tables are more difficult to work with than I'd hoped.

Thanks!

0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor

Did you see the examples in the help topic?

ValueTable—ArcGIS Pro | Documentation


... sort of retired...
0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Yes, I reviewed the Value Table help page , as well as multiple pages on script tool parameters, including the Parameter object's help page

I was able to get default to show up by pasting them into the normal default field in the script tool parameter page, using the format:

Col1A [space] Col2A; Col1B [space] Col2B

The column values are separated by spaces, and the rows are separated by semicolons. You will have to place any values that have spaces in them ("Maria's Pizzeria") in quotes.

 

However, I would like to set these in the Validation page to make setting them easier to read.

I've tried using self.params[N].values== [[Col1A, Col2A], [Col1B, Col2B]] (read: a list of lists, as hinted at in the parameter help page for value tables) under both initializeParameters(self) and updateParameters(self) with no luck.

Under updateParameters, I was predicating it on self.params[N].altered, and while that worked for other things, it did not work for this one.

What is a clean way to set the defaults of a value table in the validation page of a script tool?

0 Kudos
CSDFRADMIN
New Contributor II

Hey Alfred,

I'm a bit late to the party, but who knows, maybe you can still use this or someone else.

I struggled with the same issue and finally found a way to do it, although it's not as straightforward as simply passing a list of lists.

I read out the parameter value as a text, created a ValueTable object and loaded the table's values from the parameter's string value. Then I applied the changes in the ValueTable using the methods and properties according to ValueTable—ArcGIS Pro | Documentation. Here's my code snippet:

if self.params[0].value and not self.params[0].hasBeenValidated:
    vTbl = arcpy.ValueTable(3) # My parameter has 3 columns
    vTbl.loadFromString(self.params[0].valueAsText)
    for i in range(0, vTbl.rowCount):
        col1val = vTbl.getValue(i, 0)
        vTbl.setValue(i, 1, col2val)
        vTbl.setValue(i, 2, col3val)
    self.params[0].value = vTbl.exportToString()
return

For your specific case, it might make sense to replace col2val and col3val with functions (outside of the class) that uses col1val as an input. I don't know exactly how you want to access the data. If those "contigent values" are acutally stored as contingent values, you should be able to access them via arcpy. What I did for another tool, is to have an excel file saved somewhere and then read the data with pandas.

Hope that helped!