Select to view content in your preferred language

How to add a button in geoprocessing tool?

630
3
10-14-2024 01:36 PM
BittersDaniel
New Contributor

I was wondering if there is a way to add a button to a geoprocessing tool to validate and have a pop-up message before the user runs the tool. 

For example, the Add Join tool has a button like this.  I couldn't find a way to add a button though and am wondering if I am missing something.  Thanks!

BittersDaniel_0-1728938040386.png

 

Tags (2)
0 Kudos
3 Replies
DanPatterson
MVP Esteemed Contributor

you can validate a tool

Customizing script tool behavior—ArcGIS Pro | Documentation

you can get a checkbox but not a button if have optional parameters


... sort of retired...
HaydenWelch
MVP Regular Contributor

Here's a simple example tool that should work using @DanPatterson 's checkbox button idea. I use checkbox buttons this way all the time:

 

import arcpy

class ValidateButtonExample:
    """Generates frames for a given layout"""
    
    def __init__(self) -> None:
        self.category = "Example"
        self.label = "Button Parameter"
        self.description = "A Button Parameter Example"
        return
    
    def getParameterInfo(self):
        
        validate_button = arcpy.Parameter(
            name="validate_button",
            displayName="Validate",
            direction="Input",
            datatype="GPBoolean",
            parameterType="Required",
        )
        validate_button.value = False
            
        return [validate_button]
    
    def updateParameters(self, parameters:list[arcpy.Parameter], messages:list) -> None:
        """Modify the values and properties of parameters before internal validation is performed."""
        params = named_params(parameters)
        if params.validate_button.value:
            self.validate(parameters, messages)
            params.validate_button.value = False
        return
    
    def validate(self, parameters:list[arcpy.Parameter], messages:list) -> None:
        """This is run when someone clicks the validate checkbox"""
        parameters = named_params(parameters)
        # Implement your validation logic here
        return
    
    def execute(self, parameters:list[arcpy.Parameter], messages:list) -> None:
        """This is run when someone clicks the Run button"""
        # Implement your tool logic here
        return


def named_params(parameters:list[arcpy.Parameter]) -> object:
    """ Return an object with named parameters """
    params = object()
    for param in parameters:
        setattr(params, param.name, param)
    return params

 

0 Kudos
BittersDaniel
New Contributor

Thanks for that example, I might try to implement something like that.

One scenario I am concerned about is if a user starts checking and unchecking the box within seconds, which would probably interrupt any geoprocessing functions that need to happen in the validation and could confuse the tool.

0 Kudos