Assign field names by default to a python toolbox value table column

1213
3
Jump to solution
09-06-2017 04:34 PM
JonathanCzerniak
New Contributor II

I'm sure this exists somewhere, but I've been unable to find an answer that works with my code.  I'm trying to take the attribute field names that are assigned as parameter dependencies in my code and have them appear by default in the first column of my value table.  See pics below: 

Any help would be great.  I've also attached the code.  Thanks!

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

        # Text field to request the name of the new layer
        layerTitle = arcpy.Parameter(
            displayName="New Layer Name",
            name="layerTitle",
            datatype="GPString",
            parameterType="Required",
            direction="Input")

        # Setup Value Table
        valueTable = arcpy.Parameter(
            displayName="Value Table",
            name="valueTable",
            datatype="GPValueTable",
            parameterType="Optional",
            direction="Input")

        valueTable.columns = [['field', 'Column Name'], ['GPBoolean', 'Hide Field?'], ['GPString', 'New Alias']]
        valueTable.parameterDependencies = [in_features.name]
        valueTable.filters[1].type = 'ValueList'
        valueTable.filters[1].list = ['True', 'False']

        # Add output location for layer file
        outputParam = arcpy.Parameter(
            displayName="Output Location",
            name="outLocation",
            datatype="GPFeatureLayer",
            parameterType="Derived",
            direction="Output")

        outputParam.parameterDependencies = [in_features.name]
        outputParam.schema.clone = True

        params = [in_features, layerTitle, valueTable, outputParam]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""

        return True

    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."""
        in_features = parameters[0].valueAsText

        if parameters[0].value:
            if not parameters[2].altered:
                fields = arcpy.ListFields(in_features)
                vtab = []
                for field in fields:
                    if field.name != "u'OBJECTID" or field.name != "u'GEOMETRY'":
                        vtab.append([field.name])
                parameters[2].values = vtab


        return

Thank you for you time and consideration!

Cz

0 Kudos
1 Solution

Accepted Solutions
JonathanCzerniak
New Contributor II

I figured it out.  Using the ListFields fields function in the def updateParameters function you're able to access the field names of the input attribute table.  Then you can pass those values to a 'for loop' and append them to a list, and then pass those list values into your Value Table.  

Another thing to keep in mind is how you've set up your Value Table columns.  I set mine up requiring a field, a boolean 'true' or 'false', and a string.  This required me to structure my list values as <field name, 'false', None>

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

        # Text field to request the name of the new layer
        layerTitle = arcpy.Parameter(
            displayName="New Layer Name",
            name="layerTitle",
            datatype="GPString",
            parameterType="Required",
            direction="Input")

        # Setup Value Table
        valueTable = arcpy.Parameter(
            displayName="Value Table",
            name="valueTable",
            datatype="GPValueTable",
            parameterType="Optional",
            direction="Input")

        valueTable.columns = [['field', 'Column Name'], ['GPBoolean', 'Hide Field?'], ['GPString', 'New Alias']]
        valueTable.parameterDependencies = [in_features.name]
        valueTable.filters[1].type = 'ValueList'
        valueTable.filters[1].list = ['True', 'False']

        # Add output location for layer file
        outputParam = arcpy.Parameter(
            displayName="Output Location",
            name="outLocation",
            datatype="GPFeatureLayer",
            parameterType="Derived",
            direction="Output")

        outputParam.parameterDependencies = [in_features.name]
        outputParam.schema.clone = True

        params = [in_features, layerTitle, valueTable, outputParam]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""

        return True

    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."""

        in_features = parameters[0].valueAsText
        fields = arcpy.ListFields(in_features)

        if parameters[0].value:
            if not parameters[2].altered:

                vtab = []
                for i in fields:
                    vtab.append([i.name, "false", None])
                parameters[2].values = vtab

        return

View solution in original post

0 Kudos
3 Replies
DanPatterson_Retired
MVP Esteemed Contributor

Is the value table parameter derived?

0 Kudos
JonathanCzerniak
New Contributor II

I don't have a lot of experience with derived outputs, but my gut feeling is that the value table is not.  However, the table does have parameter dependencies associated with the input feature class that populate the list of fields on the fly.

0 Kudos
JonathanCzerniak
New Contributor II

I figured it out.  Using the ListFields fields function in the def updateParameters function you're able to access the field names of the input attribute table.  Then you can pass those values to a 'for loop' and append them to a list, and then pass those list values into your Value Table.  

Another thing to keep in mind is how you've set up your Value Table columns.  I set mine up requiring a field, a boolean 'true' or 'false', and a string.  This required me to structure my list values as <field name, 'false', None>

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

        # Text field to request the name of the new layer
        layerTitle = arcpy.Parameter(
            displayName="New Layer Name",
            name="layerTitle",
            datatype="GPString",
            parameterType="Required",
            direction="Input")

        # Setup Value Table
        valueTable = arcpy.Parameter(
            displayName="Value Table",
            name="valueTable",
            datatype="GPValueTable",
            parameterType="Optional",
            direction="Input")

        valueTable.columns = [['field', 'Column Name'], ['GPBoolean', 'Hide Field?'], ['GPString', 'New Alias']]
        valueTable.parameterDependencies = [in_features.name]
        valueTable.filters[1].type = 'ValueList'
        valueTable.filters[1].list = ['True', 'False']

        # Add output location for layer file
        outputParam = arcpy.Parameter(
            displayName="Output Location",
            name="outLocation",
            datatype="GPFeatureLayer",
            parameterType="Derived",
            direction="Output")

        outputParam.parameterDependencies = [in_features.name]
        outputParam.schema.clone = True

        params = [in_features, layerTitle, valueTable, outputParam]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""

        return True

    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."""

        in_features = parameters[0].valueAsText
        fields = arcpy.ListFields(in_features)

        if parameters[0].value:
            if not parameters[2].altered:

                vtab = []
                for i in fields:
                    vtab.append([i.name, "false", None])
                parameters[2].values = vtab

        return
0 Kudos