Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
【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
You can’t perform that action at this time.