LoginSignup
17

More than 5 years have passed since last update.

JupyterのDockerイメージで日本語が使えるように、Dockerfileを書いた

Last updated at Posted at 2018-08-14

Pythonを書くようになり、Jupyterを使って遊んでいます。matplotlibで遊んでいたのですが、日本語が文字化けする現象がおきました。

野球選手の名前を出したかったんだけど.png

Jupyterを日本語化する方法はぐぐるとそれなりに数が出てきます。しかし、それらの記事は、JupyterのDockerイメージ環境とはキャッシュの場所が違うなどして、若干手間取りました。Dockerfileを書いて、いつでも日本語化できるようにしました。

参考サイト

最初に参考記事をご紹介します。感謝感謝 :bow:

作成したdockerfile

Dockerfileを見れば、何をしたかすぐに分かるのでひとまず読んで

FROM jupyter/datascience-notebook

LABEL  maintainer "nassy20 <xxxx@gmail.com>"

RUN curl -L  "https://oscdl.ipa.go.jp/IPAexfont/ipaexg00301.zip" > font.zip
RUN unzip font.zip
RUN cp ipaexg00301/ipaexg.ttf /opt/conda/lib/python3.6/site-packages/matplotlib/mpl-data/fonts/ttf/ipaexg.ttf
RUN echo "font.family : IPAexGothic" >>  /opt/conda/lib/python3.6/site-packages/matplotlib/mpl-data/matplotlibrc
RUN rm -r ./.cache

使い方

上記Dockerfileを置いたdirで以下のdockerコマンドを実行してください。

docker build -t jupyter-japanese:0.1 .
docker run -it --rm --name notebook -p 8888:8888 jupyter-japanese:0.1

すると、以下がコンソールに流れてくるので指示に従って、tokenも含めてブラウザで実行しましょう。

 Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:
        http://(4b42f5846e22 or 127.0.0.1):8888/?token=94ab1e2dd5ce91140e302af4e8565ebc9266465b1cf17994

スクリーンショット 2018-08-14 21.49.27.jpg

問題無くjupyterのホームが表示されました。
右側のNewからpython3を選択し、以下を実行させましょう。

import matplotlib
import matplotlib.pyplot as plt
plt.figure()
plt.xlabel('豆腐 - tofu')
plt.title('豆腐 - TOFU')

スクリーンショット 2018-08-14 21.50.22.jpg

日本語表示成功です :kissing_heart:

(plt.figure()の2回目でグラフが表示される理由は分かりません :bow: )

ついでに、甲子園の各都道府県の代表校のデータ可視化してみました。(やるー:relaxed:)

20180805101300.png

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
17