<?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>topic Adding table to CURRENT map document in arcpy (works on 2nd run only) in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/adding-table-to-current-map-document-in-arcpy/m-p/685981#M53084</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;As part of my script, I am use arcpy.Frequency_analysis to summarize some values.&amp;nbsp; When run interactively, it automatically adds the table.&amp;nbsp; In the script, the table is created (and I get back "Exists", but does not add it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am using the sample in the &lt;A href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/tableview-class.htm" title="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/tableview-class.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;TableView—Help | ArcGIS for Desktop&lt;/A&gt;​&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I run the tool, it does tell me the table exists, so runs the arcpy.mapping.AddTableView command, but the table is not added.&amp;nbsp;&amp;nbsp; If I rerun the exact script from the Results table, it adds the table on the second run.&amp;nbsp; Strange&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It's a very fast script, so a rerun is not a big deal (nor is dragging the table over to add), but for future use by other users, it would be nice if it worked as expected.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any suggestions?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# ...

# running frequency to get length summary
sum_table = arcpy.os.path.join(theWorkspace, "{0}_SumLen".format(fishnetFCdd))
arcpy.Frequency_analysis(fishnetFCdd, sum_table, "TransType", "Len_m;Len_km")
sum_tableview = arcpy.mapping.TableView(sum_table)


# If running this in ArcMap, can add the new layer...will have to manually save at least during testing
#&amp;nbsp;&amp;nbsp;&amp;nbsp; if not in ArcMap, will skip.
print(" If running in ArcMap, will add the two pts files.")
try:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # my variables finalOutdd and finalOutProj are set to full paths of the output
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT")
except RuntimeError:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print("Not using ArcMap")
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; df&amp;nbsp; = arcpy.mapping.ListDataFrames(mxd)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; layerList = [fishnetFC, fishnetFCdd] 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for layer in layerList:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; addLayer = arcpy.mapping.Layer(layer)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcpy.Exists(addLayer):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(df, addLayer, "TOP")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("-&amp;gt; Added {0} to map.".format(layer)) #, finalLayer))&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcpy.Exists(sum_table):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("exists")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddTableView(df, sum_tableview)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #arcpy.mapping.AddTableView(df, sum_tableview)&amp;nbsp; #tried this, didn't work
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("can't find it")
&amp;nbsp;&amp;nbsp;&amp;nbsp; #mxd.save()&amp;nbsp;&amp;nbsp;&amp;nbsp; #haven't needed this 
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
#....&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 04:51:16 GMT</pubDate>
    <dc:creator>RebeccaStrauch__GISP</dc:creator>
    <dc:date>2021-12-12T04:51:16Z</dc:date>
    <item>
      <title>Adding table to CURRENT map document in arcpy (works on 2nd run only)</title>
      <link>https://community.esri.com/t5/python-questions/adding-table-to-current-map-document-in-arcpy/m-p/685981#M53084</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;As part of my script, I am use arcpy.Frequency_analysis to summarize some values.&amp;nbsp; When run interactively, it automatically adds the table.&amp;nbsp; In the script, the table is created (and I get back "Exists", but does not add it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am using the sample in the &lt;A href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/tableview-class.htm" title="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/tableview-class.htm" rel="nofollow noopener noreferrer" target="_blank"&gt;TableView—Help | ArcGIS for Desktop&lt;/A&gt;​&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If I run the tool, it does tell me the table exists, so runs the arcpy.mapping.AddTableView command, but the table is not added.&amp;nbsp;&amp;nbsp; If I rerun the exact script from the Results table, it adds the table on the second run.&amp;nbsp; Strange&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It's a very fast script, so a rerun is not a big deal (nor is dragging the table over to add), but for future use by other users, it would be nice if it worked as expected.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any suggestions?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# ...

# running frequency to get length summary
sum_table = arcpy.os.path.join(theWorkspace, "{0}_SumLen".format(fishnetFCdd))
arcpy.Frequency_analysis(fishnetFCdd, sum_table, "TransType", "Len_m;Len_km")
sum_tableview = arcpy.mapping.TableView(sum_table)


