1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Redash v10でs-jisでcsvをダウンロードする方法

Posted at

かつての方法

python2時代のredashは.envに↓を書けば、shift-jisでcsvがダウンロードできました。

export REDASH_CSV_WRITER_ENCODING="cp932" 

しかし、v9からpython3になりUTF-8が標準になったことから、CSVのエクスポートに上記の設定を使っているutils/__init__.pyclass UnicodeWriterを使用しなくなってしまいました(=上記設定は使われず、cp932でCSVはエクスポートできない。)

v9から

コードを見た感じではredash/handlers/query_results.pyでcsvのあれこれをしています。この為、仕方なくソースコードを変更することにしました。
https://github.com/getredash/redash/blob/2589bef1f21b7c84e4ceacdf5cd2c620bdedb111/redash/handlers/query_results.py#L436

    @staticmethod
    def make_csv_response(query_result):
        headers = {"Content-Type": "text/csv; charset=UTF-8"}
        return make_response(
            serialize_query_result_to_dsv(query_result, ","), 200, headers
        )

これを、

    @staticmethod
    def make_csv_response(query_result):
        headers = {"Content-Type": "text/csv; charset=Shift_JIS"}
        return make_response(
            serialize_query_result_to_dsv(query_result, ",").encode("cp932"), 200, headers
        )

に変更すればshift-jisでcsvがダウンロードされます。utf-8 bom付きにしたい場合は、↓のようにします。

serialize_query_result_to_dsv(query_result, ",").encode("utf_8_sig"), 200, headers

dockerに対応させる

上記を変更すればshift-jisに対応できるのですが、ちょっと前からセルフホストはdockerが標準になっているため、コンテナを停止等すると変更内容が元に戻ってしまいます。その為、下記のようにホスト側に変更したファイルを置き、volume接続することにしました。

ただ、この方法はredashのバージョンを上げるたびに再設定する必要があります。該当箇所をsed等で書き換えるDockerfile等を書いて、docker-compose buildし直すのも良いかもしれません。

変更したファイルをpatch/query_results.pyにおいた場合。

docker-compose.yml

services:
  server:
    <<: *redash-service
    command: server
    ports:
      - "5000:5000"
    volumes:
      - ./patch/query_results.py:/app/redash/handlers/query_results.py:ro
    ....

これでdocker-compose rm serverdocker-compose up -d serverすると、変更したファイルが使用されます。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?