Flask button to save table from query as csv

Deadly 提交于 2020-04-11 12:21:52

问题


I have a flask app that runs a query and returns a table. I would like to provide a button on the page so the user can export the data as a csv.

The problem is that the query is generated dynamically based on form input.

@app.route('/report/<int:account_id>', methods=['GET'])
def report(account_id):
    if request == 'GET':
        c = g.db.cursor()
        c.execute('SELECT * FROM TABLE WHERE account_id = :account_id', account_id=account_id)
        entries = [dict(title=row[0], text=row[1]) for row in c.fetchall()]
        return render_template('show_results.html', entries=entries)

On the html side it's just a simple table, looping over the rows and rendering them. I'm using bootstrap for styling, and included a tablesorter jquery plugin. None of this is really consequential. I did try one javascript exporter I found, but since my content is rendered dynamically, it saves a blank CSV.

Do I need to do some ajax-style trickery to grab a csv object from the route?


回答1:


I solved this myself. For anyone who comes across this I find it valuable for the specific use case within flask. Here's what I did.

import cx_Oracle      # We are an Oracle shop, and this changes some things
import csv
import StringIO       # allows you to store response object in memory instead of on disk
from flask import Flask, make_response # Necessary imports, should be obvious

@app.route('/export/<int:identifier>', methods=['GET'])
def export(load_file_id):
    si = StringIO.StringIO()
    cw = csv.writer(si)
    c = g.db.cursor()
    c.execute('SELECT * FROM TABLE WHERE column_val = :identifier', identifier=identifier)
    rows = c.fetchall()
    cw.writerow([i[0] for i in c.description])
    cw.writerows(rows)
    response = make_response(si.getvalue())
    response.headers['Content-Disposition'] = 'attachment; filename=report.csv'
    response.headers["Content-type"] = "text/csv"
    return response


来源:https://stackoverflow.com/questions/33766499/flask-button-to-save-table-from-query-as-csv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!