Notebook config
===
> #ipython, jupyter notebook, python notebook, jupyterlab, elyra
###### tags: `Jupyter`
###### tags: `Jupyter`, `JupyterLab`, `Notebook`, `config`
<br>
[TOC]
<br>
## 預設 config
- [JupyterLab User Settings File](https://stackoverflow.com/questions/48950670)
```bash
$ jupyter-lab --generate-config
Writing default config to: /home/tj/.jupyter/jupyter_lab_config.py
```
## 常用 config
```python=
import os
# c: <class 'traitlets.config.loader.Config'>
# without this, you can "log out" or "shut down" by the following way,
#
# CaseA: [Jupyter UI][menu] File > Log out <--- disable it
# CaseB: [Jupyter UI][menu] File > Shut down <--- disable it
#
# If CaseA:
# -> [menu] File > Log out
# -> redirected to the infra-login page.
# -> log in again (OK)
#
# If CaseB:
# -> [menu] File > Shut down
# -> (After shutting down, you won't be able to enter the jupyter UI)
# -> launch again (in the [Notebook Service Details] page)
# -> you will get the following error:
# ```
# {
# "message":"An invalid response was received from the upstream server"
# }
# ```
# -> you need to stop & start the notebook service
#
# @see https://jupyter-server.readthedocs.io/en/latest/other/full-config.html
#
c.ServerApp.quit_button = False
# without 'TOKEN', you are required to enter a password or token
# [Jupyter UI] Password or token: [________] [Log in]
# [Jupyter UI] Token authentication is enabled
#
c.ServerApp.token = os.environ.get('TOKEN', '')
# if TOKEN is unset, check PASSWORD
if not c.ServerApp.token:
password = os.environ.get('PASSWORD', '')
if password:
from notebook.auth import passwd
c.ServerApp.password = passwd(password)
# withuot 'BASE_URL', it will cause:
# - {"message":"no Route matched with those values"}
#
# default URL:
# https://your_host/lab?
#
# after applying the following config:
# BASE_URL=/jupyter/88b70935-48f1-43bd-80a0-24b46e3858fb/parabricks-1/
#
# the URL becomes:
# https://your_host/jupyter/88b70935-48f1-43bd-80a0-24b46e3858fb/parabricks-1/lab?
#
c.ServerApp.base_url = os.environ.get('BASE_URL', '/')
```
<br>
<hr>
<br>
## 相關 python code
### backend extension 讀取 notebook 根目錄
> #靈感來源:noteboot -> config -> jupyter_notebook_config.py -> 'traitlets.config.loader.Config' -> traitlets 相關的 code
#jupyter lab --ServerApp.root_dir=, config, server_app.web_app.settings
[](https://i.imgur.com/a5TGUyK.png)
```python
# __init__.py
def _load_jupyter_server_extension(server_app):
print(">>>>>> server_app.web_app.settings['server_root_dir']:",
server_app.web_app.settings.get("server_root_dir"))
print(">>>>>> server_app.config_dir:", server_app.config_dir)
print(">>>>>> server_app.notebook_dir:", server_app.notebook_dir)
print(">>>>>> server_app.root_dir:", server_app.root_dir)
print(">>>>>> server_app.runtime_dir:", server_app.runtime_dir)
```
- 參考資料
- [How to get the path of the current root_dir using FileContentsManager? (Jupyter Notebook)](https://stackoverflow.com/questions/47847214)
- ### [Jupyter Notebook 安裝插件](https://www.twblogs.net/a/5c47105bbd9eee35b21f08f5)
```python
# 打印 Jupyter 配置目錄的路徑
from jupyter_core.paths import jupyter_config_dir
jupyter_dir = jupyter_config_dir()
print(jupyter_dir)
```
執行結果:
```
/opt/ASUSCloudInfra
```
<br>
### 自行讀取 jupyter_notebook_config.py
```python
from jupyter_core.paths import jupyter_config_dir
from traitlets.config import Config
import os
c = Config()
file_path = os.path.join(jupyter_config_dir(), 'jupyter_notebook_config.py')
exec(open(file_path).read())
root_dir = c['FileContentsManager']['root_dir']
```
- [How to get the path of the current root_dir using FileContentsManager? (Jupyter Notebook)](https://stackoverflow.com/questions/47847214/)
```python
def load_jupyter_server_extension(nb_app):
web_app = nb_app.web_app
host_pattern = '.*$'
# here
print("test" + nb_app.notebook_dir)
```
- 關鍵
- - server_root_dir : ~/Asus/workplace/jupyter
<br>
## Trouleshooting
### jupyter 無法載入 jupyter_notebook_config.py
- Ubuntu:20.04: 也不正常
```
$ docker run --rm -it -p 38888:8888 -p 38889:8889 ubuntu:20.04
```
```bash=
apt update
# require the timezone configuration:
# - 6 Taipei
# - 73. Taipei
apt install -y software-properties-common
apt install -y python3-pip
pip install jupyterlab==3.2.1
```
```bash=
export JUPYTER_CONFIG_DIR=/opt/ASUSCloudInfra/
jupyter --config-dir
mkdir -p /opt/ASUSCloudInfra/
CONFIG_FILE=/opt/ASUSCloudInfra/jupyter_lab_config.py
echo 'print(">" * 60)' > $CONFIG_FILE
echo 'print(">" * 60)' >> $CONFIG_FILE
echo 'print(">" * 60)' >> $CONFIG_FILE
cat $CONFIG_FILE
# rename: jupyter_lab_config.py -> jupyter_notebook_config.py
# mv /opt/ASUSCloudInfra/jupyter_lab_config.py /opt/ASUSCloudInfra/jupyter_notebook_config.py
mkdir -p /workspace
export NOTEBOOK_FOLDER=/workspace
jupyter lab --allow-root --no-browser --ip 0.0.0.0 --port 8888 --ServerApp.root_dir="${NOTEBOOK_FOLDER}"
```

```bash
$ pip list | grep jupyter
jupyter-client 7.3.4
jupyter-core 4.11.1
jupyter-server 1.18.1
jupyterlab 3.2.1
jupyterlab-pygments 0.2.2
jupyterlab-server 2.15.0
```
```
/usr# grep -r "jupyter_{name}_config" *
local/lib/python3.8/dist-packages/jupyter_core/migrate.py: - ipython_{notebook,nbconvert,qtconsole}_config.py -> .jupyter/jupyter_{name}_config.py
local/lib/python3.8/dist-packages/jupyter_core/migrate.py: dst_base = pjoin("{jupyter_config}", "jupyter_{name}_config").format(name=name, **env)
Binary file local/lib/python3.8/dist-packages/jupyter_core/__pycache__/migrate.cpython-38.pyc matches
local/lib/python3.8/dist-packages/jupyter_server/extension/application.py: # file, jupyter_{name}_config.
```
