Select to view content in your preferred language

Update the popup content of a feature layer

273
3
3 weeks ago
Labels (1)
sandra555
Regular Contributor

I have a feature layer that I am not using in a WebMap (I am using it in a Details element in a dashboard). I want to use the python API to set some of the fields to hidden AND, if possible, set the order of the way the fields display in the popup (that is how they appear in the Details). I have looked around and found information on how to do it with a webmap, but not a feature layer. 

I can see the visibility info like this:   

item = gis.content.get(item_id)

item.get_data()['layers'][0]['popupInfo']['fieldInfos']

I tried the following, and it does not result in an error, but it is not changing the visibility.
item.layers[0].manager.update_definition({'fieldInfos': {'fieldName': 'Longitude', 'visible': False}})

What is the proper syntax for this update? Is it possible to set the order programmatically?

Thanks in advance for any assistance!

 

0 Kudos
3 Replies
Clubdebambos
MVP Regular Contributor

Hi @sandra555,

You want to edit the definition returned from get_data() and then apply to the Feature Layer.

Here's what I would do, first print the current popupInfo to screen.

from arcgis.gis import GIS
import json

## access AGOL
agol = GIS("home")

## the id for the layer to upodate the popup for (you can see this in the layer URL)
lyr_id = 0

## get the Feature Service as an Item object
fs_item = agol.content.get("FS_ITEM_ID")

## get the JSON definition for the item
fs_def = fs_item.get_data()

## access the popupInfo for the layer of interest
popupInfo = [lyr["popupInfo"] for lyr in fs_def["layers"] if lyr["id"] == lyr_id][0]

## print to screen
print(json.dumps(popupInfo, indent = 4))

 

Take the printed popupInfo and change any true to True and false to False (find and replace). Then manipulate the popupInfo definition, set visible to True or False, move a field entry to another position for examples. If your definition has a popupElements I would keep the fieldInfos in there the same as the other fieldInfos. You will get familiar with the makeup of the popupInfo definition over time.

Now you want to apply the update.

from arcgis.gis import GIS

## access AGOL
agol = GIS("home")

## the id for the layer to update the popup for (yiou can get this from the layer url)
lyr_id = 0

## get the Feature Service as an Item object
fs_item = agol.content.get("FS_ITEM_ID")

## get the definition
fs_def = fs_item.get_data()

## the altered popupInfo
popupInfo = {
    "title": "{SITE_NAME}",
    "mediaInfos": [],
    "popupElements": [
        {
            "fieldInfos": [
                {
                    "fieldName": "OBJECTID",
                    "isEditable": False,
                    "format": {
                        "places": 0,
                        "digitSeparator": False
                    },
                    "visible": False,
                    "label": "OBJECTID"
                },
                {
                    "fieldName": "SITECODE",
                    "isEditable": True,
                    "visible": True,
                    "label": "SITECODE"
                },
                {
                    "fieldName": "SITE_NAME",
                    "isEditable": True,
                    "visible": True,
                    "label": "SITE_NAME"
                },
                {
                    "fieldName": "Shape__Length",
                    "isEditable": False,
                    "format": {
                        "places": 6,
                        "digitSeparator": False
                    },
                    "visible": False,
                    "label": "SHAPE__Length"
                },
                {
                    "fieldName": "Shape__Area",
                    "isEditable": False,
                    "format": {
                        "places": 6,
                        "digitSeparator": False
                    },
                    "visible": False,
                    "label": "SHAPE__Area"
                }
            ],
            "type": "fields"
        }
    ],
    "fieldInfos": [
        {
            "fieldName": "OBJECTID",
            "isEditable": False,
            "format": {
                "places": 0,
                "digitSeparator": False
            },
            "visible": False,
            "label": "OBJECTID"
        },
        {
            "fieldName": "SITECODE",
            "isEditable": True,
            "visible": True,
            "label": "SITECODE"
        },
        {
            "fieldName": "SITE_NAME",
            "isEditable": True,
            "visible": True,
            "label": "SITE_NAME"
        },
        {
            "fieldName": "Shape__Length",
            "isEditable": False,
            "format": {
                "places": 6,
                "digitSeparator": False
            },
            "visible": False,
            "label": "SHAPE__Length"
        },
        {
            "fieldName": "Shape__Area",
            "isEditable": False,
            "format": {
                "places": 6,
                "digitSeparator": False
            },
            "visible": False,
            "label": "SHAPE__Area"
        }
    ],
    "expressionInfos": []
}

## set the definition for the popup
fs_def["layers"][lyr_id]["popupInfo"] = popupInfo

## update the definition
fs_item.update({"text" : fs_def})

 

Try on a test feature service. You can also access le item definition from ArcGIS Online Assistant. Login, click on I want to... (at the top) and select View and Items JSON. Find the feature service on the left.

Please let us know if this works for you.

All the best,

Glen

~ learn.finaldraftmapping.com
sandra555
Regular Contributor

Hi Glen, Thanks for the info. I went to test and came across another issue. When I add a feature layer into my content, then go to my script and use the get_data(), it is empty. If I go into the console and make a change (i.e. hide one of my fields in the popup), then go back and run my script, the get_data() returns my layer information. Do you know a way to do in the script the same thing that happens when I manually make a change to the feature layer (so that the get_data() will work correctly)? Thanks, Sandra

0 Kudos
Clubdebambos
MVP Regular Contributor

Hi Sandra,

I'm not sure I follow (could be just a tired brain 🤕), could you send some screenshots of the workflow?

All the best,

Glen

~ learn.finaldraftmapping.com
0 Kudos