Skip to content

Instantly share code, notes, and snippets.

@mokztk
Last active April 26, 2023 00:01
Show Gist options
  • Save mokztk/e43a5e6039a8cfe83d0bf968dc33f976 to your computer and use it in GitHub Desktop.
Save mokztk/e43a5e6039a8cfe83d0bf968dc33f976 to your computer and use it in GitHub Desktop.
jupyter/r-notebook:4.2.2 をベースに日本語環境、Language serverなどを導入したもの。

About this image

jupyter/r-notebook:4.2.2 に日本語環境と自動補完の jupyterlab-lsp ほかを導入したもの

  • 日本語ロケール他:Ubuntu の language-pack-ja-base パッケージ
  • TZ は Asia/Tokyo に変更
  • 日本語フォントとして Noto Sans/Serif CJK JP:fonts-noto-cjk
  • 日本語を含むノートブックをPDFエクスポートできるよう日本語TeX環境:texlive-lang-japanese
    • XeLaTeX + Bxjsarticle + Zxjafont で Noto CJK JP フォントを使用するようテンプレートを修正
      • /opt/conda/share/jupyter/nbconvert/templates/latex/index.tex.j2
      • IPA (ex|) Gothic/Mincho も導入されているので、jafont=ipa or jafont=ipaex とすれば IPAフォントも可
    • ctexhook.sty 他の不足に対して、texlive-lang-chinese を追加
      • 以前は出なかった LaTeX Error: File 'ctexhook.sty' not found. エラーが出るようになったことへの対策
  • 対応するカッコを自動で閉じる、コードセルの行番号、日本語表示などはイメージ作成時に予め設定しておく
    • /opt/conda/share/jupyter/lab/settings/overrides.json がうまく反映されない?
    • overrides.json より優先される ~/.jupyter/lab/user-settings/\@jupyterlab 以下の個人設定を使用

起動時の設定

  • Jupyter Lab をパスワードなしで起動する
    • jupyter/r-notebook:r-4.2.2 はもともと Jupyter Lab が起動される
    • パスワードなしとするため、start.sh jupyter lab --LabApp.token='' で起動(docker-compose.yml)
