I am fairly new to python scripting. I have just figured out how to read and write input parameters for a python script from a csv file. Now I am being asked to pre-populate the script tool dialog in toolbox with values from the csv, so that the user can see right away what the parameters are in the csv and does not have to browse to them. My hunch is that this might work with the tool validator class, but I don't even know how to start with that. Has anybody tried something similar before? Any help would be appreciated. Thanks!
Update: I was able to solve the problem. Please see my code below.(Note: that there is also a CVSHelper class, that is not shown in this code)
source_csv_file_path = str("C:\\Tool_Sources.csv")
source_csv = CSVHelper(source_csv_file_path)
source_csv_data = source_csv.read_csv_dict()
class ToolValidator(object):
"""Class for validating a tool's parameter values and controlling
the behavior of the tool's dialog."""
def __init__(self):
"""Setup arcpy and the list of tool parameters."""
self.params = arcpy.GetParameterInfo()
#Group the following parameters in the dialog box
self.params[0].category = "Defaults"
self.params[1].category = "Defaults"
self.params[2].category = "Defaults"
self.params[3].category = "Defaults"
self.params[4].category = "Defaults"
def initializeParameters(self):
"""Refine the properties of a tool's parameters. This method is
called when the tool is opened."""
if not os.path.exists(str(source_csv)):
self.params[0].value = str(source_csv_data[0]["Workspace"])
self.params[1].value = str(source_csv_data[0]["feature class1"])
self.params[2].value = str(source_csv_data[0]["feature class2"])
self.params[3].value = str(source_csv_data[0]["feature class3"])
self.params[4].value = str(source_csv_data[0]["SQL"])
return
def updateParameters(self):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return