AnsweredAssumed Answered

Adding table to CURRENT map document in arcpy (works on 2nd run only)

Question asked by rastrauch Champion on Apr 7, 2016
Latest reply on Apr 7, 2016 by Dan_Patterson

As part of my script, I am use arcpy.Frequency_analysis to summarize some values.  When run interactively, it automatically adds the table.  In the script, the table is created (and I get back "Exists", but does not add it.


I am using the sample in the TableView—Help | ArcGIS for Desktop 


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.   If I rerun the exact script from the Results table, it adds the table on the second run.  Strange


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.


Any suggestions?


# ...

# 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
#    if not in ArcMap, will skip.
print(" If running in ArcMap, will add the two pts files.")
    # my variables finalOutdd and finalOutProj are set to full paths of the output
    mxd = arcpy.mapping.MapDocument("CURRENT")
except RuntimeError:
    print("Not using ArcMap")
    df  = arcpy.mapping.ListDataFrames(mxd)[0]
    layerList = [fishnetFC, fishnetFCdd] 
    for layer in layerList:
        addLayer = arcpy.mapping.Layer(layer)
        if arcpy.Exists(addLayer):
            arcpy.mapping.AddLayer(df, addLayer, "TOP")
            print("-> Added {0} to map.".format(layer)) #, finalLayer))  
    if arcpy.Exists(sum_table):
        arcpy.mapping.AddTableView(df, sum_tableview)
        #arcpy.mapping.AddTableView(df, sum_tableview)  #tried this, didn't work
        print("can't find it")    #haven't needed this