nikolaの記事をjupyter labで書く

nikolaの記事をjupyter labで書く

この記事はPython その2 Advent Calendarの22日目の記事です。 当日休日出勤になってしまっていたので遅刻してしまいました。すいません。

nikolaはipynbファイルでブログが書けるstatic site generatorの一種です。 nikolaについては過去に記事を書いていますのでご覧ください。 https://www.hiromasa.info/categories/nikola/

jupyter labの実行環境

ここではjupyter公式のscipy-notebookのdockerイメージを立ち上げてjupyter labを動かしています。ゆくゆくはこれにnikolaのブログ構築環境を追加したイメージを作成したいと思っています。 実行する場合は下記のような感じでブログ本体のあるディレクトリを/mnt/blogにマッピングしてローカルホストに対してポート8888でアクセスします。

docker run -ti -v /path/to/blog:/mnt/blog -p 8888:8888 jupyter/scipy-notebook /bin/bash

jupyter labでのMetadataの設定

Nikolaではこの記事に書いたように.ipynbファイルを公開する場合はblog用のメタデータを.ipynbファイルに付与する必要があります。 Metadataの設定はhttps://github.com/jupyterlab/jupyterlab/issues/1308 のissueで議論され、https://github.com/jupyterlab/jupyterlab/pull/5968 のプルリクエストで追加するためのUIが4月ごろに追加されています。

メタデータを付与するためには、一番左側のアイコンの並んでいるカラムからスパナアイコンを選択肢、Advanced Toolsの中のNotebook Metadataを編集すれば良いです。この記事では下記の様な感じにしています。

"nikola": {
        "tags": "python,docker,nikola,jupyterlab",
        "title": "nikolaの記事をjupyter labで書く",
        "date": "2019-12-22 23:00:00 UTC+09:00",
        "type": "text",
        "slug": "16",
        "category": "",
        "link": "",
        "description": ""
    }

image.png

画像のDrag & Dropおよびペースト

このプルリクエストによれば、

  • ファイルブラウザからセルへのドラッグアンドドロップによるファイルの保存
  • セルへの画像のペースト
  • セルのソースをattachmentを加えるように変更する
  • セルのattatchmentがセルの出力のどこからも参照されていない場合にはattachmentを削除する

が既に実現されているようです。 上記には記述がありませんが、そもそも(finderなどから)ファイルブラウザへのドラッグアンドドロップによる保存は可能ですので、そこから更にセルにドラッグアンドドロップすることも可能ですし、osxならshift+ctrl+command+4で範囲選択した内容をcommand+vで挿入できるので便利です。挿入した画像はbase64エンコードされてipynbに埋め込まれます。注意が必要なのは各セルに対してattachmentを複数埋め込むと後から貼り付けたもので上書きされてしまう点です。各セルにcommand+vで貼る画像は今の所一つにとどめておいた方が無難なようです。

extensionの導入

shift+command+cでコマンドパレットを表示し、Enable Extension Managerを選択・実行してEnableにすると左側のアイコンが並んでいるカラムにextention managerが表示されます。これによりjupyter lab上からextensionがインストールできるようになります。コマンドライン上でjupyter labextension installを実行するのと同等の操作が実行できるのだと思います。ここでは試しにtocと打ってみて目次を表示するextensionである@jupyterlab/tocをインストールしてみます。

image.png

インストールを実行したあとにブラウザのタブをリロードすると、ビルドするかを尋ねられるのでビルドを実行後、ビルドが完了するとreloadを求められました。reload後、左側のアイコンのカラムにTOCのアイコンが出現していました。

更にgitのextensionである@jupyter/gitをインストールしてみます。 インストールを実行すると、下記のようにこのextensionを使用する前に対応したserver extensionであるjupyter-gitをインストールしておく必要があると言われます。 image.png

自分はserver extensionを入れる前にOKを押してしまいましたが、先程と同じようにブラウザのタブをリロードするとextensionのビルドが求められました。しばらくするとBuildが完了した旨が表示されます。

server extensionを入れるにはpipでjupyterlab_gitを入れた後にjupyter serverextension enable --py jupyterlab_gitを実行すれば良いようです。

