LoginSignup
19
34

More than 5 years have passed since last update.

Anacondaインストール後のJupyterの設定: ブラウザとパスワードの設定・nbextensionsとその他拡張機能の追加・設定

Last updated at Posted at 2017-04-08

特にハマりどころもないですが一応。

インストール後すぐ起動しようとしてもエラーになった

OS: macOS Sierra 10.12.4
AnacondaとJupyterのパスとバージョン

~ ❯❯❯ which anaconda                                                          
/Users/User/anaconda/bin/anaconda

~ ❯❯❯ anaconda --version
anaconda Command line client (version 1.6.0)

~ ❯❯❯ which jupyter
/Users/User/anaconda/bin/jupyter

~ ❯❯❯ jupyter --version
4.2.1

anaconda インストール後、 jupyter notebook と打ったら、

~ ❯❯❯ jupyter notebook
[W 17:31:38.060 NotebookApp] server_extensions is deprecated, use nbserver_extensions
[I 17:31:38.898 NotebookApp] [nb_conda_kernels] enabled, 3 kernels found
[I 17:31:38.916 NotebookApp] Writing notebook server cookie secret to Users/User/Library/Jupyter/runtime/notebook_cookie_secret
Traceback (most recent call last)
...
...
PermissionError: [Errno 13] Permission denied: 'Users/User/Library/Jupyter/
~ ❯❯❯ ls -l ~/Library
...
drwx------+   2 User     staff     68 Mar 30  2016 Internet Plug-Ins
drwx------    5 root     staff    170 Apr  8 17:15 Jupyter
drwxr-xr-x    3 User     staff    102 Mar 29 17:17 Keyboard
...

Jupyter配下のものを全て rootからUserに権限を移してあげる必要あり。

~ ❯❯❯ sudo chown -R User ~/Library/Jupyter

権限を変えずにsudo jupyter notebook --allow-rootでも起動できるが、根本的な解決にならないし、セキュリティ上の理由などから推奨されていない。

~ ❯❯❯ jupyter notebook

Jupyterを止めて(Ctrl + Cを2回)、今度は設定。

ブラウザとパスワードの設定

  • デフォルトのブラウザ

まずjupyterコマンドで設定ファイルを生成してあげて

~ ❯❯❯ jupyter notebook --generate-config
Writing default config to: /Users/User/.jupyter/jupyter_notebook_config.py
~ ❯❯❯ vim /Users/User/.jupyter/jupyter_notebook_config.py

設定ファイルの中でデフォルトのブラウザをChromeに設定。

c.NotebookApp.browser = 'chrome'

設定したいブラウザ名がわからない場合、REPLですぐ確認できる。

~ ❯❯❯ ipython
In [1]: import webbrowser

In [2]: webbrowser._browsers
Out[2]:
{'chrome': [None, <webbrowser.MacOSXOSAScript at 0x105545208>],
 'firefox': [None, <webbrowser.MacOSXOSAScript at 0x1055452e8>],
 'macosx': [None, <webbrowser.MacOSXOSAScript at 0x105545358>],
 'open': [None, <webbrowser.GenericBrowser at 0x1055453c8>],
 'safari': [None, <webbrowser.MacOSXOSAScript at 0x10552c908>]}
  • パスワードの設定

jupyter5.0からは、jupyter notebook passwordと叩けば設定できるが、今回はバージョンが違うので、こう。

~ ❯❯❯ python -c 'from notebook.auth import passwd;print(passwd())'              
Enter password:
Verify password:
sha1:123456789012:1234567890abcdef1234567890abcdef12345678

出力されたハッシュ(sha1...678の文字列)を先ほどの設定ファイルに追記する。

c.NotebookApp.password = `sha1:123456789012:1234567890abcdef1234567890abcdef12345678`

pythonipythonなどのREPLから from notebook.auth import passwd ... みたいな感じで設定することもできるが、passwd(<ここ>) ここにパスワードを直接入れなければならず、コマンドの履歴が残ってしまうため、よろしくない。

拡張機能の追加

より便利に使うために、Jupyter notebook extensionsを入れて拡張機能を設定できるようにする。Anacondaが入っているので、以下のコマンドで拡張機能たちがワラっと入り、かつそれらに必要なJSやCSSも一緒に入る。

~ ❯❯❯ conda install -c conda-forge jupyter_contrib_nbextensions

Jupyter notebook extensionsの中にもデフォルトでVim用のKeymapの拡張機能が用意されているが、ノーマルモードで直接セル間の移動ができなかったりして(もう一段階エスケープしないといけない)、個人的には操作しづらいので、代わりにこれを利用する。

先程入れた拡張機能は~/anaconda/share/jupyter/nbextensions/配下などに入っているはずなので、jupyter-vim-bindingもそこに追加する。

もし拡張機能たちがどこに入ったのかわからなければ、

~ ❯❯❯ find ~/anaconda -name nbextensions

Anacondaで仮想環境を作って、その中にJupyterを入れているなら、
Users/User/anaconda/envs/env/share/jupyter/nbextensions みたいなフォルダがあるかもしれない。

jupyter-vim-bindingをクローンしてきて、有効にする。

~ ❯❯❯ cd ~/anaconda/share/jupyter/nbextensions && git clone https://github.com/lambdalisue/jupyter-vim-binding vim_binding

拡張機能の設定

jupyter notebookで起動して、ブラウザで Nbextensions のタブをクリックするか、localhost:8888/nbextensions(ポートは自分の環境のものを。デフォルトは8888のはず。)へ行って、どんな拡張機能があって、何が便利そうか観ながらチェックボックスをポチポチする。

vim_bindingについては、上のほうのdisable configuration for nbextensions without explicit compatibility...のチェックボックスを外し、VIM bindingをチェックする。

もしかしたら、vim binding と VIM binding の両方がチェックボックスの項目として表示されてしまっているかもしれないが、VIM bindingのほうが正しい。名前が紛らわしかったり、どちらが目的の機能かわからないときは、各拡張機能はみんなその配下にyamlファイルを持っているので、そこのNameを見ればよい。

~ ❯❯❯ cat ~/anaconda/share/jupyter/nbextensions/vim_binding/vim_binding.yaml

参考

Running a notebook server
http://jupyter-notebook.readthedocs.io/en/latest/public_server.html
Jupyter notebook extensions
https://github.com/ipython-contrib/jupyter_contrib_nbextensions
jupyter-vim-binding
https://github.com/lambdalisue/jupyter-vim-binding/issues

19
34
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
19
34