LoginSignup
5
3

More than 1 year has passed since last update.

JupyterLabの設定ファイルの要点

Last updated at Posted at 2023-02-11

概要

JupyterLabの設定は以下のようにコマンド引数として与えることができます。

Bash
jupyter lab --ip=* --no-browser

しかし、設定したい項目が増えてくるとコマンド引数では窮屈なので設定ファイルが必要になってきます。ここでは設定ファイルの使い方について簡単に紹介します。

環境

  • JupyterLab 3.5.3

JupyterLabの設定ファイル

設定ファイルの生成

JupyterLabの設定ファイルのテンプレートは以下のコマンドで生成できます。

Bash
jupyter lab --generate-config

設定ファイルは以下のような内容で ~/.jupyter/jupyter_lab_config.py に生成されます。jupyter lab コマンド実行時にはデフォルトでこのファイルが参照されるようになっています。

~/.jupyter/jupyter_lab_config.py
# Configuration file for lab.

c = get_config()  #noqa

#------------------------------------------------------------------------------
# Application(SingletonConfigurable) configuration
#------------------------------------------------------------------------------
## This is an application.

## The date format used by logging formatters for %(asctime)s
#  Default: '%Y-%m-%d %H:%M:%S'
# c.Application.log_datefmt = '%Y-%m-%d %H:%M:%S'

# ~~~ 以下略 ~~~

このファイルは生成時には全てコメントアウトされており、必要な項目をアンコメントすることで設定を反映することができます。

任意の設定ファイルの指定

jupyter lab コマンド実行時はデフォルトで上記の ~/.jupyter/jupyter_lab_config.py が参照されるようになっていますが、 --config オプションで設定ファイルを指定することもできます。

Bash
jupyter lab --config="./jupyter_lab_config.py"

設定内容

設定項目の参照

コマンド引数や設定ファイルで設定可能な項目はヘルプコマンドで参照できます。

Bash
jupyter lab --help
jupyter lab --help-all

設定ファイルの一例

私がよく使う設定ファイルとしては以下のようなものです。Dockerコンテナをサーバーとして、小規模な使用を想定した設定内容です。

jupyter_lab_config.py
# 接続を許可するIPアドレス
c.ServerApp.ip = "*"

# ポート番号
c.ServerApp.port = 8888

# アクセス時に必要なトークンを無効化
c.ServerApp.token = ""

# コマンド実行時にブラウザを開かない
c.ServerApp.open_browser = False

# ルートユーザーで使用する
c.ServerApp.allow_root = True

# ルートユーザーのホームディレクトリをJupyterLab上のルートディレクトリに設定
c.ServerApp.root_dir = "/root"

# JupyterLab上のファイルツリーで隠しファイルを表示するオプションを使用可能にする
c.ContentsManager.allow_hidden = True

# チェックポイントファイルを /tmp に保存する
c.FileContentsManager.checkpoints_kwargs = {
    "root_dir": "/tmp/ipynb_checkpoints",
}

jupyter lab --generate-config で生成したテンプレートの先頭には c = get_config() がありましたが、ここでは無くても問題ありません。

前述の通り、--config オプションでこのファイルを指定することで設定が反映されます。

Bash
jupyter lab --config="./jupyter_lab_config.py"

ちなみに、上記の設定内容をコマンド引数で指定すると以下のようになります。コマンド引数で与えるにはちょっと窮屈です。

Bash
jupyter lab --ip=* --port=8888 --IdentityProvider.token="" --no-browser --allow-root --notebook-dir="/root" --ContentsManager.allow_hidden=True --FileContentsManager.checkpoints_kwargs="root_dir"="/tmp/ipynb_checkpoints"

参考文献

5
3
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
3