I’m making a voting application in python/flask and I need to make a button that allows the user to download a mysql dump.
The mysql code that performs the dump is this –
select * into outfile "votes.csv" fields terminated by ',' from vote;
I have 2 questions
- What should be the path where this dump is saved so that the it can be downloaded?
- I’m using sqlalchemy. How can I execute this code from the flask application and provide a link to the file generated?
Thanks a lot!
For streaming?
@app.route('/foo')
def foo():
def generate():
yield 'first part'
yield 'second part'
return Response(generate(), direct_passthrough=True)
Okay so lets look into the problem.
You want to dump your votes into a csv file where the fields are terminated by ‘,’ from the vote table?
You can probably just directly iterate over the sqlalchemy table 🙂
Something like this.
Obviously it might be slightly different, but still point stands. Construct the csv in Python rather than getting mysql to dump it out.
Now as for where to save it, you have two choices, either write it to hard drive or store it in memory (depending on constraints)
If its written to hard drive just make sure its saved in the
/staticdirectory of your application, if it is in memory you can just stream it to the request directly.Another way could be to generate the file on the fly just writing it out to the request-er.