Add UTF-8 BOM header to Flask-Admin export

A simple trick to add additional bytes prepending Flask's stream_with_context

from flask_admin.contrib.sqla import ModelView
from itertools import chain

class MyAdminView(ModelView):
    can_export = True
    column_export_list = ['field1', 'field2']

    def _export_csv(self, return_url):
        r = super(MyAdminView, self)._export_csv(return_url)
        r.response = chain((b'\xef\xbb\xbf',), r.response)
        return r

I am still using Python2 and Flask-admin shit. It's a shame but anyway.

Comments