Created
November 24, 2016 03:52
-
-
Save kurozumi/22d3eefdea3e8cf30113017e2d343d2e to your computer and use it in GitHub Desktop.
【Python】Bottleを使ってCSVダウンロード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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