system.dataset.toCSV() requires a dataset.
# root > table-flex > body > table
table = self.getChild("Table Flex").getChild("Body").getChild("Table")
# create a list of columns that are selected by user
colsToKeep = [col["field"] for col in table.props.columns if col["visible"]]
data=[]
for el in table.props.data:
rowData = [v["value"] for _,v in el.items() if _ in colsToKeep]
data.append(rowData)
newCsv = system.dataset.toCSV(system.dataset.toDataSet(colsToKeep, data)
system.perspective.download("Export.csv",newCsv)