Defining a tool in a Python toolbox
Tools are added to a .pyt as classes. Each tool class should include at a minimum an __init__ and execute method. Optionally, getParameterInfo, isLicensed, updateParameters, and updateMessages methods can be used to add additional control to the behavior of the tool.
Tool method | Required/Optional | Description |
---|---|---|
__init__ | Required | Initializes the tool class. |
Optional | Defines the tool's parameters | |
Optional | Returns whether the tool is licensed to execute. | |
Optional | Called each time the user changes a parameter on the tool dialog box. After returning from updateParameters, geoprocessing calls its internal validation routine. | |
Optional | Called after returning from the internal validation routine. You can examine the messages created from internal validation and change them if desired. | |
Required | The tool's source code. |
Setting a tool class' __init__
The __init__ method in a tool class is a standard Python class initialization method. For a tool in a Python toolbox, the __init__ method is used to set properties of the tool, including the tool's label and description. The tool's name is established by the name of the class itself (in the example, below, the tool name is CalculateSinuosity).
The __init__ method establishes tool properties such as the label and description. Below, a tool named CalculateSinuosity is created.
class CalculateSinuosity(object):
def __init__(self):
self.label = "Calculate Sinuosity"
self.description = "Sinuosity measures the amount that a river meanders within its valley, " + \
"calculated by dividing total stream length by valley length."
The following properties can be set in a tool's __init__ method.
Property | Description |
---|---|
canRunInBackground | If canRunInBackground is unset or set to True, the tool will respect the current Background Processing setting in the Geoprocessing Options dialog. If set to False, the tool will always run in the foreground, overriding the Background Processing setting in the Geoprocessing Options dialog. |
category | The name of the toolset in which the tool is located. A toolset is a way to organize tools within a toolbox. |
description | The description for the tool. |
label | The label is the display name for the tool as shown in the Catalog window. |
stylesheet | To change the default stylesheet used for the tool. If unset, the default stylesheet is used. |