Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Created November 24, 2016 03:52
Show Gist options
  • Save kurozumi/22d3eefdea3e8cf30113017e2d343d2e to your computer and use it in GitHub Desktop.
Save kurozumi/22d3eefdea3e8cf30113017e2d343d2e to your computer and use it in GitHub Desktop.
【Python】Bottleを使ってCSVダウンロード
# coding: utf-8
import csv
from io import StringIO
from bottle import route, response, run
@route("/download")
def download():
"""
CSVファイルを作成してダウンロード
"""
stream = StringIO()
writer = csv.writer(stream)
writer.writerow(["str", "string"])
writer.writerow(["int", 123])
# コンテンツタイプにapplication/octet-streamを指定
response.content_type = "application/octet-stream"
# ダウンロードするファイル名を指定
response.headers["Content-Disposition"] = "attachment; filename='test.csv'"
return stream.getvalue().encode("shift-jis")
if __name__ == "__main__":
run(host="localhost", port=8080)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment