LoginSignup
5
5

More than 5 years have passed since last update.

Jupyter notebook(Python 3.6環境)でnwdiagを使う

Last updated at Posted at 2018-04-09

はじめに

Jupyter NotebookのModuleで簡単にネットワーク図を作成できるPythonライブラリのnwdiagが
ipthon-diagsという形で出ています。
ただ、最終更新が2013年と古いため、
Dockerで配布されているJupyter Notebook(Python3.6の環境)でipython-diagsを試したところ、
Python3.6上では動かないモジュールでした。(Unicode Encode Errorが発生)

そこで、修正個所は多くなかったので、動くように直しました。
https://gist.github.com/a8e8269d4331b6b93e7ef90744fe0b3b.git

動作スクショ
image.png

この記事ではそのコードのJupyter Notebookへの適応手順を記載します。

手順

  • Docker imageをPullする
shell
docker pull jupyter/datascience-notebook
  • Dockerコンテナを起動する
shell

docker run -d --name notebook -p 8888:8888 jupyter/datascience-notebook
  • 起動したコンテナへ接続する
shell
docker exec -it  notebook /bin/bash
  • トークンを表示し、出力されたURLへブラウザからアクセスする。
shell
jupyter notebook list
  • アクセスしたブラウザにて、よしなにnwdiagを実行したいnotebookを作成する。(kernelはpython3を選択する)

  • コンソールに戻り、nwdiagをインストールする

shell
pip install nwdiag
  • diagmagic.pyをgit cloneする
shell

git clone https://gist.github.com/a8e8269d4331b6b93e7ef90744fe0b3b.git
  • 作成したnotebookのディレクトリ配下の.ipython/extensions/へdiagmagic.pyをコピーする。
shell
cp a8e8269d4331b6b93e7ef90744fe0b3b/diagmagic.py <notebookの格納ディレクトリ>/.ipython/extensions/
  • 下のスクリーンショットの様に定義して、「Run」で実行していけばネットワーク図を生成することができる。 image.png
5
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
5
5