Select to view content in your preferred language

ArcGIS API for Python : Error while publishing content

574
1
07-20-2023 10:24 PM
AmritaJayakumar
New Contributor
Dear Team,
 
I am trying to implement the ArcGIS API for python where in the code im trying to connect to a developer account using an API key, ready a csv file, import into a dataframe and then upload the content as a feature layer.
 
I have written the code as said in the tutorial. However, it returns an error "'NoneType' object is not subscriptable" at the line csv_item = contentManager.add(csv_properties) as per my understanding.
 
Please find below the code snippet: (Portion that errs out is highlighted in yellow)
 

 

def upload_csv(request):
    try:

        csv_filename = request.GET.get("csv_filename","")
        ARC_GIS_API_KEY = apps.get_app_config('arcgis_bridge').ARC_GIS_API_KEY
        if csv_filename:
            if ARC_GIS_API_KEY:

                    gis = GIS(api_key=ARC_GIS_API_KEY)

                    if gis:
                        dir_path = os.path.dirname(os.path.realpath(__file__))
                        print("dir_path :: ", dir_path)
                        data_path = os.path.join(dir_path, "..\input_data")
                        print("data_path :: ", data_path)
                        csv_path = os.path.join(data_path, csv_filename)
                        print("csv_path :: ", csv_path)
                        ports_df = pandas.read_csv(csv_path)
                        # print("ports_df :: ", ports_df)
                        ports_fc = gis.content.import_data(ports_df)
                        print("ports_fc :: ", ports_fc)
                        # ports_fc_dict = dict(ports_fc.properties)
                        ports_fc_dict = dict(ports_fc.layer)
                        # print("ports_fc_dict :: ", ports_fc_dict)
                        ports_json = json.dumps({"featureCollection": {"layers": [ports_fc_dict]}})
                        # print("ports_json :: ", ports_json)
                        csv_properties = {
                            'title': 'test file',
                            'description': 'attempting content upload from API',
                            'tags': 'arcgis, python, testing, csv, importing',
                            'text': ports_json,
                            'type': 'Feature Collection'
                        }
                        try:
                            contentManager = gis.content
                            print("contentManager :: ", contentManager)
                            csv_item = contentManager.add(csv_properties)
                            print("csv_item :: ", csv_item)
                            csv_item_publish = csv_item.publish()
                            print("csv_item_publish :: ", csv_item_publish)
                        except Exception as e:
                            print("csv_item ERROR :: ", str(e))
                            return JsonResponse({"error": True, "message":str(e)})
                        return JsonResponse({"error": False, "message":"CSV Uploaded Successfully.","csv_item_publish":csv_item_publish})
                    else:
                        return JsonResponse({"error": True, "message":"ARC GIS API CONNECTION COULD NOT BE OBTAINED.","csv_item_publish":{}})
            else:
                return JsonResponse({"error": True, "message":"ARC GIS API KEY IS MISSING.","csv_item_publish":{}})
        else:
            return JsonResponse({"error": True, "message":"CSV FILE NAME IS MISSING.","csv_item_publish":{}})

    except Exception as e:
        return JsonResponse({"error": True, "message":str(e), "csv_item_publish":''})

 

 
 
Terminal Output:
 
 

 

[21/Jul/2023 10:07:50] "GET /arcgis_bridge/uploadCSV?csv_filename=file1.CSV HTTP/1.1" 200 68
dir_path ::  C:\Users\aksha\Projects\arc_gis\sree_app\arcgis_bridge
data_path ::  C:\Users\aksha\Projects\arc_gis\sree_app\arcgis_bridge\..\input_data
csv_path ::  C:\Users\aksha\Projects\arc_gis\sree_app\arcgis_bridge\..\input_data\file1.CSV
ports_fc ::  <FeatureCollection>
contentManager ::  <arcgis.gis.ContentManager object at 0x0000023BC9E03070>
csv_item ERROR ::  'NoneType' object is not subscriptable

 

 
Looking forward to any support on this regard.
 
Tags (3)
0 Kudos
1 Reply
by Anonymous User
Not applicable

It means that your return from the content.add(...) is None.

contentManager.add(csv_properties)

JeffK_0-1690026327114.png

Something is not set correctly and it's not adding so when it gets to the print, it cant subscript None to str.

0 Kudos