[SOLVED] Download filtered Perspective table component data

Good morning,

I have a Perspective button that executes this onActionPerformed event which takes data from a perspective table and outputs a CSV file to the local download folder.

ds = self.parent.parent.getChild("tbl_Data").props.data
csv = system.dataset.toCSV(dataset = ds, showHeaders = True, forExport = False)
system.perspective.download("test.csv", csv)

This code works fine. However, when I filter the results using the built in table filter the above code returns all the dataset still. So I changed the code below to only look at the filter results:

ds = self.parent.parent.getChild("tbl_Data").props.filter.results.data
csv = system.dataset.toCSV(dataset = ds, showHeaders = True, forExport = False)
system.perspective.download("test.csv", csv)

However, this new code is returning an empty file:

image

Is this a bug? Or am I looking at the wrong place for the data? Or am I not going about this process correctly?

Any advice is appreciated.

This isn't a dataset anymore, even if the input is. It's a JSON-array-like structure. You need to normalize it back into a dataset yourself.

1 Like

Ok.

First issue was I had to enable the filter in the settings which allowed the results to be stored in the results below.

image

And then I had to convert the JSON-array-like structure (that could totally just be a dataset but I'm not complaining...) using the following code:

import ast

ds = str(self.parent.parent.getChild("tbl_Data").props.filter.results.data)

# Extract the content within "<ArrayWrapper>: [...]"
data_string = ds[ds.index("["):].replace("u'", "'")

# Parse the string into a list of dictionaries
data_list = ast.literal_eval(data_string)

# Extract the headers
headers = list(data_list[0].keys())

# Extract the data rows
data = [list(d.values()) for d in data_list]

ds = system.dataset.toDataSet(headers, data)
csv = system.dataset.toCSV(dataset = ds, showHeaders = True, forExport = False)

system.perspective.download("test.csv", csv)

Thank you for the help.

2 Likes

I have Chinese in column 3, how to encode it?
Caused by: org.python.core.PyException: TypeError: Unable to convert row 1, column 3 to type class java.lang.Integer