Python開発にはJupyter notebookが便利

pythonWeb開発

エンジニアのブログを見ていると、Jupyter notebookを使用しているケースがたびたび見受けられるので、今回インストールしてどんなものか試してみました。

自分で簡単なテストコードを書いて、動作確認してみたいといったときは、便利なツールだと思います。

特にデータ分析系で役に立ちそうです。

使い勝手は、UIも含めてなかなかよいです。

Jupyter notebookとは?

Webブラウザからファイルやディレクトリを操作できるwebアプリケーションです。

notenook形式のファイル(.ipynb)を操作し、実行結果を確認しながら、開発を進めることができます。

主にpythonのデータ分析系の開発者が使用している模様です。

以下のように、ブラウザからファイル操作できます。

上のRunボタンを押下するとPythonを実行して結果をみることもできます。

Jupyter notebookインストール方法

いつも通り、インストール方法を記載しておきます。

以下、環境情報です。

OS: CentOS7
python version: 3.8.3

まず、最初に開発用のディレクトリ作成してます。あくまでこれはテスト用です。

$ mkdir python_env
$ cd python_env/
$ pwd
/home/vagrant/python_env

virtual envを作成する。

$ python -m venv jupyter_test
$ source jupyter_test/bin/activate

virtual envをactivateしたら、さっそくインストールしましょう。

$ pip install jupyter
Collecting jupyter
  Downloading https://files.pythonhosted.org/packages/83/df/0f5dd132200728a86190397e1ea87cd76244e42d39ec5e88efd25b2abd7e/jupyter-1.0.0-py2.py3-none-any.whl
Collecting jupyter-console (from jupyter)
  Downloading https://files.pythonhosted.org/packages/0a/89/742fa5a80b552ffcb6a8922712697c6e6828aee7b91ee4ae2b79f00f8401/jupyter_console-6.1.0-py2.py3-none-any.whl
Collecting nbconvert (from jupyter)
  Downloading https://files.pythonhosted.org/packages/79/6c/05a569e9f703d18aacb89b7ad6075b404e8a4afde2c26b73ca77bb644b14/nbconvert-5.6.1-py2.py3-none-any.whl (455kB)
     |████████████████████████████████| 460kB 727kB/s 
Collecting qtconsole (from jupyter)
  Downloading https://files.pythonhosted.org/packages/42/63/08c241c351d527089083002f65905b81457fc5849fb87cafb08f2a00b354/qtconsole-4.7.5-py2.py3-none-any.whl (118kB)
     |████████████████████████████████| 122kB 2.8MB/s 
Collecting ipywidgets (from jupyter)
  Downloading https://files.pythonhosted.org/packages/56/a0/dbcf5881bb2f51e8db678211907f16ea0a182b232c591a6d6f276985ca95/ipywidgets-7.5.1-py2.py3-none-any.whl (121kB)
     |████████████████████████████████| 122kB 1.2MB/s 
Collecting ipykernel (from jupyter)
  Downloading https://files.pythonhosted.org/packages/51/84/eed2d91a20baf5fbbebd8380e3ec79092fd69c0af3a14a4393bf20287387/ipykernel-5.3.2-py3-none-any.whl (120kB)
     |████████████████████████████████| 122kB 2.4MB/s 
Collecting notebook (from jupyter)
  Downloading https://files.pythonhosted.org/packages/b1/f1/0a67f09ef53a342403ffa66646ee39273e0ac79ffa5de5dbe2f3e28b5bdf/notebook-6.0.3-py3-none-any.whl (9.7MB)
     |████████████████████████████████| 9.7MB 849kB/s 
Collecting prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 (from jupyter-console->jupyter)
  Downloading https://files.pythonhosted.org/packages/e4/a7/81b39aa50e9284fe2cb21cc7fb7de7817b224172d42793fd57451d38842b/prompt_toolkit-3.0.5-py3-none-any.whl (351kB)
     |████████████████████████████████| 358kB 552kB/s 
