# -----------------------------------------------------
log = console.log
def export_data (items) :
'export data as a file'
# ----- convert items to string
result = window.JSON.stringify (items)
log (f'result = { result }')
# ----- format timestamp, you can skip this part
date = window.Date.new ()
datestr = date.toISOString () # '2021-10-12T20:15:17.948Z'
stamp = datestr
stamp = stamp.split ('.', 1) [0] # drop ms
stamp = stamp.replace (':', '_')
stamp = stamp.replace ('T', '.') # '2021-10-12.20_15_17'
# ----- save data as file
# **** chrome btoa wont handle non-ascii data.
# need to find another btoa solution. js lib?
# doesnt work, zero output
#blob = window.Blob.new ( window.Uint8Array.new (result) )
# doesnt work, zero output
#blob = window.Blob.new (['\ufeff' + result] , { 'type' : 'text/csv;charset=utf-8'} )
# doesnt work, zero output
#blob = window.Blob.new ( window.Uint8Array.new (result) )
# finally, this **** works!
blob = window.Blob.new ( [result] )
url = window.URL.createObjectURL (blob)
# url is now a working link to download the data as a file
# my code runs in a chrome extension. I use chrome.downloads to trigger the download without any dialogs
# if android has similar functionality you can use that instead
# or trigger the download with normal js (with normal save dialogs)
# or insert the url in a new link / control and have the user launch it manually
chrome.downloads.download ({
'url' : url ,
'filename' : f'downloads.{ stamp }.json'
})
--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/2e35cf8d-3b02-4abc-9226-3a2592c64f20n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/e7069412-9ec6-4a40-a9b1-8e6103009ef1n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/f8b46224-4039-47e9-8d45-e78003338a34n%40googlegroups.com.