- Published on
Jupyter Notebook 10分チュートリアル
- Authors
- Name
- Shou Arisaka / 有坂翔
プログラミング学習やチュートリアルの作成、ノートテイキングなどに重要な役割を果たす、インタラクティブなウェブベースのプログラム実行環境、Jupyter Notebookについて紹介します。
![https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_4b48933e-1c89-40a2-be3f-253a04deab08.png](https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_4b48933e-1c89-40a2-be3f-253a04deab08.png)
「Jupyter Notebook」はブラウザから編集・閲覧できるノートテイキングツール。PythonやBash、その他複数の言語の実行結果をノートとして表示したり、PythonのグラフやLatexなどもレンダリングできる。要はMarkdownもLatexもプログラミング言語もまとめて使用できるEvernote的なもの。 また、ウィジェットと呼ばれる機能を使うことでユーザーからのインプットに答えるインタラクティブなこともできるなど機能は様々。
公式チュートリアルガイド: Jupyter Notebook Tutorial: Definitive Guide (article) - DataCamp
Githubリポジトリ: Topic: jupyter-notebook
Jupyter Notebookのインストールと起動
python3 -m pip install --upgrade pip
python3 -m pip install jupyter
jupyter notebook
![https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_220e863d-e48a-4270-a33c-b119caca9eb3.png](https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_220e863d-e48a-4270-a33c-b119caca9eb3.png)
トークン付きのURLをブラウザで開いたら、NewボタンよりPython3を選択して新規のノートを作成する。
Pythonを実行
![https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_5a954fe0-455e-477f-ae3b-2042030a0aa3.png](https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_5a954fe0-455e-477f-ae3b-2042030a0aa3.png)
from IPython.display import display, Math, Latex
display(Math(r'\sqrt{a^2 + b^2}'))
Markdownを書く
![https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_0d419b74-7aa7-4859-8e4c-a17dae50afa7.png](https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_0d419b74-7aa7-4859-8e4c-a17dae50afa7.png)
Markdownを書きたい場合は画面上部バーのセレクトボックスからMarkdownを選択。
Bashとかを実行
![https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_d9cb7339-0643-421c-8a78-d7710114f6c8.png](https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_d9cb7339-0643-421c-8a78-d7710114f6c8.png)
%%bash
for i in $( seq 1 9 ); do echo ${i} ; done
latexは以下。
![https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_1047a309-d6ae-47d1-ae00-485a5f1d3bda.png](https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_1047a309-d6ae-47d1-ae00-485a5f1d3bda.png)
%%latex
インタラクティブインプットボックスの実装
![https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_ea3ef316-f866-4281-ad44-4978724008be.png](https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_ea3ef316-f866-4281-ad44-4978724008be.png)
from ipywidgets import widgets
from IPython.display import display
text=widgets.Text()
display(text)
def handle_submit(sender):
print(text.value)
text.on_submit(handle_submit)
エクスポートして共有する
![https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_08c5817a-5cb8-45d5-ae5f-3531b8906244.png](https://yuis.xsrv.jp/images/ss/ShareX_ScreenShot_08c5817a-5cb8-45d5-ae5f-3531b8906244.png)
作ったノートはHTMLやIpynb形式でエクスポートできる。HTML形式など静的なタイプへエクスポートするとインタラクティブなインプットボックスなどは使えなくなる。ipynb形式はJupyter Notebookを表す拡張子なのでこれでエクスポートしてGithubなりでシェアするなりするとインタラクティブを残したままシェアできるらしい。