LoginSignup
2
4

More than 3 years have passed since last update.

Dockerで起動したJupyter Notebookでvimキーバインドを使う

Last updated at Posted at 2020-02-16

概要

最近はデータサイエンスの勉強で統計学の本を読んでいるのですが、本を読んでいるだけだとなかなかイメージしにくいこともあるので、Jupyter Notebookを使って手元でコードを書きながらやりたいなと思いました。
Jupyter Notebookでコード書くならvimのキーバインドが使えないとテンションが上がらないのでvimが使えるようにしたいと思いましたが、多少ハマった部分などあったのでやり方をまとめました。

環境

バージョン
Mac 10.15.3
Docker 19.03.4
docker-compose 1.24.1

環境構築手順

Dockerfileとdocker-composeを使ってvimのキーバインドが使えるJupyter Notebookを起動します。
※以下ファイルはGitHubにもまとめているのでご参考ください。
https://github.com/hikarut/Jupyter-Notebook

1. notebook保存用のディレクトリ作成
$ mkdir notebooks
2. Dockerfileの作成
Dockerfile
FROM jupyter/minimal-notebook:latest

USER root

RUN pip install jupyter_contrib_nbextensions && \
    jupyter contrib nbextension install --user && \
    git clone https://github.com/lambdalisue/jupyter-vim-binding /home/jovyan/.local/share/jupyter/nbextensions/vim_binding && \
    jupyter nbextension enable vim_binding/vim_binding

EXPOSE 10000
CMD ["bash"]

jupyter-vim-bindingレポジトリのクローン先はコマンドjupyterのpathと合わせる必要があります。(jupyter --pathコマンドで確認出来ます)

3. docker-compose.ymlの作成
docker-compose.yml
version: '3'
services:
  data-science:
    restart: always
    build: .
    container_name: 'data-science'
    ports:
      - "10000:10000"
    working_dir: '/root/'
    tty: true
    volumes:
      - ./notebooks:/root/notebooks/
4. コンテナのビルド
$ docker-compose up -d --build
5. コンテナにログイン
$ docker-compose exec data-science bash
6. Jupyter Notebookの起動
/root# jupyter notebook --port 10000 --allow-root

表示されるhttp://127.0.0.1:10000/?token=xxxxxxxxxxxxxxxxにアクセスします。

7. VIM bindingのエクステンションを有効にする

デフォルトだとエクステンションの設定にdisableにチェックが入っているのでチェックを外して有効にします。
スクリーンショット 2020-02-16 9.41.55.png

これでnotebook上でvimが使えるようになります
スクリーンショット 2020-02-16 9.47.41.png

最後に

Jupyter Notebookでvimのキーバインドを使えるようにするやり方は結構まとまっていたのですが、色々試していたら以外とハマった部分もあったので自分なりにまとめてみました。
これで快適なNotebookが書けそうです!
(ただ今回はユーザーを全てrootにしちゃったのでその辺は変えた方が良いかもです。。)

参考
2
4
0

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
2
4