LoginSignup
2
5

More than 3 years have passed since last update.

[MEMO][開発環境構築] JupyterNotebook

Last updated at Posted at 2020-02-03

はじめに

もっと詳しく知りたい方は
こちらを参考にしてください!

急いでる時用

以下、インストールから有効化まで行います。

基本

  • jupyter
  • nbextensions
  • jupyterthems
  • vim-binds

scraping用

  • wheel
  • pandas
  • bs4
  • selenium
  • requests
  • googlestrans
  • emoji
pip install -U pip && pip install wheel pandas bs4 selenium requests googletrans emoji jupyter jupyter_contrib_nbextensions jupyterthemes  && jupyter nbextensions_configurator enable --user  && mkdir -p $(jupyter --data-dir)/nbextensions  && cd $(jupyter --data-dir)/nbextensions && git clone https://github.com/lambdalisue/jupyter-vim-binding vim_binding  && chmod -R go-w vim_binding  && jupyter nbextension enable vim_binding/vim_binding 

jupyter インストール

pip install jupyter

pipリストのバックアップと再インストール

  1. freezeでpipのインストールリストを出力して、pip_list_backup.txt(ファイル名は何でも良い)に出力する
  2. pip install -r コマンドでバックアップファイルを指定してインストール
バックアップ
pip freeze > pip_list_backup.txt
pip install -r pip_list_backup.txt

自動読み込み

importしているモジュールを自動再読込み

セルに入力してshift+enter
%load_ext autoreload
%autoreload 2

セルの幅を最大にする

pandasのDataFrameを見る時に、セル幅がデフォルトだと文字が見切れてしまうので、セル幅を最大になるように設定する

~/.jupyter/custom/custom.css
.container { width:100% !important; }

jupyter-nbextension

拡張機能を使えるようにする

pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

table of content

markdownのリストを表示

設定

Nbextensionタブ
toc2
Perameter
Display toc window/sidebar at startupをチェック

jupyter-themes

pip install jupyterthemes

jupyterthemesにfontを追加

~/usr/share/fontsからお好みのfontをとってきてjupyterthemesのインストールディレクトリ内のにfonts/monospace等でfont名と同じフォルダを作成する(python/site-packages/jupyterthemes/fonts/monospace/フォント名のフォルダ)
このフォルダないにfontを入れる.

jupyterthemes
   ├── fonts
   │   ├── monospace
   │   │   ├── migu
   |   |   |   ├── migu.ttf

あとはpython/site-packages/jupyterthemes/stylefx.pyに追加したいfontについて記述する.

python/site-packages/jupyterthemes/fonts/monospace/stylefx.py

def stored_font_dicts(fontcode, get_all=False):
    fonts = {'mono':
            {'anka': ['Anka/Coder', 'anka-coder'],
            ........
            ........
            'ubuntu': ['Ubuntu Mono', 'ubuntu'],
            'migu': ['Migu 1M', 'migu']}, # 新しく追加したfont
            'sans':
            {'droidsans': ['Droid Sans', 'droidsans'],
            ........
            ........
themeを設定
# ダーク系
jt -t monokai -cellw 100% -fs 15 -T -f migu

# ライト系
jt -t grade3 -fs 95 -altp -tfs 11 -nfs 115 -cellw 88% -T

図の背景色 (ダークテーマ限定)

図の背景色とテーマの色が被る不具合があるので背景色を指定し直す

下記のファイルに追記する

~/.jupyter/custom.css
div.output_area img, div.output_area svg {
 background: #fff;
}

commentカラー

~/.jupyter/custom.css
.edit_mode .cell.selected .CodeMirror-focused.cm-fat-cursor {
 /* ダーク系のテーマのとき*/
 /* background-color: #282828; */

 /* ライト系のテーマのとき*/
 background-color: #fffae6;
 color: #f8f8f0;
}

.edit_mode .cell.selected .CodeMirror-focused:not(.cm-fat-cursor) {
 /* ダーク系のテーマのとき*/
 /* background-color: #282828; */

 /* ライト系のテーマのとき*/
 background-color: #fffae6;
 color: #f8f8f0;
}

Vim

1. jupyter_contrib_nbextensionsを入れる

pip install jupyter_contrib_nbextensions

2. nbextensionsを有効にする

jupyter nbextensions_configurator enable --user

3. jupyterのdataディレクトリにnbextensionsフォルダをつくる

mkdir -p $(jupyter --data-dir)/nbextensions

4. nbextensionsフォルダにvim_bindingをcloneする

gitがなければインストールする。

cd $(jupyter --data-dir)/nbextensions
git clone https://github.com/lambdalisue/jupyter-vim-binding vim_binding

5. vim_bindingに権限を付与する

chmod -R go-w vim_binding

6. vimを有効にする

jupyter nbextension enable vim_binding/vim_binding
OKになれば導入成功
Enabling notebook extension vim_binding/vim_binding...
      - Validating: OK

7. themeがダーク系であれば背景色を変える

monokaiの場合 #272822とかにするといい感じになる。

~/.local/share/jupyter/nbextensions/vim_binding/vim_binding.css
...
...
...
.edit_mode .cell.selected .CodeMirror-focused.cm-fat-cursor {
  background-color: #272822;
}
.edit_mode .cell.selected .CodeMirror-focused:not(.cm-fat-cursor) {
  background-color: #272822;
}

やっぱ設定しなくていいのかも???????

キーマップ

~/.jupyter/custom/custom.js
// Configure CodeMirror Keymap
require([
  'nbextensions/vim_binding/vim_binding',   // depends your installation
], function() {
  // Map jj to <Esc>
  CodeMirror.Vim.map("jj", "<Esc>", "insert");
  // Swap j/k and gj/gk (Note that <Plug> mappings)
  CodeMirror.Vim.map("j", "<Plug>(vim-binding-gj)", "normal");
  CodeMirror.Vim.map("k", "<Plug>(vim-binding-gk)", "normal");
  CodeMirror.Vim.map("gj", "<Plug>(vim-binding-j)", "normal");
  CodeMirror.Vim.map("gk", "<Plug>(vim-binding-k)", "normal");
});

jupyter-vim-bindingのリンク
https://github.com/lambdalisue/jupyter-vim-binding#customization

keymapについて
https://codemirror.net/keymap/vim.js

ショートカット

最低限覚えておく&設定するショートカット
必要以上に覚えるとVimのコマンドど重なるので拘らない

前提

  • vim-bindingを導入済み
  • jupyterモードの使い方
    • ショートカットキーはShift + Escjupyterモードになってからでないと使用できないものがある
    • セルをカット(dd)、ペースト(p)など

すべてのモード共通

  • Ctrl-1 : change cell to code
  • Ctrl-2 : change cell to markdown
  • Ctrl-3 : change cell to raw
  • Ctrl-Shift-Enter : run all cells

jupyterモード限定

  • o : insert cell below
  • dd : cut cell
  • y : copy cell
  • p : paste cell below

ひとこと

まあこれ以上覚えてもいいけど、覚える頭があるならね。
他のEditorとショートカットとかと混乱しなければ、どんどん追加してjupyterマスターになりましょう。

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