5
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

DockerでJupyterLab環境を作る - 拡張機能追加

Posted at

DockerでJupyterLab環境を作るでは基本的なJupyterLab環境構築の方法をしめしたので、
この記事では拡張機能の設定をしていきたいと思います。

オレオレJupyterlab環境をDockerで作ったを参考に最低限必要なコードに絞ってみました。

jupyterlab-kiteのインストール

jupyterlab-kiteで必要なDockerfile
FROM python:3

# Node.jsのインストール
RUN curl -sL https://deb.nodesource.com/setup_12.x |bash - \
    && apt-get install -y --no-install-recommends \
    nodejs

# pythonライブラリのインストール
COPY requirements.txt .
RUN pip3 install --upgrade pip && \
    pip3 install --no-cache-dir -r requirements.txt \
    && rm -rf ~/.cache/pip

# JupyterLabと拡張機能のインストール
RUN pip3 install --upgrade --no-cache-dir \
    jupyterlab \
    'jupyterlab-kite>=2.0.2' \
    && jupyter labextension install \
        '@kiteco/jupyterlab-kite' \

# jupyter-kiteのインストール
RUN cd && \
    wget https://linux.kite.com/dls/linux/current && \
    chmod 777 current && \
    sed -i 's/"--no-launch"//g' current > /dev/null && \
    ./current --install ./kite-installer

WORKDIR /src
COPY /src /src

jupyterlab-kiteをインストールするためにはNode.jsが必要なので、
冒頭のNode.jsのインストールの箇所が必要になってきます。

それとjupyter-kiteのインストールの箇所もなぜ必要なのかわからず、
外して実行すると下記のようなエラーが表示されました。

2021-07-23_15h51_47.png

jupyter-kiteの実行にはKite Engineのデスクトップアプリケーションが必要ですと表示されます。

docker-compose.yml

docker-compose.ymlファイルには--NotebookApp.token=''を追加しました。

version: '3'
services:
  jupyterlab:
    restart: always
    build:
      context: .
      dockerfile: Dockerfile
    container_name: jupyterlab
    working_dir: '/src'
    tty: true
    volumes:
      - ./src:/src
    ports: 
      - "8080:8080"
    command: jupyter-lab --ip 0.0.0.0 --port=8080 --allow-root --no-browser --NotebookApp.token=''

これで-dのバックグラウンドでの実行でもトークンの入力なくJupyterLabを開くことができます。

docker compose up -d

機能テストで追加したい場合

前の記事で書いているコンテナの中に入ってインストールすることが可能です。

docker exec -it [コンテナ名] bash

その中で試してみて動けばDockerfileに追記するというやり方もできます。
私の場合は後述するVariable Inspectorのインストールができなくて、
エラー内容を確認するために、コンテナに入って実行しました。

その他欲しい機能を追加

追加したい機能は下記を参考にさせてもらいました。

この中から下記3つを入れるのにハマった点が2つありました。

  • Variable Inspector
  • Table of Contents
  • jupyterlab_code_formatter

### Variable Inspector

Variable Inspectorを入れる際は
jupyter labextension install @lckr/jupyterlab_variableinspectorはそのままでは使えませんでした。
こちらに書かれていrequirementsに書かれているライブラリを全てイントールすると問題なく動きます。

ただ、それだと不要なライブラリも多いので代わりに
pip3 install lckr-jupyterlab-variableinspectorを使っても問題なく動きました。

jupyterlab_code_formatter

最初参考記事と同じようにyapfを使おうと思ったらisortが入っていないというエラーがでました。
検索して違いを調べた結果、この記事を参考にblack, isortを使っていこうかなと思ってその2つを選択しています。

root userの設定

コンテナ内で拡張機能を追加しているとroot userとして実行してという
WARNINGがでてきたので、その設定もDockfileに追加した方がよさそうです。
Kiteに加えて下記3つをデフォで使うのにDockerfikeを変更しました。

最終的なDockerfile

現状はどこで何を入れているのか把握しやすいように拡張機能ごとに
RUNを分けて実行していますが、慣れたたら混ぜて実行するかもしれないです。

4つの拡張機能追加
FROM python:3
USER root

RUN curl -sL https://deb.nodesource.com/setup_12.x |bash - \
    && apt-get install -y --no-install-recommends \
    nodejs \
    yarn

# install python library
COPY requirements.txt .
RUN pip3 install --upgrade pip && \
    pip3 install --no-cache-dir -r requirements.txt \
    && rm -rf ~/.cache/pip

# install jupyterlab&kite
RUN pip3 install --no-cache-dir \
    jupyterlab \
    'jupyterlab-kite>=2.0.2' \
    && jupyter labextension install \
        '@kiteco/jupyterlab-kite'

# install jupyterlab_variableinspector
RUN pip3 install --upgrade --no-cache-dir \
    black \
    isort \
    jupyterlab_code_formatter \
    && jupyter labextension install \
        @ryantam626/jupyterlab_code_formatter \
    && jupyter serverextension enable --py jupyterlab_code_formatter

# install jupyterlab_variableinspector
RUN pip3 install --no-cache-dir lckr-jupyterlab-variableinspector

# install other_extentions
RUN  jupyter labextension install \
    @jupyterlab/toc

# install jupyter-kite
RUN cd && \
    wget https://linux.kite.com/dls/linux/current && \
    chmod 777 current && \
    sed -i 's/"--no-launch"//g' current > /dev/null && \
    ./current --install ./kite-installer

WORKDIR /src
COPY /src /src

拡張機能に関する情報

コンテナの中に入って下記のコマンドを実行すると今の入っている拡張機能一覧が表示できます。

拡張機能一覧表示
jupyter labextension list
出力結果
JupyterLab v3.0.16
/usr/local/share/jupyter/labextensions
        @jupyter-widgets/jupyterlab-manager v3.0.0 enabled OK (python, jupyterlab_widgets)
        @kiteco/jupyterlab-kite v2.0.2 enabled OK (python, jupyterlab_kite)
        @ryantam626/jupyterlab_code_formatter v1.4.10 enabled OK (python, jupyterlab-code-formatter)
        @lckr/jupyterlab_variableinspector v3.0.9 enabled OK (python, lckr_jupyterlab_variableinspector)

Other labextensions (built into JupyterLab)
   app dir: /usr/local/share/jupyter/lab
        @jupyterlab/toc v5.0.12 enabled OK

動かない場合はserverextentionも見るとよいそうです。
詳細はわかっていないです。

jupyter serverextension list
出力結果
config dir: /root/.jupyter
    jupyterlab_code_formatter  enabled 
    - Validating...
      jupyterlab_code_formatter 1.4.10 OK
config dir: /usr/local/etc/jupyter
    jupyterlab  enabled 
    - Validating...
      jupyterlab 3.0.16 OK
    jupyterlab_code_formatter  enabled 
    - Validating...
      jupyterlab_code_formatter 1.4.10 OK
5
15
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
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?