Reordering Parameters in scripting tools

570
4
Jump to solution
05-18-2022 04:39 PM
AlfredBaldenweck
MVP Regular Contributor

Hi all, 

I'm wondering how to reorder my parameters in a scripting tool.

For example, I just added Parameter #10, but I want it to show up 3rd when the user opens the tool. Is this possible to do?

Thanks!

0 Kudos
1 Solution

Accepted Solutions
Luke_Pinner
MVP Regular Contributor

Assuming you are using a custom toolbox (*.tbx), right click your script in the toolbox and select "Properties", select the parameters tab and then right click on the parameter you want to move and select "Move up" or "Move down". And then change the parameter references in your code.

Luke_Pinner_0-1652935299382.png

 

If you're actually using a Python Toolbox (*.pyt) then it's easier, just move the parameters around in your tool `getParameterInfo` method and then refer to the parameters by name using a dict or namedtuple

 

class Tool
    etc...
    
    def execute(self, parameters, messages):
        parameters = {p.name: p for p in parameters}
        # do something with parameters["some_name"]

# Or
from collections import namedtuple

class Tool
    etc...

    def execute(self, parameters, messages):
        parameters = namedtuple('Parameters', (p.name for p in parameters))(*parameters)
        # do something with parameters[0] or parameters.some_name

 

I also recommend voting for the following idea:

ArcGIS Pro Ideas - Make Python Toolbox parameters a namedtuple instead of a list

 

View solution in original post

4 Replies
I_AM_ERROR
Occasional Contributor

My understanding is that you need to move it to parameter#3 and then update all the references in you code to parameters[3] (or parameters[2])

0 Kudos
Luke_Pinner
MVP Regular Contributor

Assuming you are using a custom toolbox (*.tbx), right click your script in the toolbox and select "Properties", select the parameters tab and then right click on the parameter you want to move and select "Move up" or "Move down". And then change the parameter references in your code.

Luke_Pinner_0-1652935299382.png

 

If you're actually using a Python Toolbox (*.pyt) then it's easier, just move the parameters around in your tool `getParameterInfo` method and then refer to the parameters by name using a dict or namedtuple

 

class Tool
    etc...
    
    def execute(self, parameters, messages):
        parameters = {p.name: p for p in parameters}
        # do something with parameters["some_name"]

# Or
from collections import namedtuple

class Tool
    etc...

    def execute(self, parameters, messages):
        parameters = namedtuple('Parameters', (p.name for p in parameters))(*parameters)
        # do something with parameters[0] or parameters.some_name

 

I also recommend voting for the following idea:

ArcGIS Pro Ideas - Make Python Toolbox parameters a namedtuple instead of a list

 

AlfredBaldenweck
MVP Regular Contributor

Thanks for the response.

Unfortunately, that is exactly what I am trying to avoid; moving the parameters manually like that means I have to change every other reference. So #10-->3, #3-->4, #4-->5, etc. 

I've been able to get around it partially by using categories, but that's not viable for everything.

I thought I saw something the other week about parameter order independent of its index position but I guess not. I'll leave this up for another day or so just in case and then mark you as the solution.

Thanks again

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Unfortunately not a solution for scripting tools, but for python toolboxes, the parameter order can be changed by moving the parameters around in the params list.

AlfredBaldenweck_0-1695243559880.png

 

0 Kudos