def sendCsv(iterable, filename, fields=None, schema=None, delimiter=',', encoding='utf-8', writer_kwargs=None, **kwargs): buf = StringIO() writer_cls = ValidatedWriter if schema: writer_cls = ValidatedWriter.from_schema(schema) writer_kwargs = writer_kwargs or {} writer = writer_cls(buf, fields, delimiter=delimiter, **writer_kwargs) writer.writeheader() for i in range(len(iterable)): for line in iterable[i]: writer.writerow(line) buf.seek(0) buf = BytesIO(buf.read().encode(encoding)) mimetype = 'Content-Type: text/csv; charset=' + encoding return send_file(buf, attachment_filename=filename, as_attachment=True, mimetype=mimetype, **kwargs)
def send_csv(cls, iterable, filename, fields=None, schema=None, delimiter=',', encoding='utf-8', writer_kwargs=None, **kwargs): buf = StringIO() # BOM (optional...Excel needs it to open UTF-8 file properly) buf.write('\ufeff') writer_cls = ValidatedWriter if schema: writer_cls = ValidatedWriter.from_schema(schema) writer_kwargs = writer_kwargs or {} writer = writer_cls(buf, fields, delimiter=delimiter, **writer_kwargs) writer.writeheader() for line in iterable: writer.writerow(line) buf.seek(0) buf = BytesIO(buf.read().encode(encoding)) mimetype = 'Content-Type: text/csv; charset=' + encoding return send_file(buf, attachment_filename=filename, as_attachment=True, mimetype=mimetype, **kwargs)
def write_csv(iterable, filename, fields=None, schema=None, delimiter=',', encoding='utf-8', writer_kwargs=None, **kwargs): buf = StringIO() writer_cls = ValidatedWriter if schema: writer_cls = ValidatedWriter.from_schema(schema) writer_kwargs = writer_kwargs or {} writer = writer_cls(buf, fields, delimiter=delimiter, **writer_kwargs) writer.writeheader() for line in iterable: writer.writerow(line) return writer
def summarize(): report = Report() if request.method == 'POST': start = request.form['start'] end = request.form['end'] paging, data_list = report.summary(start=start, end=end) # https://github.com/Shir0kamii/Flask-CSV if data_list: encoding = 'utf-8-sig' filename = start + '_' + end + '.csv' buf = StringIO() writer = ValidatedWriter(buf, fieldnames=data_list[0].keys()) writer.writeheader() for data in data_list: writer.writerow(data) buf.seek(0) buf = BytesIO(buf.read().encode(encoding)) return send_file(buf, attachment_filename=filename, as_attachment=True, mimetype='text/csv') form = PeriodSubmitForm() page, _, start, end = request_get(request.args) paging, data_list = report.summary(page=page, start=start, end=end) return render_template('report/summary.html', **locals())
def attend(): report = Report() if request.method == 'POST': start = request.form['start'] end = request.form['end'] name = request.form['name'] data_list = report.attend(page='all', name=name, start=start, end=end) if data_list: encoding = 'utf-8-sig' filename = start + '_' + end + '.csv' buf = StringIO() writer = ValidatedWriter(buf, fieldnames=data_list[0].keys()) writer.writeheader() for data in data_list: writer.writerow(data) buf.seek(0) buf = BytesIO(buf.read().encode(encoding)) return send_file(buf, attachment_filename=filename, as_attachment=True, mimetype='text/csv') # https://gist.github.com/doobeh/3e685ef25fac7d03ded7#file-vort-html-L11 form = PeriodSubmitForm() page, name, start, end = request_get(request.args) paging, today, data_list, summary = report.attend(page=page, name=name, start=start, end=end) return render_template('report/attendance.html', **locals())
def send_csv(iterable, filename, fields=None, schema=None, delimiter=',', **kwargs): buf = StringIO() writer_cls = ValidatedWriter if schema: writer_cls = ValidatedWriter.from_schema(schema) writer = writer_cls(buf, fields, delimiter=delimiter) writer.writeheader() for line in iterable: writer.writerow(line) buf.seek(0) buf = BytesIO(buf.read().encode("utf-8")) return send_file(buf, attachment_filename=filename, as_attachment=True, **kwargs)