Suzanne

Set default values for python toolbox parameters

Discussion created by Suzanne on Feb 13, 2015
Latest reply on Mar 14, 2015 by lpinner

I am sure this is already out there but I could not find it so I thought I would share this code sample for storing and setting python toolbox tool parameter values using python pickles.  I am not a programmer so any feedback for doing this a better way is always appreciated.

 

 

import arcpy, pickle, os, sys

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Pickle_Test"
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [Tool]
   
class Tool(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Tool"
        self.description = ""
        self.canRunInBackground = False
       
    def getParameterInfo(self):
        """Define parameter definitions"""
       
    #set default values for tool parameters
    scriptPath = sys.path[0]
    test_val = pickle.load(open(scriptPath + "\\test_params.txt", "rb"))   
       
    #First parameter
    param0 = arcpy.Parameter(
        displayName="Input Feature Class",
        name="in_fc",
        datatype=["Feature Class"],
        parameterType="Required",
        direction="Input")

    param0.value = test_val['param0']
   
    params = [param0]
    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."""
        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""
   
        scriptPath = sys.path[0]
        test_val = {}
        test_val = {"param0": parameters[0].valueAsText}
        pickle.dump (test_val, open(scriptPath + "\\test_params.txt", "wb"))

        return

Outcomes