How to reference variables as input to script tool?

3103
7
Jump to solution
05-31-2021 05:01 PM
ChrisGAEG
Regular Contributor

Hi there, 

I have a really simple arcpy script I use to pull data from my orgs portal. I'd like to turn this into a script tool so I can share it with my co-worker. Basically the script assigns feature layer urls to the different layers we need to pull in on a regular basis from portal to ArcPro. 

 

What I'm aiming for is for us to just input the variable for the layer we need and then for the tool to add that data from path to the map. My question is, is this possible and if so what would the input parameter type for 'Layer' be? 

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

I think I see the issue now, I'd say you're trying to input the string slack_loop etc. and expecting it to reference the variable slack_loop.

here's an example of what should work instead, but this is before my second coffee so it's very basic and hopefully someone can point you to a more elegant solution.

 

url_list = [['structure', rdof_design + '/0'],
            ['slack_loop', rdof_design + '/1'],
            ...
            ]

tool_input = arcpy.GetParameterAsText(0)

url = ''
for item in url_list:
    if item[0] == tool_input:
        url = item[1]
        break

maps.addDataFromPath(url)

 

 

 

View solution in original post

0 Kudos
7 Replies
DavidAnderson_GISS
Occasional Contributor

The layer parameter is a string.  Use r at the beginning if using Windows paths to indicate  raw string, so the backslash is interpreted properly.
To suggest a coding fix, the 23 lines of appending can be reduced to 1 line:

design_list = [rdof_design+"/"+str(i) for i in range(1,23)]

 

 

 

David Anderson
david.anderson@usda.gov
0 Kudos
ChrisGAEG
Regular Contributor

Thanks for the input. I'm not referencing any local paths in the script, just urls to our feature services. And i've created a list for the data before but I still am having trouble inputting these as string inputs. I get a runtime error - 

File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\_mp.py", line 1779, in addDataFromPath
return convertArcObjectToPythonObject(self._arc_object.addDataFromPath(*gp_fixargs((data_path,), True)))
RuntimeError

0 Kudos
DavidPike
MVP Frequent Contributor

I'd check there's not a spelling mistake in the URL (be aware of capitalisation also) this would throw the error you see.  try a print statement and try the URL in a web browser.  Are you also signed in to AGOL etc (not sure if you might need to specify a token in the URL?) try the same code on an AGOL sample service to discard this as a potential issue.

0 Kudos
ChrisGAEG
Regular Contributor

Thanks for the reply David. The links all work and I am authenticated in my orgs portal in pro. The problem I'm having is that Layer is not being assigned in the tool. I have it set as a string input type parameter for the tool, and set some of the variables I assigned in the script as options to choose from. But these aren't being assigned to Layer when chosen and ran. 

0 Kudos
DavidPike
MVP Frequent Contributor

I think I see the issue now, I'd say you're trying to input the string slack_loop etc. and expecting it to reference the variable slack_loop.

here's an example of what should work instead, but this is before my second coffee so it's very basic and hopefully someone can point you to a more elegant solution.

 

url_list = [['structure', rdof_design + '/0'],
            ['slack_loop', rdof_design + '/1'],
            ...
            ]

tool_input = arcpy.GetParameterAsText(0)

url = ''
for item in url_list:
    if item[0] == tool_input:
        url = item[1]
        break

maps.addDataFromPath(url)

 

 

 

0 Kudos
DavidPike
MVP Frequent Contributor

Edit - should've used a dictionary (duh).  But same idea.

0 Kudos
ChrisGAEG
Regular Contributor

This is exactly what I was trying to do. I had read the documentation of the arcpy.GetParameterAsText() , 

but was a bit thrown off that the function didn't use [] to reference list index. This really helps clarify it for me. Thanks a lot!!