Writing table data

Hi
I'm trying to something that should be simple... but maybe it is me that is simple?
I would like to display a text file in a table so that I can modify the style of a row based on it's content. The table will have only one column, for the text line, and one row per line in the file.
The script that I currently have in the binding transform is

def transform(self, value, quality, timestamp):
	rows = []

	for line in value.splitlines():
		row = {
	        # I need code for correct formatting of the row data 
			#  here (including style (eg background color) here
                        # in json format?

			}
		rows.append(row)
	
	return rows

Thanks

This should give you a good starting point. Try working it out and then post back with what didn't work.

NOTE: There is at least one link to a old manual page, which will not work. You'll need to find it in the new manual.

1 Like

Hey try this.

row = {
            "text": line,
            "style": {
                #add color
            }

You can get a good flavour of how this should work by analysing the first row of the default data in the Table component. This has "Folsom" on an orange background. The JSON looks like this:

{
  "city": {
    "align": "center",
    "editable": true,
    "justify": "left",
    "style": {
      "backgroundColor": "#F7901D",
      "classes": "some-class"
    },
    "value": "Folsom"
  },
  "country": "United States",
  "population": 77271
}

If you want to do this well, then define a style in Project Browser | Perspective | Styles and use that in the style.classes property in the JSON above and remove the backgroundColor property.

Thank you all for response. I still haven't got it sorted
I trimmed out everything leaving hopefully just one column for the text data. My code is as follows:

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

	rows={}
	ctr=0
	for line in value.splitlines():	
		row = {
		  "data": {
		    "value": line
		  		}
			}		
		rows[ctr] = row		
		ctr+=1
	return rows

The data seems to get to the table:
image
The "value" correctly displays the lines of text. The 24 error messages say "Invalid key"
The order of the lines is odd, but thats another issue

That's returning a dictionary of dictionaries. You need a list (array) of dictionaries. Note that the key word "data" shouldn't appear in the list. The whole list is assigned to props.data instead.

The JSON should look like this.
[
  {
    "city": {
      "align": "center",
      "editable": true,
      "justify": "left",
      "style": {
        "backgroundColor": "#F7901D",
        "classes": "some-class"
      },
      "value": "Folsom"
    },
    "country": "United States",
    "population": 77271
  },
  {
    "city": "Helsinki",
    "country": "Finland",
    "population": 635591
  },
  {
    "city": "Jakarta",
    "country": "Indonesia",
    "population": 10187595
  }
]
The code should look something like this.
def transform(self, value, quality, timestamp):
    rows = []    # An empty LIST!
    for line in value.splitlines():	
	if row['col1'] > 15:
      rows.append({"col1": {
        "value": value['col1'],
        "style": {"classes": "myStyleClass"}
      }})
	else:
      rows.append({"col1": value['col1']})
	return rows

I haven't tested this. I've used spaces for indentation on the forum. You might want to convert to tabs.

The order of the lines is odd, but that's another issue.

That's because you returned a dictionary and dictionaries in Jython are unordered. Returning as a list solves this problem, maintains the order and is the format the Table component expects.

1 Like

That's it
Thank you for amplifying

I'm glad to help.

I forgot to repeat my suggestion that you define a style for the highlighting rather than hard code it in your script. That way it's reusable throughout your application and easy to modify, if required. Modify "myStyleClass" to suit your style name.