<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>idea Default Python script in Python window in ArcGIS Pro Ideas</title>
    <link>https://community.esri.com/t5/arcgis-pro-ideas/default-python-script-in-python-window/idi-p/1372108</link>
    <description>&lt;P&gt;&lt;EM&gt;ArcGIS Pro 3.2.1&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;I have a Python script that converts a selection to a definition query:&lt;/P&gt;&lt;PRE&gt;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)&lt;/PRE&gt;&lt;P&gt;Result:&lt;/P&gt;&lt;PRE&gt;OBJECTID IN (4, 5, 8, 9, 10, 14, 15, 16, 20, 24, 25, 26, 29, 30)&lt;/PRE&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="https://community.esri.com/t5/arcgis-pro-ideas/selection-to-definition-query/idc-p/1272879/highlight/true#M24101" target="_self"&gt;Selection to Definition Query&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;&lt;A href="https://gis.stackexchange.com/questions/368848/creating-definition-query-from-selected-features-using-arcgis-pro/456318#456318" target="_self"&gt;Creating definition query from selected features using ArcGIS Pro&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Fri, 19 Jan 2024 17:51:25 GMT</pubDate>
    <dc:creator>Bud</dc:creator>
    <dc:date>2024-01-19T17:51:25Z</dc:date>
    <item>
      <title>Default Python script in Python window</title>
      <link>https://community.esri.com/t5/arcgis-pro-ideas/default-python-script-in-python-window/idi-p/1372108</link>
      <description>&lt;P&gt;&lt;EM&gt;ArcGIS Pro 3.2.1&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;I have a Python script that converts a selection to a definition query:&lt;/P&gt;&lt;PRE&gt;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)&lt;/PRE&gt;&lt;P&gt;Result:&lt;/P&gt;&lt;PRE&gt;OBJECTID IN (4, 5, 8, 9, 10, 14, 15, 16, 20, 24, 25, 26, 29, 30)&lt;/PRE&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="https://community.esri.com/t5/arcgis-pro-ideas/selection-to-definition-query/idc-p/1272879/highlight/true#M24101" target="_self"&gt;Selection to Definition Query&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;&lt;A href="https://gis.stackexchange.com/questions/368848/creating-definition-query-from-selected-features-using-arcgis-pro/456318#456318" target="_self"&gt;Creating definition query from selected features using ArcGIS Pro&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jan 2024 17:51:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-ideas/default-python-script-in-python-window/idi-p/1372108</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-19T17:51:25Z</dc:date>
    </item>
    <item>
      <title>Re: Default Python script in Python window</title>
      <link>https://community.esri.com/t5/arcgis-pro-ideas/default-python-script-in-python-window/idc-p/1372383#M28093</link>
      <description>&lt;P&gt;You could make a script tool (in a toolbox) from your code, and run it that way.&lt;/P&gt;&lt;P&gt;This tutorial has two notebooks that show converting a workflow from a notebook to a script tool.&lt;/P&gt;&lt;P&gt;&lt;A href="https://learngis.maps.arcgis.com/home/item.html?id=24e13f2f4fe8453b9d1107e1f5b1d3cf" target="_blank"&gt;https://learngis.maps.arcgis.com/home/item.html?id=24e13f2f4fe8453b9d1107e1f5b1d3cf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Here is the help topic:&amp;nbsp;&lt;A href="https://pro.arcgis.com/en/pro-app/3.1/help/analysis/geoprocessing/basics/create-a-python-script-tool.htm" target="_blank"&gt;https://pro.arcgis.com/en/pro-app/3.1/help/analysis/geoprocessing/basics/create-a-python-script-tool.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Use arcpy.addMessage to print the result in the tool messages.&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/addmessage.htm" target="_blank"&gt;https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/addmessage.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jan 2024 17:46:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-ideas/default-python-script-in-python-window/idc-p/1372383#M28093</guid>
      <dc:creator>BobBooth1</dc:creator>
      <dc:date>2024-01-19T17:46:30Z</dc:date>
    </item>
    <item>
      <title>Re: Default Python script in Python window</title>
      <link>https://community.esri.com/t5/arcgis-pro-ideas/default-python-script-in-python-window/idc-p/1372389#M28094</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/95621"&gt;@BobBooth1&lt;/a&gt;.&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;...or even easier, not have any parameters at all.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Are you suggesting the tool would use whatever layer is selected in the Contents pane?&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jan 2024 17:59:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-ideas/default-python-script-in-python-window/idc-p/1372389#M28094</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-19T17:59:16Z</dc:date>
    </item>
    <item>
      <title>Re: Default Python script in Python window</title>
      <link>https://community.esri.com/t5/arcgis-pro-ideas/default-python-script-in-python-window/idc-p/1372396#M28095</link>
      <description>&lt;P&gt;Hi Bud,&lt;/P&gt;&lt;P&gt;The code, as written, would pick the layer:&lt;/P&gt;&lt;PRE&gt;lyr = m.listLayers('MY_USER.ROADS')[0] &lt;/PRE&gt;&lt;P&gt;But you could also set it up so it would accept a drop-down pick of layers on your map.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jan 2024 18:02:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-ideas/default-python-script-in-python-window/idc-p/1372396#M28095</guid>
      <dc:creator>BobBooth1</dc:creator>
      <dc:date>2024-01-19T18:02:45Z</dc:date>
    </item>
    <item>
      <title>Re: Default Python script in Python window</title>
      <link>https://community.esri.com/t5/arcgis-pro-ideas/default-python-script-in-python-window/idc-p/1372493#M28097</link>
      <description>&lt;P&gt;Bud,&lt;/P&gt;&lt;P&gt;So two ways to go:&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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)&lt;/LI-CODE&gt;&lt;P&gt;Script tool with a single input parameter that lets you pick the layer:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# 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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The difference is you can set the script tool to accept an input feature layer, so you can choose a layer from the map.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="script_tool_no_params_A.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/92258i8C2C575DFCBF4CC2/image-size/large?v=v2&amp;amp;px=999" role="button" title="script_tool_no_params_A.png" alt="script_tool_no_params_A.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;So you double-click the tool and see:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="This_has_no_params_hard_coded_layer.png" style="width: 265px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/92259i166B125A3B190612/image-size/large?v=v2&amp;amp;px=999" role="button" title="This_has_no_params_hard_coded_layer.png" alt="This_has_no_params_hard_coded_layer.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Click Run...&lt;/P&gt;&lt;P&gt;The hard-coded target layer gets its definition query updated.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="script_tool_no_params_B.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/92260i18AE6C5D007F6F64/image-size/large?v=v2&amp;amp;px=999" role="button" title="script_tool_no_params_B.png" alt="script_tool_no_params_B.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Versus the case where you set up a script tool with an input layer parameter:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="script_tool_with_parameter_A.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/92261i277BA35B8E402313/image-size/large?v=v2&amp;amp;px=999" role="button" title="script_tool_with_parameter_A.png" alt="script_tool_with_parameter_A.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="script_tool_with_parameter_B.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/92262i574E00CF62B83D4F/image-size/large?v=v2&amp;amp;px=999" role="button" title="script_tool_with_parameter_B.png" alt="script_tool_with_parameter_B.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="script_tool_with_parameter_C.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/92263i3DBDFC4F58995A62/image-size/large?v=v2&amp;amp;px=999" role="button" title="script_tool_with_parameter_C.png" alt="script_tool_with_parameter_C.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;The difference when setting up the script tool is you have to specify a parameter and set the type to feature layer.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="script_tool_with_parameter_D.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/92264i445DC4B5BCD2BA65/image-size/large?v=v2&amp;amp;px=999" role="button" title="script_tool_with_parameter_D.png" alt="script_tool_with_parameter_D.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 Jan 2024 20:05:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-ideas/default-python-script-in-python-window/idc-p/1372493#M28097</guid>
      <dc:creator>BobBooth1</dc:creator>
      <dc:date>2024-01-19T20:05:46Z</dc:date>
    </item>
    <item>
      <title>Re: Default Python script in Python window</title>
      <link>https://community.esri.com/t5/arcgis-pro-ideas/default-python-script-in-python-window/idc-p/1372667#M28106</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/95621"&gt;@BobBooth1&lt;/a&gt;&amp;nbsp;Thanks Bob. That helps.&lt;/P&gt;</description>
      <pubDate>Sat, 20 Jan 2024 08:31:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-ideas/default-python-script-in-python-window/idc-p/1372667#M28106</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2024-01-20T08:31:12Z</dc:date>
    </item>
    <item>
      <title>Re: Default Python script in Python window - Status changed to: Closed</title>
      <link>https://community.esri.com/t5/arcgis-pro-ideas/default-python-script-in-python-window/idc-p/1374704#M28182</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/95621"&gt;@BobBooth1&lt;/a&gt;&amp;nbsp;for helping&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/351335"&gt;@Bud&lt;/a&gt;&amp;nbsp;with an existing solution. I'll add that there are some additional options for you:&lt;/P&gt;&lt;P&gt;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:&amp;nbsp;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/help/analysis/geoprocessing/basics/schedule-geoprocessing-tools.htm" target="_blank"&gt;Schedule geoprocessing tools—ArcGIS Pro | Documentation&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;2. &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/get-started/pro-notebooks.htm" target="_self"&gt;Notebooks in ArcGIS Pro&lt;/A&gt;&amp;nbsp;can serve as a document containing a (Python code) workflow.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jan 2024 22:42:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-ideas/default-python-script-in-python-window/idc-p/1374704#M28182</guid>
      <dc:creator>HannesZiegler</dc:creator>
      <dc:date>2024-01-25T22:42:07Z</dc:date>
    </item>
  </channel>
</rss>

