I'm trying to build a toolbox, and I would like users to be able to select two possible values from two lists.
The first list is stored in a JSON, that I access using HTML.
The second list is also a JSON, but its HTML varies depending on the first one selected.
For such, I trying to code a Tool Validator.
Here's the code:
import arcpy
import requests
class ToolValidator(object):
def __init__(self):
self.params = arcpy.GetParameterInfo()
def initializeParameters(self):
return
def updateParameters(self):
data = requests.get(u'-first.html-').json()
values = [d['facility_name'] for d in data if 'facility_name' in d]
self.params[0].filter.list = values
return
def updateMessages(self):
if self.params[0].altered:
facility = self.params[0].value
facility.encode('utf-8').decode('utf-8')
data2 = requests.get(u'-second.html-{}'.format(facility)).json()
values2 = [d['task_code'] for d in data2]
self.params[1].filter.list = values2
return
It works fine for the most part, but for some list items, I get an error (Value Error: FilterObject: invalid list value)
and do not get the second list.
At first, I thought it could be an encoding problem since the HTML can have values with accents. However, I tested several different values, but I get this error in some items that do not have special characters, and I don't have any error in other items that do have special characters.
params[0]
and params[1]
are texts.
I have tried a similar code directly on the python terminal and it works perfectly. I only get errors using the toolbox.
Any ideas on what am I doing wrong?
*.pyt
) or are you instead trying to apply tool validation to a Python Script tool in a standard toolbox (*.tbx
)?