In [13]:
! pip install jupyterlab_git
Collecting jupyterlab_git
  Downloading https://files.pythonhosted.org/packages/24/9b/ce8a9166a0ed945aada0703d0ebae3e98b539b45db0188e571c1d384e68d/jupyterlab_git-0.9.0-py3-none-any.whl (144kB)
     |████████████████████████████████| 153kB 641kB/s eta 0:00:01
Requirement already satisfied: pexpect in /opt/conda/lib/python3.7/site-packages (from jupyterlab_git) (4.7.0)
Requirement already satisfied: notebook in /opt/conda/lib/python3.7/site-packages (from jupyterlab_git) (6.0.0)
Collecting nbdime>=1.1.0
  Downloading https://files.pythonhosted.org/packages/0f/3f/668922f04fa6848437d8486c495ceea2fbe2b011e1084cb49a792c4bec31/nbdime-1.1.0-py2.py3-none-any.whl (4.3MB)
     |████████████████████████████████| 4.3MB 294kB/s eta 0:00:01
Requirement already satisfied: ptyprocess>=0.5 in /opt/conda/lib/python3.7/site-packages (from pexpect->jupyterlab_git) (0.6.0)
Requirement already satisfied: jupyter-client>=5.3.1 in /opt/conda/lib/python3.7/site-packages (from notebook->jupyterlab_git) (5.3.3)
Requirement already satisfied: ipykernel in /opt/conda/lib/python3.7/site-packages (from notebook->jupyterlab_git) (5.1.3)
Requirement already satisfied: prometheus-client in /opt/conda/lib/python3.7/site-packages (from notebook->jupyterlab_git) (0.7.1)
Requirement already satisfied: traitlets>=4.2.1 in /opt/conda/lib/python3.7/site-packages (from notebook->jupyterlab_git) (4.3.3)
Requirement already satisfied: tornado>=5.0 in /opt/conda/lib/python3.7/site-packages (from notebook->jupyterlab_git) (6.0.3)
Requirement already satisfied: Send2Trash in /opt/conda/lib/python3.7/site-packages (from notebook->jupyterlab_git) (1.5.0)
Requirement already satisfied: terminado>=0.8.1 in /opt/conda/lib/python3.7/site-packages (from notebook->jupyterlab_git) (0.8.3)
Requirement already satisfied: jinja2 in /opt/conda/lib/python3.7/site-packages (from notebook->jupyterlab_git) (2.10.3)
Requirement already satisfied: pyzmq>=17 in /opt/conda/lib/python3.7/site-packages (from notebook->jupyterlab_git) (18.1.1)
Requirement already satisfied: nbformat in /opt/conda/lib/python3.7/site-packages (from notebook->jupyterlab_git) (4.4.0)
Requirement already satisfied: jupyter-core>=4.4.0 in /opt/conda/lib/python3.7/site-packages (from notebook->jupyterlab_git) (4.6.1)
Requirement already satisfied: nbconvert in /opt/conda/lib/python3.7/site-packages (from notebook->jupyterlab_git) (5.6.1)
Requirement already satisfied: ipython-genutils in /opt/conda/lib/python3.7/site-packages (from notebook->jupyterlab_git) (0.2.0)
Requirement already satisfied: six in /opt/conda/lib/python3.7/site-packages (from nbdime>=1.1.0->jupyterlab_git) (1.13.0)
Requirement already satisfied: pygments in /opt/conda/lib/python3.7/site-packages (from nbdime>=1.1.0->jupyterlab_git) (2.5.2)
Requirement already satisfied: requests in /opt/conda/lib/python3.7/site-packages (from nbdime>=1.1.0->jupyterlab_git) (2.22.0)
Collecting colorama
  Downloading https://files.pythonhosted.org/packages/c9/dc/45cdef1b4d119eb96316b3117e6d5708a08029992b2fee2c143c7a0a5cc5/colorama-0.4.3-py2.py3-none-any.whl
Collecting GitPython!=2.1.4,!=2.1.5,!=2.1.6
  Downloading https://files.pythonhosted.org/packages/20/8c/4543981439d23c4ff65b2e62dddd767ebc84a8e664a9b67e840d1e2730d3/GitPython-3.0.5-py3-none-any.whl (455kB)
     |████████████████████████████████| 460kB 801kB/s eta 0:00:01
