From 6b6bb902b68e00631e0b6c3979187ce2c2122fbf Mon Sep 17 00:00:00 2001 From: saundersp Date: Tue, 22 Aug 2023 09:43:49 +0200 Subject: [PATCH] Added files --- .gitattributes | 3 ++ helper.sh | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 .gitattributes create mode 100644 helper.sh diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..ed670ec --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +# Set line endings to LF, even on Windows. Otherwise, execution within Docker fails. +# See https://help.github.com/articles/dealing-with-line-endings/ +*.sh text eol=lf diff --git a/helper.sh b/helper.sh new file mode 100644 index 0000000..9d7d76c --- /dev/null +++ b/helper.sh @@ -0,0 +1,93 @@ +#!/usr/bin/env bash + +USAGE="Jupyterlab portable helper +Implemented by Pierre Saunders, 2023 + +USAGE: $0 FLAG +Available flags: + i, -i, install, --install Install Jupyterlab locally to the script. + k, -k, kernel, --kernel Install the R kernel extension to jupyter + u, -u, update, --update Update every python packages of the local virtual environnement. + U, -U, uninstall, --uninstall Uninstall Jupyterlab locally to the script. + l, -l, launch, --launch Launch Jupyterlab locally to the script (Require installation beforehand). + t, -t, tensorboard, --tensorboard Launch Tensorboard locally to the script (Require installation beforehand). + h, -h, help, --help Which display this help message." + +VENV_PATH=./venv +JUPYTER_ROOT_PATH=./jupyter +TENSORBOARD_LOG_DIR=./logs +export R_LIBS_SITE=./libs +export JUPYTER_ALLOW_INSECURE_WRITES=0 +export JUPYTERLAB_SETTINGS_DIR="$JUPYTER_ROOT_PATH"/settings +export JUPYTERLAB_WORKSPACES_DIR="$JUPYTER_ROOT_PATH"/workspace +export JUPYTER_CONFIG_DIR="$JUPYTER_ROOT_PATH"/config +export JUPYTER_RUNTIME_DIR="$JUPYTER_ROOT_PATH"/runtime +export JUPYTER_DATA_DIR="$JUPYTER_ROOT_PATH"/data + +enable_venv(){ + test ! -d "$VENV_PATH" && echo 'Python virtual envrionnement not installed' && exit 1 + + case "$1" in + --windows|-W) ENV_PATH="$VENV_PATH"/Scripts/activate ;; + '') ENV_PATH="$VENV_PATH"/bin/activate ;; + *) echo 'Invalid selected OS' && exit 1 ;; + esac + + test ! -f "$ENV_PATH" && echo 'Installation is corrupted, please reinstall' && exit 1 + . "$ENV_PATH" +} + +case "$1" in + i|-i|install|--install) + python -m venv "$VENV_PATH" + mkdir -p "$JUPYTER_ROOT_PATH"/{settings,workspace,config,runtime,data} "$JUPYTERLAB_SETTINGS_DIR"/@jupyterlab/notebook-extension + echo '{ "maxNumberOutputs": 0 }' > "$JUPYTERLAB_SETTINGS_DIR"/@jupyterlab/notebook-extension/tracker.jupyterlab-settings + enable_venv "$2" + python -m pip install --timeout 60 --retries 9999999999999999 -U pip setuptools + pip install --timeout 60 --retries 9999999999999999 -r requirements.txt + ;; + + k|-k|kernel|--kernel) + enable_venv "$2" + mkdir -p "$R_LIBS_SITE" + Rscript -e 'install.packages("IRkernel",repos="http://cran.us.r-project.org");library(IRkernel);IRkernel::installspec()' + ;; + + c|-c|check|--check) + enable_venv "$2" + pip check + ;; + + u|-u|update|--update) + enable_venv "$2" + python -m pip install -U pip + pip freeze | xargs -n1 pip install -U + ;; + + U|-U|uninstall|--uninstall) + rm -rf "$VENV_PATH" "$JUPYTER_ROOT_PATH" "$R_LIBS_SITE" + ;; + + l|-l|launch|--launch) + enable_venv "$2" + if command -v jupyter >> /dev/null; then + exec jupyter lab + else + echo "Jupyter lab isn't installed" + exit 1 + fi + ;; + + t|-t|tensorboard|--tensorboard) + enable_venv "$2" + if command -v tensorboard >> /dev/null; then + tensorboard --logdir="$TENSORBOARD_LOG_DIR" + else + echo "Tensorboard isn't installed" + exit 1 + fi + ;; + + h|-h|help|--help) echo "$USAGE" ;; + *) echo "$USAGE" && exit 1 ;; +esac