1

I'm trying to derive a Python Toolbox parameter from another parameter. I'd like to have a tool which determines fields from an input table and applies the list as a filter to a second parameter. This is what I've come up with so far:

class Tool(object):
    def __init__(self):
        self.label = "MFF"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        #input excel spreadsheet table
        lkTab = arcpy.Parameter (
            displayName = "Leak Excel Worksheet",
            name = "intab",
            datatype = "DETable",
            parameterType = "Required",
            direction = "Input")

        #input table field
        lkLkTab = arcpy.Parameter (
            displayName = "Leak Excel Worksheet Leak Field",
            name = "lkLkFld",
            datatype = "Field",
            parameterType = "Required",
            direction = "Input")

        return [lkTab, lkLkTab]

    def updateParameters(self, parameters):
        flds = []
        tab = parameters [0].valueAsText
        if tab:
            flds = [f.name for f in arcpy.ListFields (tab)]
        if not parameters [1].valueAsText in flds:
            parameters [1].value = None
        #trying here to apply filter list of fields
        parameters [1].filter.list = flds

My field list parameter doesn't get populated however when I indicate a table in the first parameter:

enter image description here

How do filter a parameter with a list of fields from another parameter?

1 Answer 1

2

My input field parameter needed to be type GPString instead of Field.

lkLkTab = arcpy.Parameter (
        displayName = "Leak Excel Worksheet Leak Field",
        name = "lkLkFld",
        datatype = "GPString", ###changed
        parameterType = "Required",
        direction = "Input")

I've also found that filtering parameters based off an input table can be accomplished with dependencies.

def getParameterInfo(self):
    in_fc = arcpy.Parameter(
        name='in_features',
        displayName='Input Features',
        datatype='GPFeatureLayer',
        direction='Input',
        parameterType='Required')

    vt = arcpy.Parameter(
        name = 'summary_fields',
        displayName = 'Summary field',
        datatype = 'Field',
        direction = 'Input',
        parameterType = 'Required')

    vt.parameterDependencies = [in_fc.name]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.