Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
share|improve this question
    
Tool validation is the right place to look –  PolyGeo Jun 4 '13 at 22:27
    
Are you writing a classic .tbx or a Python toolbox? In the case of the Python toolbox, you can do everything you need in the updateParameters method. How do you want to display the parameters to the user in the tool? as a drop-down list they select from? A value list? –  scw Jun 4 '13 at 22:47
    
Hi, I would like for the user to see the parameter filled in, no value or drop-down list. It is a script attached to a classic .tbx. –  Kat Jun 4 '13 at 22:53
    
Kat, I do not understand, you have the code already, what are you asking? How to edit the tool validator class? It is discussed at great length here: resources.arcgis.com/en/help/main/10.1/index.html#/… –  Hornbydd Jun 5 '13 at 16:32
    
Thanks for your comments. I figured out how to display paths in the tool dialogue. My code is updated above. –  Kat Jun 5 '13 at 18:32

1 Answer 1

I happened to answer my own question. Please see the modified code above.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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