0

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?

3
  • 1
    I assume without seeing the original html files that you refer to it's almost impossible to answer this question? But as a fudge, are these html files changing, if not could you hardwire the choices?
    – Hornbydd
    Commented Sep 21, 2021 at 17:43
  • The first json is fixed, but the second one changes depending on the option selected. Commented Sep 21, 2021 at 17:46
  • Are you actually using a Python Toolbox (*.pyt) or are you instead trying to apply tool validation to a Python Script tool in a standard toolbox (*.tbx)?
    – PolyGeo
    Commented Oct 6, 2021 at 22:37

1 Answer 1

0

A colleague was able to figure out the issue: some options, for an unknown, had an empty value u'' in the values2 list. That was the problem, because the ArcGIS wouldnt recognize this empty as a valid item for the filter list, and returned an error.

We added a piece of code to remove these empty values that would appear in some options, and everything worked out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.