2

I am trying to be able to add a list of years (or return periods) to a python toolbox tool I want to create. In the getParametersInfo function, I am currently setting up a parameter as a "GPString" datatype and want it to be a multiValue list. Here is my current code:

import arcpy
class Toolbox(object):
    def __init__(self):
        self.label = "Toolbox"
        self.alias = ""
        # List of tool classes associated with this toolbox
        self.tools = [TestParameters]
class TestParameters(object):
    def __init__(self):
        self.label = "TestParameters"
        self.description = ""
        self.canRunInBackground = False
    def getParameterInfo(self):
        param0 = arcpy.Parameter(
                    displayName ='Do Nothing Here:',
                    name ='in_features',
                    datatype ="GPFeatureLayer",
                    parameterType ='Required',
                    direction ='Input')
        param1 = arcpy.Parameter(
                    displayName ='Enter Return Periods as Years',
                    name ='tbl_features',
                    datatype = "GPString", #"GPValueTable", #"GPTableView", #"DETable", #
                    parameterType ='Required',
                    direction ='Input',
                    multiValue=True)
        param1.filter.type = "ValueList"
        param1.filter.list = []
        params = [param0,param1]
        return params
    def isLicensed(self):
        return True
    def updateParameters(self, params):
        param1.filter.list = []
        return
    def updateMessages(self, params):
        return
    def execute(self, params, messages):
        arcpy.AddMessage("Testing Tool, nothing here yet")
        arcpy.AddMessage(params[1].valueAsText)
        return  

However, when I pull up the tool in ArcCatalog and open the tool prompt I get an empty box. I expected that, but the "Add Value" button does not allow me to add any values. I would like to add years values such as "2", "5", "10", "100", "500", & etc...

Am I missing something that should go inside the updateParameters function? Or is there a way to enable the "Add Value" button? It would be nice not to have to hard code year values, as others may be using this script later and may have different years they need to input. This is why I left the param1.filter.list = [] as a blank list, as the ESRI documentation for it doesn't cover how to enter values after the tool is opened.

Or is there a different type of parameter that can easily do this? I've tried the values commented out on the datatype line for param1. If any of those would work, which one might help me be able to customize this?

1

I think this looks counterintuitive, I would do just what you've done so far. Unfortunately, the only way around this problem (when you don't want to present your users with existing list of years) is to use the ValueTable with a single column. Looks like a hack first, but it works well.

The tool after some years have been added:

enter image description here

The tool code after modifications. Just removed all the filters settings and added definition of Value Table columns.

import arcpy
class Toolbox(object):
    def __init__(self):
        self.label = "Toolbox"
        self.alias = ""
        # List of tool classes associated with this toolbox
        self.tools = [TestParameters]

class TestParameters(object):
    def __init__(self):
        self.label = "TestParameters"
        self.description = ""
        self.canRunInBackground = False
    def getParameterInfo(self):
        param0 = arcpy.Parameter(
                    displayName ='Do Nothing Here:',
                    name ='in_features',
                    datatype ="GPFeatureLayer",
                    parameterType ='Required',
                    direction ='Input')
        param1 = arcpy.Parameter(
                    displayName ='Enter Return Periods as Years',
                    name ='tbl_features',
                    datatype = "GPValueTable", #"GPValueTable", #"GPTableView", #"DETable", #
                    parameterType ='Required',
                    direction ='Input',
                    multiValue=True)
        param1.columns =([["GPString", "Period as year"]])
        params = [param0,param1]
        return params
    def isLicensed(self):
        return True
    def updateParameters(self, params):
        return
    def updateMessages(self, params):
        return
    def execute(self, params, messages):
        arcpy.AddMessage("Testing Tool, nothing here yet")
        arcpy.AddMessage(params[1].valueAsText)
        return

The output after clicking OK:

Testing Tool, nothing here yet 2016;2017;2018

  • Awesome, Thank you. I was struggling with getting the Value Table to work, mostly because of how the *.columns parameter works for them. This seems to be exactly what I needed! – S Duncan Nov 10 '17 at 22:17
  • Great! Please mark the answer as accepted so the thread would be seen as closed to others. Thanks! – Alex Tereshenkov Nov 11 '17 at 7:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.