version: "3"
services:
jupyter:
image: "mokztk/r-notebook:r-4.2.2"
container_name: "jupyter"
command: >
start.sh
jupyter lab
--no-browser
--LabApp.token=''
ports:
- "8888:8888"
volumes:
- .:/home/jovyan/work
# customize jupyter/r-notebook
FROM jupyter/r-notebook:r-4.2.2
# Ubuntuミラーサイトの設定
# x86_64 の場合は日本のミラーサーバーで一番回線が太い ICSCoE(IPA産業サイバーセキュリティセンター)に変更
USER root
RUN if [ `uname -m` = "x86_64" ]; then \
sed -i.bak -e "s%http://[^ ]\+%http://ftp.udx.icscoe.jp/Linux/ubuntu/%g" /etc/apt/sources.list; \
fi
# 日本語環境の導入(ロケール、TimeZone、フォント、TeX環境)
RUN set -x \
&& apt-get update \
&& apt-get install -y --no-install-recommends \
language-pack-ja-base \
fonts-noto-cjk \
texlive-lang-japanese \
texlive-lang-chinese \
&& /usr/sbin/update-locale LANG=ja_JP.UTF-8 LANGUAGE="ja_JP:ja" \
&& /bin/bash -c "source /etc/default/locale" \
&& ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
USER ${NB_UID}
RUN set -x \
# Jupyter Lab extensions のインストール
&& mamba install -y \
jupyterlab-language-pack-ja-jp \
jupyter-lsp \
jupyterlab-lsp \
nodejs \
python-lsp-server \
# R Lanugage server は linux-arm64 版が conda-forge にないので CRAN からインストール
&& echo "options(repos = c(CRAN='https://cloud.r-project.org'))" > ~/.Rprofile \
&& Rscript -e "install.packages('languageserver')" \
# R の追加パッケージ from conda-forge
&& mamba install -y \
r-pacman \
r-here \
r-tidylog \
r-proc \
r-cmprsk \
r-psych \
r-clinfun \
r-car \
r-survminer \
r-ggally \
r-ggrepel \
r-ggsci \
r-patchwork \
r-gridextra \
r-gt \
r-gtsummary \
r-flextable \
r-palmerpenguins \
# 上記のうち、arm64 で conda-forge から入らないもの(上をコメントアウトしてこちらを有効に)
# && Rscript -e "install.packages(c('cmprsk', 'clinfun', 'car', 'survminer', 'ggrepel'))" \
# Python の追加パッケージ
&& mamba install -y \
pandas \
seaborn \
# その他のツール類
&& mamba install -y \
jupytext \
# cleaning
&& mamba clean --all -f -y \
&& fix-permissions "${CONDA_DIR}" \
&& fix-permissions "/home/${NB_USER}"
# 日本語を含む文書をPDFエクスポートできるよう、TeX template を上書き
# Bxjsarticle, Zxjafont で Noto CJK fonts を使用する設定
# ref: https://home.hirosaki-u.ac.jp/heroic-2020/1781/
# https://github.com/zr-tex8r/ZXjafont/blob/master/zxjafont.pdf
COPY --chown=${NB_UID}:${NB_GID} index.tex.j2 /opt/conda/share/jupyter/nbconvert/templates/latex/
# Jupyter Labの設定を上書き(テーマ、日本語表示の有効化、など)
COPY --chown=${NB_UID}:${NB_GID} jupyterlab-settings.sh /home/${NB_USER}
RUN chmod +x /home/${NB_USER}/jupyterlab-settings.sh \
&& /home/${NB_USER}/jupyterlab-settings.sh
ENV LANG=ja_JP.UTF-8 \
LANGUAGE=ja_JP.UTF-8 \
LC_ALL=ja_JP.UTF-8 \
TZ=Asia/Tokyo
((=- Default to the notebook output style -=))
((*- if not cell_style is defined -*))
((* set cell_style = 'style_jupyter.tex.j2' *))
((*- endif -*))
((=- Inherit from the specified cell style. -=))
((* extends cell_style *))
%===============================================================================
% Latex Article
%===============================================================================
((*- block docclass -*))
%\documentclass[11pt]{article}
\documentclass[a4paper,xelatex,ja=standard,jafont=noto,nothreeweight]{bxjsarticle}
((*- endblock docclass -*))
#!/bin/bash
# JupyterLab の各種設定(overrides.json より優先される)
## 言語設定
mkdir -p .jupyter/lab/user-settings/\@jupyterlab/translation-extension/
cat << EOF > .jupyter/lab/user-settings/\@jupyterlab/translation-extension/plugin.jupyterlab-settings
{
"locale": "ja_JP"
}
EOF
## テーマ設定
mkdir -p .jupyter/lab/user-settings/\@jupyterlab/apputils-extension/
cat << EOF > .jupyter/lab/user-settings/\@jupyterlab/apputils-extension/themes.jupyterlab-settings
{
"theme": "JupyterLab Dark",
"overrides": {
"content-font-size1": "18px",
"code-font-size": "16px"
}
}
EOF
## コード記載部分:カッコを自動で閉じる設定、タブ、行番号など
mkdir -p .jupyter/lab/user-settings/\@jupyterlab/notebook-extension/
cat << EOF > .jupyter/lab/user-settings/\@jupyterlab/notebook-extension/tracker.jupyterlab-settings
{
"codeCellConfig": {
"autoClosingBrackets": true,
"lineNumbers": true,
"matchBrackets": true,
"insertSpaces": true,
"tabSize": 4
},
"markdownCellConfig": {
"autoClosingBrackets": true,
"lineNumbers": true,
"matchBrackets": true,
"insertSpaces": true,
"tabSize": 4
}
}
EOF
mkdir -p .jupyter/lab/user-settings/\@jupyterlab/console-extension/
cat << EOF > .jupyter/lab/user-settings/\@jupyterlab/console-extension/tracker.jupyterlab-settings
{
"promptCellConfig": {
"autoClosingBrackets": true,
"matchBrackets": true,
"insertSpaces": true,
"tabSize": 4
}
}
EOF
@mokztk
Copy link
Author

mokztk commented Mar 24, 2023

  • R Language server を conda-forge ではなく CRAN から導入するようにしたので、ARM版でも使用可能のはず
    • 以前と異なり、ARM版も jupyter/r-notebook のタグどおりのバージョンの R がインストールされている
  • PDF以外の形式でノートブックをエクスポートすると日本語部分がみえない(要調査)
  • Rがメインなので、code cell の tabSize は 2 の方がいいかも

@mokztk
Copy link
Author

mokztk commented Mar 29, 2023

[2023-03-29] ARM64 の Chromebook 実機で docker image build してエラーが出た箇所を修正

@mokztk
Copy link
Author

mokztk commented Apr 26, 2023

[2023-04-26] Rmd <-> ipynb の変換用に jupytext を追加、R の {here} {pacman} を追加

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment