Exodeus
Exodeus•2y ago

Download csv file from python script + app

Hello, I'm trying to download a csv file created in a python script launched from an app, but I can't find a functional solution for the whole thing 😦 No matter how much I encode my raw data in base64, return it, or store it in a state for download from a download button, I never get a correct csv file 😦 Do you have any ideas for me? šŸ™‚ Thanks šŸ™‚
5 Replies
rubenf
rubenf•2y ago
Hi, can you describe what you have tried and what do you get please
Exodeus
ExodeusOP•2y ago
I have an app, which gets an external service. I retrieve a list of data that I reformat and display in a Rich Result. I would like to be able to download this data in csv format. So I have a little script to format it in csv via python:
import csv
import base64

def main(data: list):
file_path = 'file.csv'
if not data:
raise ValueError("No data")
with open(file_path, mode='w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=data[0].keys())
writer.writeheader()
writer.writerows(data)

with open(file_path, 'rb') as file:
return base64.b64encode(file.read()).decode()
import csv
import base64

def main(data: list):
file_path = 'file.csv'
if not data:
raise ValueError("No data")
with open(file_path, mode='w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=data[0].keys())
writer.writeheader()
writer.writerows(data)

with open(file_path, 'rb') as file:
return base64.b64encode(file.read()).decode()
Whether I return this data in base64 format directly, or whether I store it in a variable to use a Download Button, I cannot recover my formatted data in csv. The best I got was an html file....
rubenf
rubenf•2y ago
Ok it's super confusing how it's done right now but you need to prefix to your base64: data:application/pdf;base64, change application/pdf; for the correct mimetype we will fix it to be able to take in raw base64 @Exodeus i've done a fix on very latest so that it will not be necessary anymore will deploy on cloud in 10mins, and should be available to be pulled
Exodeus
ExodeusOP•2y ago
Whoa! What responsiveness! I’m in awe haha! I see with our service si for pulled šŸ™‚
rubenf
rubenf•2y ago
it was a very small typo in one of our check to transform automatically base64

Did you find this page helpful?