Collecting ipython (from jupyter-console->jupyter)
  Downloading https://files.pythonhosted.org/packages/23/6a/210816c943c9aeeb29e4e18a298f14bf0e118fe222a23e13bfcc2d41b0a4/ipython-7.16.1-py3-none-any.whl (785kB)
     |████████████████████████████████| 788kB 624kB/s 
Collecting jupyter-client (from jupyter-console->jupyter)
  Downloading https://files.pythonhosted.org/packages/93/4b/26eabcf24b3e7cfdf063858609af45eecea245cf64d82e2fc88d2ef57857/jupyter_client-6.1.5-py3-none-any.whl (107kB)
     |████████████████████████████████| 112kB 644kB/s 
Collecting pygments (from jupyter-console->jupyter)
  Downloading https://files.pythonhosted.org/packages/2d/68/106af3ae51daf807e9cdcba6a90e518954eb8b70341cee52995540a53ead/Pygments-2.6.1-py3-none-any.whl (914kB)
     |████████████████████████████████| 921kB 525kB/s 
Collecting bleach (from nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/9a/1e/7d6cb3b27cd2c490558349ca5d5cc05b390b017da1c704cac807ac8bd9fb/bleach-3.1.5-py2.py3-none-any.whl (151kB)
     |████████████████████████████████| 153kB 646kB/s 
Collecting jinja2>=2.4 (from nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/30/9e/f663a2aa66a09d838042ae1a2c5659828bb9b41ea3a6efa20a20fd92b121/Jinja2-2.11.2-py2.py3-none-any.whl (125kB)
     |████████████████████████████████| 133kB 461kB/s 
Collecting nbformat>=4.4 (from nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/4d/d1/b568bd35f95321f152f594b3647cd080e96d3347843ff2fa34dce871b8bf/nbformat-5.0.7-py3-none-any.whl (170kB)
     |████████████████████████████████| 174kB 465kB/s 
Collecting jupyter-core (from nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/63/0d/df2d17cdf389cea83e2efa9a4d32f7d527ba78667e0153a8e676e957b2f7/jupyter_core-4.6.3-py2.py3-none-any.whl (83kB)
     |████████████████████████████████| 92kB 447kB/s 
Collecting mistune<2,>=0.8.1 (from nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/09/ec/4b43dae793655b7d8a25f76119624350b4d65eb663459eb9603d7f1f0345/mistune-0.8.4-py2.py3-none-any.whl
Collecting pandocfilters>=1.4.1 (from nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/4c/ea/236e2584af67bb6df960832731a6e5325fd4441de001767da328c33368ce/pandocfilters-1.4.2.tar.gz
Collecting defusedxml (from nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/06/74/9b387472866358ebc08732de3da6dc48e44b0aacd2ddaa5cb85ab7e986a2/defusedxml-0.6.0-py2.py3-none-any.whl
Collecting traitlets>=4.2 (from nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/ca/ab/872a23e29cec3cf2594af7e857f18b687ad21039c1f9b922fac5b9b142d5/traitlets-4.3.3-py2.py3-none-any.whl (75kB)
     |████████████████████████████████| 81kB 477kB/s 
Collecting testpath (from nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/1b/9e/1a170feaa54f22aeb5a5d16c9015e82234275a3c8ab630b552493f9cb8a9/testpath-0.4.4-py2.py3-none-any.whl (163kB)
     |████████████████████████████████| 174kB 399kB/s 
Collecting entrypoints>=0.2.2 (from nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/ac/c6/44694103f8c221443ee6b0041f69e2740d89a25641e62fb4f2ee568f2f9c/entrypoints-0.3-py2.py3-none-any.whl
Collecting qtpy (from qtconsole->jupyter)
  Downloading https://files.pythonhosted.org/packages/cd/fd/9972948f02e967b691cc0ca1f26124826a3b88cb38f412a8b7935b8c3c72/QtPy-1.9.0-py2.py3-none-any.whl (54kB)
     |████████████████████████████████| 61kB 647kB/s 
Collecting ipython-genutils (from qtconsole->jupyter)
  Downloading https://files.pythonhosted.org/packages/fa/bc/9bd3b5c2b4774d5f33b2d544f1460be9df7df2fe42f352135381c347c69a/ipython_genutils-0.2.0-py2.py3-none-any.whl
Collecting pyzmq>=17.1 (from qtconsole->jupyter)
  Downloading https://files.pythonhosted.org/packages/0c/c8/57cb8955afd0ba7bfbb8ba7e7627c9fc1a595543a4fa7b774a26b094fc12/pyzmq-19.0.1-cp38-cp38-manylinux1_x86_64.whl (1.1MB)
     |████████████████████████████████| 1.1MB 492kB/s 
Collecting widgetsnbextension~=3.5.0 (from ipywidgets->jupyter)
  Downloading https://files.pythonhosted.org/packages/6c/7b/7ac231c20d2d33c445eaacf8a433f4e22c60677eb9776c7c5262d7ddee2d/widgetsnbextension-3.5.1-py2.py3-none-any.whl (2.2MB)
     |████████████████████████████████| 2.2MB 872kB/s 
Collecting tornado>=4.2 (from ipykernel->jupyter)
  Downloading https://files.pythonhosted.org/packages/95/84/119a46d494f008969bf0c775cb2c6b3579d3c4cc1bb1b41a022aa93ee242/tornado-6.0.4.tar.gz (496kB)
     |████████████████████████████████| 501kB 919kB/s 
Collecting Send2Trash (from notebook->jupyter)
  Downloading https://files.pythonhosted.org/packages/49/46/c3dc27481d1cc57b9385aff41c474ceb7714f7935b1247194adae45db714/Send2Trash-1.5.0-py3-none-any.whl
Collecting terminado>=0.8.1 (from notebook->jupyter)
  Downloading https://files.pythonhosted.org/packages/ff/96/1d9a2c23990aea8f8e0b5c3b6627d03196a73771a17a2d9860bbe9823ab6/terminado-0.8.3-py2.py3-none-any.whl
Collecting prometheus-client (from notebook->jupyter)
  Downloading https://files.pythonhosted.org/packages/3f/0e/554a265ffdc56e1494ef08e18f765b0cdec78797f510c58c45cf37abb4f4/prometheus_client-0.8.0-py2.py3-none-any.whl (53kB)
     |████████████████████████████████| 61kB 1.8MB/s 
Collecting wcwidth (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->jupyter-console->jupyter)
  Downloading https://files.pythonhosted.org/packages/59/7c/e39aca596badaf1b78e8f547c807b04dae603a433d3e7a7e04d67f2ef3e5/wcwidth-0.2.5-py2.py3-none-any.whl
Collecting jedi>=0.10 (from ipython->jupyter-console->jupyter)
  Downloading https://files.pythonhosted.org/packages/07/83/7e711550fcb2722f1ca9c8564d5bb23f625ae67d99f4a360b428c0f3e932/jedi-0.17.1-py2.py3-none-any.whl (1.4MB)
     |████████████████████████████████| 1.4MB 1.2MB/s 
Collecting pickleshare (from ipython->jupyter-console->jupyter)
  Downloading https://files.pythonhosted.org/packages/9a/41/220f49aaea88bc6fa6cba8d05ecf24676326156c23b991e80b3f2fc24c77/pickleshare-0.7.5-py2.py3-none-any.whl
Collecting decorator (from ipython->jupyter-console->jupyter)
  Downloading https://files.pythonhosted.org/packages/ed/1b/72a1821152d07cf1d8b6fce298aeb06a7eb90f4d6d41acec9861e7cc6df0/decorator-4.4.2-py2.py3-none-any.whl
Requirement already satisfied: setuptools>=18.5 in ./jupyter_test/lib/python3.8/site-packages (from ipython->jupyter-console->jupyter) (41.2.0)
Collecting pexpect; sys_platform != "win32" (from ipython->jupyter-console->jupyter)
  Downloading https://files.pythonhosted.org/packages/39/7b/88dbb785881c28a102619d46423cb853b46dbccc70d3ac362d99773a78ce/pexpect-4.8.0-py2.py3-none-any.whl (59kB)
     |████████████████████████████████| 61kB 1.9MB/s 
Collecting backcall (from ipython->jupyter-console->jupyter)
  Downloading https://files.pythonhosted.org/packages/4c/1c/ff6546b6c12603d8dd1070aa3c3d273ad4c07f5771689a7b69a550e8c951/backcall-0.2.0-py2.py3-none-any.whl
Collecting python-dateutil>=2.1 (from jupyter-client->jupyter-console->jupyter)
  Downloading https://files.pythonhosted.org/packages/d4/70/d60450c3dd48ef87586924207ae8907090de0b306af2bce5d134d78615cb/python_dateutil-2.8.1-py2.py3-none-any.whl (227kB)
     |████████████████████████████████| 235kB 1.2MB/s 
Collecting six>=1.9.0 (from bleach->nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/ee/ff/48bde5c0f013094d729fe4b0316ba2a24774b3ff1c52d924a8a4cb04078a/six-1.15.0-py2.py3-none-any.whl
Collecting packaging (from bleach->nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/46/19/c5ab91b1b05cfe63cccd5cfc971db9214c6dd6ced54e33c30d5af1d2bc43/packaging-20.4-py2.py3-none-any.whl
Collecting webencodings (from bleach->nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/f4/24/2a3e3df732393fed8b3ebf2ec078f05546de641fe1b667ee316ec1dcf3b7/webencodings-0.5.1-py2.py3-none-any.whl
Collecting MarkupSafe>=0.23 (from jinja2>=2.4->nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/4b/20/f6d7648c81cb84815d0be935d5c74cd1cc0239e43eadb1a61062d34b6543/MarkupSafe-1.1.1-cp38-cp38-manylinux1_x86_64.whl
Collecting jsonschema!=2.5.0,>=2.4 (from nbformat>=4.4->nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/c5/8f/51e89ce52a085483359217bc72cdbf6e75ee595d5b1d4b5ade40c7e018b8/jsonschema-3.2.0-py2.py3-none-any.whl (56kB)
     |████████████████████████████████| 61kB 2.2MB/s 
Collecting ptyprocess; os_name != "nt" (from terminado>=0.8.1->notebook->jupyter)
  Downloading https://files.pythonhosted.org/packages/d1/29/605c2cc68a9992d18dada28206eeada56ea4bd07a239669da41674648b6f/ptyprocess-0.6.0-py2.py3-none-any.whl
Collecting parso<0.8.0,>=0.7.0 (from jedi>=0.10->ipython->jupyter-console->jupyter)
  Downloading https://files.pythonhosted.org/packages/b5/61/998cce9e7476de000d031874df26a18f67cb73448164fc44a98f0c55920b/parso-0.7.0-py2.py3-none-any.whl (100kB)
     |████████████████████████████████| 102kB 1.4MB/s 
Collecting pyparsing>=2.0.2 (from packaging->bleach->nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/8a/bb/488841f56197b13700afd5658fc279a2025a39e22449b7cf29864669b15d/pyparsing-2.4.7-py2.py3-none-any.whl (67kB)
     |████████████████████████████████| 71kB 1.7MB/s 
Collecting pyrsistent>=0.14.0 (from jsonschema!=2.5.0,>=2.4->nbformat>=4.4->nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/9f/0d/cbca4d0bbc5671822a59f270e4ce3f2195f8a899c97d0d5abb81b191efb5/pyrsistent-0.16.0.tar.gz (108kB)
     |████████████████████████████████| 112kB 1.3MB/s 
Collecting attrs>=17.4.0 (from jsonschema!=2.5.0,>=2.4->nbformat>=4.4->nbconvert->jupyter)
  Downloading https://files.pythonhosted.org/packages/a2/db/4313ab3be961f7a763066401fb77f7748373b6094076ae2bda2806988af6/attrs-19.3.0-py2.py3-none-any.whl
Installing collected packages: wcwidth, prompt-toolkit, parso, jedi, pickleshare, decorator, six, ipython-genutils, traitlets, ptyprocess, pexpect, pygments, backcall, ipython, jupyter-core, pyzmq, tornado, python-dateutil, jupyter-client, ipykernel, jupyter-console, pyparsing, packaging, webencodings, bleach, MarkupSafe, jinja2, pyrsistent, attrs, jsonschema, nbformat, mistune, pandocfilters, defusedxml, testpath, entrypoints, nbconvert, qtpy, qtconsole, Send2Trash, terminado, prometheus-client, notebook, widgetsnbextension, ipywidgets, jupyter
  Running setup.py install for tornado ... done
  Running setup.py install for pyrsistent ... done
  Running setup.py install for pandocfilters ... done
Successfully installed MarkupSafe-1.1.1 Send2Trash-1.5.0 attrs-19.3.0 backcall-0.2.0 bleach-3.1.5 decorator-4.4.2 defusedxml-0.6.0 entrypoints-0.3 ipykernel-5.3.2 ipython-7.16.1 ipython-genutils-0.2.0 ipywidgets-7.5.1 jedi-0.17.1 jinja2-2.11.2 jsonschema-3.2.0 jupyter-1.0.0 jupyter-client-6.1.5 jupyter-console-6.1.0 jupyter-core-4.6.3 mistune-0.8.4 nbconvert-5.6.1 nbformat-5.0.7 notebook-6.0.3 packaging-20.4 pandocfilters-1.4.2 parso-0.7.0 pexpect-4.8.0 pickleshare-0.7.5 prometheus-client-0.8.0 prompt-toolkit-3.0.5 ptyprocess-0.6.0 pygments-2.6.1 pyparsing-2.4.7 pyrsistent-0.16.0 python-dateutil-2.8.1 pyzmq-19.0.1 qtconsole-4.7.5 qtpy-1.9.0 six-1.15.0 terminado-0.8.3 testpath-0.4.4 tornado-6.0.4 traitlets-4.3.3 wcwidth-0.2.5 webencodings-0.5.1 widgetsnbextension-3.5.1
WARNING: You are using pip version 19.2.3, however version 20.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

pipのupgradeのwarningが出たので、upgradeしておきます。

$ pip install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/43/84/23ed6a1796480a6f1a2d38f2802901d078266bda38388954d01d3f2e821d/pip-20.1.1-py2.py3-none-any.whl (1.5MB)
     |████████████████████████████████| 1.5MB 729kB/s 
Installing collected packages: pip
  Found existing installation: pip 19.2.3
    Uninstalling pip-19.2.3:
      Successfully uninstalled pip-19.2.3
Successfully installed pip-20.1.1

あとは、開発したいディレクトリに移動し、そこでJupyter notebookを起動するだけです。

今回は、適当にpython_devというディレクトリを作成して、そのフォルダ上でファイル操作をしてみます。

$ mkdir python_dev
$ cd python_dev/
$ pwd
/home/vagrant/python_dev

Jupyter notebookを起動する。今回は、Vagrantで起動したCentOS7にアクセスするので、CentOS7のローカルIPを付与して起動してます。

$ jupyter notebook --ip 192.168.XX.XX
[I 12:39:54.966 NotebookApp] Serving notebooks from local directory: /home/vagrant/python_dev
[I 12:39:54.966 NotebookApp] The Jupyter Notebook is running at:
[I 12:39:54.966 NotebookApp] http://192.168.XX.XX:8888/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[I 12:39:54.966 NotebookApp]  or http://127.0.0.1:8888/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
[I 12:39:54.966 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 12:39:54.978 NotebookApp] No web browser found: could not locate runnable browser.
[C 12:39:54.979 NotebookApp] 
    
    To access the notebook, open this file in a browser:
        file:///home/vagrant/.local/share/jupyter/runtime/nbserver-XXXXX-open.html
    Or copy and paste one of these URLs:
        http://192.168.XX.XX:8888/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
     or http://127.0.0.1:8888/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Host OS上のブラウザから、

`http://192.168.XX.XX:8888/?token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`

にアクセスすればOKです。

Jupyter notebookをちょっと使ってみた感想

MathematicaやMatlabに、ちょっと近い印象をうけました。コードの動作を確認しながら開発を進められるのと、他の人にも結果を共有できるのがいいですね。

参考

Install and Use — Jupyter Documentation 4.1.1 alpha documentation
タイトルとURLをコピーしました