Select to view content in your preferred language

Python Toolbox: Only validate when you press run?

197
1
09-17-2024 11:58 AM
AlfredBaldenweck
MVP Regular Contributor

I'm running into an issue where the standard validation is just taking waaaay too long (Describing the input) and I really just need it to validate when I press Run, not beforehand.

That is, if there's an error, only tell me when I press Run.

Has anyone figured out a way to do this?

 

0 Kudos
1 Reply
HaydenWelch
Frequent Contributor

You can just rename your validation function so it's not called in the validation loop and run it at the top of your execute function, something like this:

 

import arcpy
import time

class Tool:
    
    ... # other methods
    
    def validateParameters(self, parameters):
        # Expensive validation goes here
        time.sleep(5)
        
        # Randomly fail validation
        if time.time() % 2 == 0:
            arcpy.AddError("Validation failed")
            return False
        
        arcpy.AddMessage("Validation complete")
        return True
    
    def execute(self, parameters, messages):
        # Expensive execution goes here (after run)
        if not self.validateParameters(parameters):
            arcpy.AddError("Validation failed")
            return
        return