Select to view content in your preferred language

Default Python script in Python window

748
6
01-18-2024 09:58 PM
Status: Closed
Labels (1)
Bud
by
Esteemed Contributor

ArcGIS Pro 3.2.1

I have a Python script that converts a selection to a definition query:

def qdef_selected_features(lyr):
    desc = arcpy.Describe(lyr)
    # Get a semicolon-delimited string of selected feature IDs 
    fid_list = desc.FIDSet.split(";")
    # build the query definition
    query = '{} IN ({})'.format(desc.OIDFieldName, ",".join(fid_list))
    # apply the query definition back to the layer   
    lyr.definitionQuery = query

aprx = arcpy.mp.ArcGISProject('current') 
m = aprx.activeMap 
lyr = m.listLayers('MY_USER.ROADS')[0] 
qdef_selected_features(lyr)

Result:

OBJECTID IN (4, 5, 8, 9, 10, 14, 15, 16, 20, 24, 25, 26, 29, 30)

That script is the only Python script I ever use. I want to save it as the default script so that it opens automatically when I open the Python window.

Could functionality be added that lets me set a default Python script? That would be more efficient than opening the file via File Explorer and copying/pasting the script into the Python window each time I want to use it.

6 Comments
BobBooth1

You could make a script tool (in a toolbox) from your code, and run it that way.

This tutorial has two notebooks that show converting a workflow from a notebook to a script tool.

https://learngis.maps.arcgis.com/home/item.html?id=24e13f2f4fe8453b9d1107e1f5b1d3cf

Here is the help topic: https://pro.arcgis.com/en/pro-app/3.1/help/analysis/geoprocessing/basics/create-a-python-script-tool...

You could set up the tool so it takes an input layer (even set your favorite layer as the default) or even easier, not have any parameters at all.

Use arcpy.addMessage to print the result in the tool messages.

https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/addmessage.htm

 

Bud
by

Thanks @BobBooth1.

...or even easier, not have any parameters at all.

Are you suggesting the tool would use whatever layer is selected in the Contents pane?

BobBooth1

Hi Bud,

The code, as written, would pick the layer:

lyr = m.listLayers('MY_USER.ROADS')[0] 

But you could also set it up so it would accept a drop-down pick of layers on your map. 

BobBooth1

Bud,

So two ways to go:

Script tool with no parameters, you would double-click the tool and click Run, and it would apply the definition query to the hard-coded layer.

def qdef_selected_features(lyr):
    desc = arcpy.Describe(lyr)
    # Get a semicolon-delimited string of selected feature IDs
    fid_list = desc.FIDSet.split(";")
    # build the query definition
    query = '{} IN ({})'.format(desc.OIDFieldName, ",".join(fid_list))
    # apply the query definition back to the layer
    lyr.definitionQuery = query

aprx = arcpy.mp.ArcGISProject('current')
m = aprx.activeMap

# I hard-coded my layer name, TestBob here
lyr = m.listLayers('TestBob')[0]
qdef_selected_features(lyr)

Script tool with a single input parameter that lets you pick the layer:

# configure script to accept a layer input from the script tool

lyr = arcpy.GetParameterAsText(0)

def qdef_selected_features(lyr):
    desc = arcpy.Describe(lyr)
    # Get a semicolon-delimited string of selected feature IDs
    fid_list = desc.FIDSet.split(";")
    # build the query definition
    query = '{} IN ({})'.format(desc.OIDFieldName, ",".join(fid_list))
    # apply the query definition back to the layer
    lyr.definitionQuery = query

aprx = arcpy.mp.ArcGISProject('current')
m = aprx.activeMap
thelyr = m.listLayers(lyr)[0]
qdef_selected_features(thelyr)

 

The difference is you can set the script tool to accept an input feature layer, so you can choose a layer from the map.

script_tool_no_params_A.png

So you double-click the tool and see:

This_has_no_params_hard_coded_layer.png

Click Run...

The hard-coded target layer gets its definition query updated.

script_tool_no_params_B.png

Versus the case where you set up a script tool with an input layer parameter:

script_tool_with_parameter_A.png

script_tool_with_parameter_B.png

script_tool_with_parameter_C.png

The difference when setting up the script tool is you have to specify a parameter and set the type to feature layer.

script_tool_with_parameter_D.png

Bud
by

@BobBooth1 Thanks Bob. That helps.

HannesZiegler
Status changed to: Closed

Thanks @BobBooth1 for helping @Bud with an existing solution. I'll add that there are some additional options for you:

1. If you go the route of a Script tool, and you run the same script repeatedly on a schedule, you could look into scheduling the tool to run: Schedule geoprocessing tools—ArcGIS Pro | Documentation

2. Notebooks in ArcGIS Pro can serve as a document containing a (Python code) workflow.

3. Probably not what you are looking for but possibly more efficient than copy/pasting code from a script: You can right-click the prompt area of the Python Window and select Load Code from the context menu to load a script file's contents directly into the prompt.

In summary, we will close this idea as there are established routes for achieving a similar result that work better with the intended use pattern of the Python Window and the broader user story of Python in ArcGIS Pro.