toCSV is Downloading as a .txt file

I know that I'm doing something incorrectly, as I'm not amazingly practiced with Python and with Perspective scripting, so I apologize if it's something really obvious. I'm running some tests for a download button on a table component. The button works, technically! However, it is downloading as a *.txt file rather than a *.csv. I figure I've done something in the script...but I can't really pinpoint where.

def runAction(self, event):
	tableData = self.getSibling('TestTable')
	
	csv = system.dataset.toCSV(tableData.props.data)
	
	system.perspective.download('tableData', csv)

Well, CSV is a text format, so autodetection will do that. Supply a content type to system.perspective.download.

To add to Phil's response, here is the modification you need to make.

system.perspective.download('tableData', csv, 'text/csv')
1 Like

Thank you! Another issue I'm having is that the t_stamp isn't exporting correctly? Or it is, and I haven't formatted it correctly. It's just showing the time, but not the date itself, however the date is shown in the perspective table.

Did you open the CSV in Excel, or in a text editor? If not a text editor, use a text editor to see what was really exported.

It was Excel. the text editor is showing the correct timestamp-- eg. "2024-03-03 00:00:00" instead of just "00:00:00"

Any clue how to format the csv so it shows the actual tstamp? It's obviously being exported in some way, because it's showing it when I view it in Notepad. Just not Excel.

That's an Excel issue. When opening most CSVs in Excel, timestamp isn't reliably shown.
If this is going to always be opened in Excel, then use the system.dataset.toExcel function to get a reliable spreadsheet.

Otherwise, you will need to open the file in Excel and then manually format the column as a DateTime. Just know that column formatting isn't part of a CSV, so that won't be saved with the file.

1 Like

Consider converting to Excel directly, instead of to CSV. That CSV opens in Excel by default is utter bogus, IMNSHO.

2 Likes