Requirement already satisfied: python-dateutil>=2.1 in /opt/conda/lib/python3.7/site-packages (from jupyter-client>=5.3.1->notebook->jupyterlab_git) (2.8.1)
Requirement already satisfied: ipython>=5.0.0 in /opt/conda/lib/python3.7/site-packages (from ipykernel->notebook->jupyterlab_git) (7.10.1)
Requirement already satisfied: decorator in /opt/conda/lib/python3.7/site-packages (from traitlets>=4.2.1->notebook->jupyterlab_git) (4.4.1)
Requirement already satisfied: MarkupSafe>=0.23 in /opt/conda/lib/python3.7/site-packages (from jinja2->notebook->jupyterlab_git) (1.1.1)
Requirement already satisfied: jsonschema!=2.5.0,>=2.4 in /opt/conda/lib/python3.7/site-packages (from nbformat->notebook->jupyterlab_git) (3.2.0)
Requirement already satisfied: bleach in /opt/conda/lib/python3.7/site-packages (from nbconvert->notebook->jupyterlab_git) (3.1.0)
Requirement already satisfied: entrypoints>=0.2.2 in /opt/conda/lib/python3.7/site-packages (from nbconvert->notebook->jupyterlab_git) (0.3)
Requirement already satisfied: defusedxml in /opt/conda/lib/python3.7/site-packages (from nbconvert->notebook->jupyterlab_git) (0.6.0)
Requirement already satisfied: pandocfilters>=1.4.1 in /opt/conda/lib/python3.7/site-packages (from nbconvert->notebook->jupyterlab_git) (1.4.2)
Requirement already satisfied: mistune<2,>=0.8.1 in /opt/conda/lib/python3.7/site-packages (from nbconvert->notebook->jupyterlab_git) (0.8.4)
Requirement already satisfied: testpath in /opt/conda/lib/python3.7/site-packages (from nbconvert->notebook->jupyterlab_git) (0.4.4)
Requirement already satisfied: idna<2.9,>=2.5 in /opt/conda/lib/python3.7/site-packages (from requests->nbdime>=1.1.0->jupyterlab_git) (2.8)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.7/site-packages (from requests->nbdime>=1.1.0->jupyterlab_git) (2019.11.28)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /opt/conda/lib/python3.7/site-packages (from requests->nbdime>=1.1.0->jupyterlab_git) (1.25.7)
Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /opt/conda/lib/python3.7/site-packages (from requests->nbdime>=1.1.0->jupyterlab_git) (3.0.4)
Collecting gitdb2>=2.0.0
  Downloading https://files.pythonhosted.org/packages/03/6c/99296f89bad2ef85626e1df9f677acbee8885bb043ad82ad3ed4746d2325/gitdb2-2.0.6-py2.py3-none-any.whl (63kB)
     |████████████████████████████████| 71kB 1.7MB/s eta 0:00:01
Requirement already satisfied: backcall in /opt/conda/lib/python3.7/site-packages (from ipython>=5.0.0->ipykernel->notebook->jupyterlab_git) (0.1.0)
Requirement already satisfied: prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0 in /opt/conda/lib/python3.7/site-packages (from ipython>=5.0.0->ipykernel->notebook->jupyterlab_git) (3.0.2)
Requirement already satisfied: pickleshare in /opt/conda/lib/python3.7/site-packages (from ipython>=5.0.0->ipykernel->notebook->jupyterlab_git) (0.7.5)
Requirement already satisfied: setuptools>=18.5 in /opt/conda/lib/python3.7/site-packages (from ipython>=5.0.0->ipykernel->notebook->jupyterlab_git) (42.0.2.post20191201)
Requirement already satisfied: jedi>=0.10 in /opt/conda/lib/python3.7/site-packages (from ipython>=5.0.0->ipykernel->notebook->jupyterlab_git) (0.15.1)
Requirement already satisfied: pyrsistent>=0.14.0 in /opt/conda/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat->notebook->jupyterlab_git) (0.15.6)
Requirement already satisfied: attrs>=17.4.0 in /opt/conda/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat->notebook->jupyterlab_git) (19.3.0)
Requirement already satisfied: importlib-metadata; python_version < "3.8" in /opt/conda/lib/python3.7/site-packages (from jsonschema!=2.5.0,>=2.4->nbformat->notebook->jupyterlab_git) (1.1.0)
Requirement already satisfied: webencodings in /opt/conda/lib/python3.7/site-packages (from bleach->nbconvert->notebook->jupyterlab_git) (0.5.1)
Collecting smmap2>=2.0.0
  Downloading https://files.pythonhosted.org/packages/55/d2/866d45e3a121ee15a1dc013824d58072fd5c7799c9c34d01378eb262ca8f/smmap2-2.0.5-py2.py3-none-any.whl
