Python Toolbox Value Table with list

17233
18
12-05-2014 03:13 PM
ChristinaKellum
New Contributor III

I am trying to create a value table parameter in my python toolbox where the user selects a feature class (first column) and then the second column displays a list of the fields available to choose from based on that feature class.  It sounds like that was going to be available in 10.2 but the documentation doesn't mention it.

Tags (1)
18 Replies
curtvprice
MVP Esteemed Contributor

Well, I mentioned two out of the three in my post Alexander.. I have read the help. Thanks for adding the link to the thread for those who haven't, though.

0 Kudos
DuncanHornby
MVP Notable Contributor

Alexander,

I have been trying to find out how to create a drop down in a value table control, I had posted what I had achieved so far on GIS SE here then discovered this thread on this forum. You say this is not functionality available yet. I guess 10.3 does not have it too?

0 Kudos
RichardFairhurst
MVP Honored Contributor

ArcGIS 10.3 does have it.  The syntax that works in a 10.3 python toolbox is as follows:

    def getParameterInfo(self):
        """Define parameter definitions"""

        # First parameter
        param0 = arcpy.Parameter(
            displayName="Input Primary Table",
            name="in_prim_table",
            datatype="DETable",
            parameterType="Required",
            direction="Input")
#...
        # Third parameter
        param2 = arcpy.Parameter(
            displayName="Primary Case Field",
            name="case_fields",
            datatype="GPValueTable",
            parameterType="Required",
            direction="Input")


        param2.columns = [['GPString', 'Primary Case Fields'], ['GPString', 'Secondary Case Fields']]
        param2.filters[0].type="ValueList"
        param2.filters[0].list = ["X"] # dummy list value to be replaced when user updates other parameters.
        param2.filters[1].type="ValueList"
        param2.filters[1].list=["x"] # dummy list value to be replaced when user updates other parameters.
#...

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""

        if parameters[0].value and parameters[0].altered:
            # Return primary table  
            tbl = parameters[0].value  
            desc = arcpy.Describe(tbl)  
            fields = desc.fields  
            l=[]  
            for f in fields:  
                if f.type in ["String", "Text", "Short", "Long", "Float", "Single", "Double", "Integer","OID", "GUID"]:  
                    l.append(f.name)  
            parameters[2].filters[0].list = l  # first column list is updated to show field names associated with parameter 0.
        if parameters[1].value and parameters[1].altered:  
            # Return secondary table  
            tbl = parameters[1].value  
            desc = arcpy.Describe(tbl)  
            fields = desc.fields  
            l=[]  
            for f in fields:  
                if f.type in ["String", "Text", "Short", "Long", "Float", "Single", "Double", "Integer","OID", "GUID"]:  
                    l.append(f.name)  
            parameters[2].filters[1].list = l  # second column list is updated to show field name associate with parameter1
        return
#...
0 Kudos
RobVaughan
New Contributor

Looking for some help.... I have used the code above to add the full file path to a file along with an attribute name to a value table (2 columns). I have been running into issues with the code example above when the feature class contains field aliases. I saw the commented out code section for developing a field list with field name aliases and implemented that. The code accepts the feature class, recognizes the appropriate field names to choose from (including the alias named field), but throws a runtime error (99999999) when it attempts to add the row to the value table. Hard coding the file and field works. Without the ability to debug python toolboxes, I am not sure what is happening..Any insights? 

def updateParameters(self, parameters):
 """Modify the values and properties of parameters before internal
 validation is performed. This method is called whenever a parameter
 has been changed."""
# update zone features\rasters and their field names dynamically in window
 if not parameters[0].hasBeenValidated and parameters[0].value:
 listOfFields = []
 fields = arcpy.Describe(parameters[0].value).fields
 for field in fields:
 listOfFields.append(field.aliasName)
parameters[1].filter.list = listOfFields
 #parameters[1].filter.list = [f.name for f in arcpy.Describe(parameters[0].value).fields]
 #parameters[1].filter.list = [f.name for f in arcpy.ListFields(parameters[0].value)]
if not parameters[1].hasBeenValidated and parameters[1].value:
global vtab
 #vtab = arcpy.ValueTable(len(parameters[2].columns))
 vtab = arcpy.ValueTable(2)
 if (parameters[2].valueAsText not in ('', ' ', None)):
 vtab.loadFromString(parameters[2].valueAsText)
 else:
 pass
vtab.addRow('{0} {1}'.format(str(parameters[0].value), parameters[1].value))‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

0 Kudos
RobVaughan
New Contributor

Sorry for the crappy code sample! 

def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""

        # update zone features\rasters and their field names dynamically in window
        if not parameters[0].hasBeenValidated and parameters[0].value:
            listOfFields = []
            fields = arcpy.Describe(parameters[0].value).fields
            for field in fields:
                listOfFields.append(field.aliasName)

            parameters[1].filter.list = listOfFields
            #parameters[1].filter.list = [f.name for f in arcpy.Describe(parameters[0].value).fields]
            #parameters[1].filter.list = [f.name for f in arcpy.ListFields(parameters[0].value)]

        if not parameters[1].hasBeenValidated and parameters[1].value:

            global vtab
            #vtab = arcpy.ValueTable(len(parameters[2].columns))
            vtab = arcpy.ValueTable(2)
            if (parameters[2].valueAsText not in ('', ' ', None)):
                vtab.loadFromString(parameters[2].valueAsText)
            else:
                pass

            vtab.addRow('{0} {1}'.format(str(parameters[0].value), parameters[1].value))
0 Kudos
RichardFairhurst
MVP Honored Contributor

Typically when code only works with hard coded values it is because the code either does not interpret the users choice in the same way as the hard coded values, or the user has the freedom to choose options that are not anticipated and handled by the code.  Where would you insert the hard coded values?  Anyway, I have no time for the forums anymore outside of my weekends, so I cannot do anything until then.

0 Kudos
DrewFlater
Esri Regular Contributor

Hi everyone. I have always said better late than never, and thought it was worthwhile to update this thread as there are some familiar faces still active on geonet....

This enhancement has been complete in ArcGIS Pro 2.4 alpha software, for both standard tbx toolboxes as well as PYT toolboxes, so you can expect to see this working when the 2.4 final release is available (or if you sign up and participate in the beta program). 

How it works .... you can define a value table parameter, much like discussed above, where one column is a feature layer, and a second column is a field. If this parameter has NO DEPENDENCY set, it will apply an intrinsic dependency between the field column and the layer/data column, to read and populate the field list from the data selected in the first column. Again this only works if the parameter doesn't have a dependency set on another parameter -- we want this dependency to be applied WITHIN the value table, not between the value table parameter and another tool parameter.

I hope it makes sense. Attached a small video of the script tool properties and the tool dialog to help illustrate.

DuncanHornby
MVP Notable Contributor

I've not had the opportunity to do any scripting in ArcPro, so it is a nice surprise that this interface is now supported. I think a lot of ArcPro developers will like this.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Duncan... other examples, albeit static

/blogs/dan_patterson/2016/05/19/toolbox-creation-in-arcgis-pro