Convert Array of objects to Dataset for export to CSV

Hi all!

I've got a table in perspective that I use an export function to spit out to CSV (code below).

This is great for dumping the whole dataset, but doesn't give me the current data view.
I thought I could get around this by instead enabling and copying the 'Results' data set when a filter is applied. The problem I'm having with this 'hack', is I first need to convert this into a dataset to be able to export it.
When using system.xxx.toDataset on the array, it gives me a 5X1 dataset where the objects are strings and not columns.

Can anyone please advise how I correctly convert this structure to a dataset? And/or if there's a better way of exporting the current table data view to CSV?

Thanks!
Alex

# Get the table component to retrieve the dataset from
table = self.parent.parent.getChild("TableOfThings")
	
# Create a CSV from the dataset
csvData = system.dataset.toCSV(dataset = table.props.data, showHeaders = True, forExport = False)

# Prompt the browser to download the file
system.perspective.download(fileName, csvData)

This is far from a hack, it's pretty much what it's for.

I'm guessing you're passing the wrong thing to toDataset, but without the code all we can do is guess.
This should make a proper dataset:

data = self.parent.parent.getChild("TableOfThings")
headers = data[0].keys()
rows = [row.values() for row in data]
ds = system.dataset.toDataSet(headers, rows)
return ds

edit : one more thing... Be aware that saving results may have a performance cost.
I used to do this with alarm tables and whatnot. I stopped because of performance issues.
Now, for csv exports, I actually fetch the filters configuration from the table itself, then apply them in a queryStatus or whatever the function is that corresponds to the concerned table.
The export itself might take longer than before, because I'm doing a query just for it, but the loading of the table itself is much smoother, and exports are actually quite rare.

4 Likes