# If running this in ArcMap, can add the new layer...will have to manually save at least during testing
#&amp;nbsp;&amp;nbsp;&amp;nbsp; if not in ArcMap, will skip.
print(" If running in ArcMap, will add the two pts files.")
try:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # my variables finalOutdd and finalOutProj are set to full paths of the output
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT")
except RuntimeError:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print("Not using ArcMap")
else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; df&amp;nbsp; = arcpy.mapping.ListDataFrames(mxd)[0]
&amp;nbsp;&amp;nbsp;&amp;nbsp; layerList = [fishnetFC, fishnetFCdd] 
&amp;nbsp;&amp;nbsp;&amp;nbsp; for layer in layerList:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; addLayer = arcpy.mapping.Layer(layer)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcpy.Exists(addLayer):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddLayer(df, addLayer, "TOP")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("-&amp;gt; Added {0} to map.".format(layer)) #, finalLayer))&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcpy.Exists(sum_table):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("exists")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.mapping.AddTableView(df, sum_tableview)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #arcpy.mapping.AddTableView(df, sum_tableview)&amp;nbsp; #tried this, didn't work
&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("can't find it")
&amp;nbsp;&amp;nbsp;&amp;nbsp; #mxd.save()&amp;nbsp;&amp;nbsp;&amp;nbsp; #haven't needed this 
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
#....&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:51:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-table-to-current-map-document-in-arcpy/m-p/685981#M53084</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2021-12-12T04:51:16Z</dc:date>
    </item>
    <item>
      <title>Re: Adding table to CURRENT map document in arcpy (works on 2nd run only)</title>
      <link>https://community.esri.com/t5/python-questions/adding-table-to-current-map-document-in-arcpy/m-p/685982#M53085</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;A href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/refreshtoc.htm" title="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/refreshtoc.htm"&gt;RefreshTOC—Help | ArcGIS for Desktop&lt;/A&gt; &lt;/P&gt;&lt;P&gt;&lt;A href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/refreshactiveview.htm" title="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/refreshactiveview.htm"&gt;RefreshActiveView—Help | ArcGIS for Desktop&lt;/A&gt;&lt;/P&gt;&lt;P&gt;do either have any effect?&amp;nbsp; I was wondering if the first would work if the data view were selected in the TOC &lt;/P&gt;&lt;P&gt;and just in case check to make sure results are added to display, although supposedly for tools only&lt;/P&gt;&lt;P&gt;&lt;A href="http://desktop.arcgis.com/en/arcmap/latest/analyze/executing-tools/using-geoprocessing-options-to-control-tool-execution.htm" title="http://desktop.arcgis.com/en/arcmap/latest/analyze/executing-tools/using-geoprocessing-options-to-control-tool-execution.htm"&gt;Using geoprocessing options to control tool execution—Help | ArcGIS for Desktop&lt;/A&gt; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 07 Apr 2016 21:18:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-table-to-current-map-document-in-arcpy/m-p/685982#M53085</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-04-07T21:18:27Z</dc:date>
    </item>
    <item>
      <title>Re: Adding table to CURRENT map document in arcpy (works on 2nd run only)</title>
      <link>https://community.esri.com/t5/python-questions/adding-table-to-current-map-document-in-arcpy/m-p/685983#M53086</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Does running &lt;A href="http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-functions/refreshtoc.htm"&gt;RefreshTOC &lt;/A&gt;make a difference?&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 07 Apr 2016 21:20:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-table-to-current-map-document-in-arcpy/m-p/685983#M53086</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2016-04-07T21:20:40Z</dc:date>
    </item>
    <item>
      <title>Re: Adding table to CURRENT map document in arcpy (works on 2nd run only)</title>
      <link>https://community.esri.com/t5/python-questions/adding-table-to-current-map-document-in-arcpy/m-p/685984#M53087</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you Dan and Joshua.&amp;nbsp; RefreshTOC was the missing piece.&amp;nbsp; I adding the refresh right after adding it worked.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;arcpy.mapping.AddTableView(df, sum_tableview)
arcpy.RefreshActiveView()&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Dan responded first, so he got the check. But I appreciate the answeres from both.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:51:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-table-to-current-map-document-in-arcpy/m-p/685984#M53087</guid>
      <dc:creator>RebeccaStrauch__GISP</dc:creator>
      <dc:date>2021-12-12T04:51:19Z</dc:date>
    </item>
    <item>
      <title>Re: Adding table to CURRENT map document in arcpy (works on 2nd run only)</title>
      <link>https://community.esri.com/t5/python-questions/adding-table-to-current-map-document-in-arcpy/m-p/685985#M53088</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;IMG src="https://community.esri.com/legacyfs/online/emoticons/happy.png" /&gt; check to Joshua as well&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 07 Apr 2016 22:08:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/adding-table-to-current-map-document-in-arcpy/m-p/685985#M53088</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2016-04-07T22:08:31Z</dc:date>
    </item>
  </channel>
</rss>

