Adding friendly name to a table in perspective

I have a table in Perspective but I wanted to change the names to something friendly. I used Transform in the binding to add a script. The script works, but the property editor is telling me I have 12 invalid key’s;

def transform(self, value, quality, timestamp):

# If value is None, return empty list
    if not value:
        return []

    # Convert dataset to list of dictionaries
    py_data = []
    for i in range(value.rowCount):
        row_dict = {}
        for col in value.columnNames:
            row_dict[col] = value.getValueAt(i, col)
        py_data.append(row_dict)

    # Map to friendly column headers
    friendly_data = []
    for row in py_data:
        friendly_data.append({
        	"ID": row.get("id"),
            "Machine": row.get("machineId"),
            "Employee ID": row.get("employeeNumber"),
            "Work Center": row.get("workCenter"),
            "Job #": row.get("jobNumber"),
            "Bends": row.get("bendsPerPart"),
            "Pedal Pressed": row.get("pedalPressed"),
            "Total Hits": row.get("totalHits"),
            "Produced Hits": row.get("hitCounter")
        })

    return friendly_data

There is usually no need to do this.

Use the columns property of the table to do that. See below:

Once you point one column to a key in your data, you have access to the “header” property, where you can specify a more human-friendly text as the header of your column.

If you need more info about column configuration, see below:

You’ll have to define as many columns as you want data to be shown. In your script, you use 9 keys, that means you will have to define 9 columns.

3 Likes

You have spaces in your keys. Perspective will tolerate them but they're not valid JSON.

A useful tool for checking is

Copy the output of the binding and paste it into https://jsonlint.com and let it validate it.

Okay, I see. I looked at the columns and it confused me, but now I get it. I add (9) columns, 0-8, and then I can edit the appearance of each. Thank you for clearing that up.