Insert data in a table layer ArcGIS online with python

3799
3
Jump to solution
01-17-2021 05:10 PM
testfiverr
New Contributor

I have a table layer in ArcGIS online and I want to insert data through a python program, is possible? If it is not possible, what method would be the optimal one to be able to insert data automatically from a csv to the table (without doing it manually through the web page)

 

For example, insert here id = 3, text = 'hi3'

idtext
1hi1
2hi2
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Hi @testfiverr ,

This can be done using the ArcGIS Python API - more specifically by using the API to get a Table object that represents you table in ArcGIS Online, then using the table's edit_features method to add new data to the table.

First we'll need to use the ArcGIS Python API to authenticate with ArcGIS Online and locate the hosted table that we want to insert data into. In the snippet of code below, the value for your table_item_id can be obtain from the address bar of your web browser when viewing the item description page for the table in ArcGIS Online. This just serves as a unique identifier to easily search for the specific item we're after.  The gis.content.search method will return a list of items even if there's only one search result - so we can add [0] to the end of the line to grab the first item out of that list and put store that in our hosted_table variable. 

from arcgis.gis import GIS

table_item_id = ""
username="your-username"
password="your-password"

gis = GIS("https://www.arcgis.com/", username, password)
hosted_table_item = gis.content.search(table_item_id)[0]

 

Now we've got a reference to the hosted table item, we can access a table object associated with this item.  Assuming there's only one table in our hosted table item, we can grab the first item out of the tables list associated with this item. Next we need some data to insert into the table - assuming we want add data to attributes named 'id' and 'text', we would format the data as in the new_data variable below. Then all we need to do, is call the edit_features method using the adds parameter to indicate we want add the new_data to the table. 

table = hosted_table_item.tables[0]
new_data = [{'attributes': {'id': '3', 'text': 'hi3'}}]
table.edit_features(adds=new_data)

It should also be noted, that if the ID attribute is an auto-incrementing value as in an Object ID - you can get away with skipping that one and  only specifying the value for the 'text' attribute ie. new_data = [{'attributes': {'text': 'hi3'}}] 

Hope that helps!

Cheers,

James

View solution in original post

3 Replies
by Anonymous User
Not applicable

Hi @testfiverr ,

This can be done using the ArcGIS Python API - more specifically by using the API to get a Table object that represents you table in ArcGIS Online, then using the table's edit_features method to add new data to the table.

First we'll need to use the ArcGIS Python API to authenticate with ArcGIS Online and locate the hosted table that we want to insert data into. In the snippet of code below, the value for your table_item_id can be obtain from the address bar of your web browser when viewing the item description page for the table in ArcGIS Online. This just serves as a unique identifier to easily search for the specific item we're after.  The gis.content.search method will return a list of items even if there's only one search result - so we can add [0] to the end of the line to grab the first item out of that list and put store that in our hosted_table variable. 

from arcgis.gis import GIS

table_item_id = ""
username="your-username"
password="your-password"

gis = GIS("https://www.arcgis.com/", username, password)
hosted_table_item = gis.content.search(table_item_id)[0]

 

Now we've got a reference to the hosted table item, we can access a table object associated with this item.  Assuming there's only one table in our hosted table item, we can grab the first item out of the tables list associated with this item. Next we need some data to insert into the table - assuming we want add data to attributes named 'id' and 'text', we would format the data as in the new_data variable below. Then all we need to do, is call the edit_features method using the adds parameter to indicate we want add the new_data to the table. 

table = hosted_table_item.tables[0]
new_data = [{'attributes': {'id': '3', 'text': 'hi3'}}]
table.edit_features(adds=new_data)

It should also be noted, that if the ID attribute is an auto-incrementing value as in an Object ID - you can get away with skipping that one and  only specifying the value for the 'text' attribute ie. new_data = [{'attributes': {'text': 'hi3'}}] 

Hope that helps!

Cheers,

James

testfiverr
New Contributor

yeah it works, thank you very much

0 Kudos
DanPatterson
MVP Esteemed Contributor

Do you have to prepare the table in AGOL? If you are using arcmap or arcgis pro, why not let it do the heavy lifting

Table To Table (Conversion)—ArcGIS Pro | Documentation

then feed the result to whatever


... sort of retired...
0 Kudos