I cannot enable parameters in a Python toolbox if they are conditionally disabled. My Toolbox code is as follows:
import os, shutil
import arcpy as ap
import arcgis
from arcgis.gis import GIS
global AGOL_TYPES
AGOL_TYPES = ["Feature Collection",
"Feature Service",
"KML",
"KML Collection",
"CSV"
]
class Toolbox(object):
def __init__(self):
self.label = "AGOL Backup"
self.alias = "Backup"
self.tools = [BackupLocal]
class BackupLocal(object):
def __init__(self):
self.label = "Backup"
self.description = "Create local backup of ArcGis Online items."
self.canTunInBackground = False
def getParameterInfo(self):
global AGOL_TYPES
USR = ap.Parameter(
displayName = "AGOL username",
name = "usr",
datatype = "GPString",
parameterType = "Required",
direction = "Input")
PW = ap.Parameter(
displayName = "Password",
name = "psswrd",
datatype = "GPEncryptedString",
parameterType = "Optional",
direction = "Input")
COMPLETE = ap.Parameter(
displayName = "Complete backup",
name = "complete",
datatype = "GPBoolean",
parameterType = "Required",
direction = "Input")
COMPLETE.value = False
OWNER = ap.Parameter(
displayName = "Owner (if different from user)",
name = "owner",
datatype = "GPString",
parameterType = "Optional",
direction = "Input")
OWNER.value = None
DTYPES = ap.Parameter(
displayName = "Data types",
name = "dtypes",
datatype = "GPString",
parameterType = "Optional",
direction = "Input",
multiValue = True)
DTYPES.filter.list = AGOL_TYPES
TAGS = ap.Parameter(
displayName = "Tags",
name = "tags",
datatype = "GPString",
parameterType = "Optional",
direction = "Input",
multiValue = True)
OVERWRITE = ap.Parameter(
displayName = "Overwrite",
name = "overwrite",
datatype = "GPBoolean",
parameterType = "Required",
direction = "Input")
OVERWRITE.value = False
OUT_DIR = ap.Parameter(
displayName = "Backup directory",
name = "out_dir",
datatype = "DEFolder",
parameterType = "Required",
direction = "Output")
OUT_DIR.value = "F:/AGOL_backup"
parameters = [USR, PW, COMPLETE, OWNER, DTYPES, TAGS, OVERWRITE, \
OUT_DIR]
return parameters
def isLicensed(self):
return True
def updateParameters(self, parameters):
[USR, PW, COMPLETE, OWNER, DTYPES, TAGS, OVERWRITE, OUT_DIR] = \
parameters
if COMPLETE and COMPLETE.altered:
OWNER.enabled = False
DTYPES.enabled = False
TAGS.enabled = False
else:
parameters[3].enabled = True
OWNER.enabled = True
DTYPES.enabled = True
TAGS.enabled = True
return
def updateMessages(self, parameters):
return
def execute(self, parameters, messages):
return
I want to show the OWNER, DTYPE and TAG parameters only if COMPLETE is set to False. However, even if the COMPLETE parameter is set to False, these parameters do not show up in the Toolbox interface. Why is that?
As you can see, this happens regardless of whether I call the parameters after assigning them to variables, or directly in the parameter list. Moreover, they do not show up despite the default value for COMPLETE being False, i.e., these parameters are hidden whenever there is just the possibility of their .enabled attribute being set to False.
Solved! Go to Solution.
if COMPLETE and COMPLETE.altered:
You're not checking the value of the parameter, you're checking if a non-empty value is assigned to the variable COMPLETE.
Take a look at this code and its output:
test_values = [False, 0, None, "", [], ()]
for value in test_values:
msg = str(value) + ": "
if value:
msg += "TRUE"
else:
msg += "FALSE"
print(msg)
False: FALSE 0: FALSE None: FALSE : FALSE []: FALSE (): FALSE
So, your if COMPLETE: could also be written as
if COMPLETE not in [False, 0, None, "", [], ()]: # and maybe some other values, too
and that will always return False, because COMPLETE has a value (it is an arcpy.Parameter).
Instead, you have to check the value of that arcpy.Parameter:
if COMPLETE.value:
if COMPLETE and COMPLETE.altered:
You're not checking the value of the parameter, you're checking if a non-empty value is assigned to the variable COMPLETE.
Take a look at this code and its output:
test_values = [False, 0, None, "", [], ()]
for value in test_values:
msg = str(value) + ": "
if value:
msg += "TRUE"
else:
msg += "FALSE"
print(msg)
False: FALSE 0: FALSE None: FALSE : FALSE []: FALSE (): FALSE
So, your if COMPLETE: could also be written as
if COMPLETE not in [False, 0, None, "", [], ()]: # and maybe some other values, too
and that will always return False, because COMPLETE has a value (it is an arcpy.Parameter).
Instead, you have to check the value of that arcpy.Parameter:
if COMPLETE.value:
Oh, thanks. Today is not my day, it seems. I should have known that I need to access the value and not the parameter 😬