Requirement already satisfied: wcwidth in /opt/conda/lib/python3.7/site-packages (from prompt-toolkit!=3.0.0,!=3.0.1,<3.1.0,>=2.0.0->ipython>=5.0.0->ipykernel->notebook->jupyterlab_git) (0.1.7)
Requirement already satisfied: parso>=0.5.0 in /opt/conda/lib/python3.7/site-packages (from jedi>=0.10->ipython>=5.0.0->ipykernel->notebook->jupyterlab_git) (0.5.1)
Requirement already satisfied: zipp>=0.5 in /opt/conda/lib/python3.7/site-packages (from importlib-metadata; python_version < "3.8"->jsonschema!=2.5.0,>=2.4->nbformat->notebook->jupyterlab_git) (0.6.0)
Requirement already satisfied: more-itertools in /opt/conda/lib/python3.7/site-packages (from zipp>=0.5->importlib-metadata; python_version < "3.8"->jsonschema!=2.5.0,>=2.4->nbformat->notebook->jupyterlab_git) (7.2.0)
Installing collected packages: colorama, smmap2, gitdb2, GitPython, nbdime, jupyterlab-git
Successfully installed GitPython-3.0.5 colorama-0.4.3 gitdb2-2.0.6 jupyterlab-git-0.9.0 nbdime-1.1.0 smmap2-2.0.5
In [14]:
! jupyter serverextension enable --py jupyterlab_git
Enabling: jupyterlab_git
- Writing config: /home/jovyan/.jupyter
    - Validating...
      jupyterlab_git  OK

上記を実行した後にブラウザのタブを再実行したところnbdime-jupyterlabのビルドがダイアログが表示され要求されたため実行しました。 しかしここまでを実行したところ、左側のアイコンが並んでいるカラムにgitのアイコンが表示されるようにはなりましたが、ローカルのリポジトリを読み込んでくれる気配がありませんのでこのあたりについては要調査の上で追記したいと思います。

2020/1/13追記

  • 後日確認したところjupyter lab上でgitのextensionをインストールする前にjupyter serverextension enable --py jupyterlab_gitを実行しておくと、自動的にjupyter labのextensionもインストールされていました
  • 事前にコマンドラインからビルドを実行しておく場合はjupyter lab buildで行けるようです
  • ローカルのリポジトリを読み込まない問題はリポジトリのページを確認したところTroubleShootingの欄のIssue: the Git panel does not recognize that you are in a Git repository.として記載がありましたが原因はjupyter serverextension enable --py jupyterlab_gitを実行した後に、コマンドラインまたはjupyter lab上の表示からビルドを行った上でjupyter labを立ち上げていないことでした。つまり稼働中にserverextensionをenableしてからビルドを行うのではなく、稼働前にビルドを実行しておく必要がありました。上記を踏まえた上でjupyter labを再起動したら下記のような感じのインタフェースが使えるようになりました。

image.png

ブログのビルド

ここではブログのビルド自体はローカル環境(docker上ではない)で実行しています。

$ nikola build
$ nikola serve -b

上記を実行するとビルドを実行した上でローカルサーバを立ち上げてくれ、-bオプションによって自動的にブラウザのタブが開きます。内容に問題ないことを確認したら、あとはサーバーにプッシュしておしまいです。

まとめ

jupyter lab上でブログを執筆する環境構築について記しました。

Comments

Comments powered by Disqus