JupyterHelper/helper.sh

103 lines
3.6 KiB
Bash
Executable File

#!/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 environnement 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,extensionmanager-extension,apputils-extension}
# Setting it to zero doesn't have the expected result, therefore, let's set it to an abnormally large number.
echo '{ "maxNumberOutputs": 10000000000 }' > "$JUPYTERLAB_SETTINGS_DIR"/@jupyterlab/notebook-extension/tracker.jupyterlab-settings
echo '{ "disclaimed": true }' > "$JUPYTERLAB_SETTINGS_DIR"/@jupyterlab/extensionmanager-extension/plugin.jupyterlab-settings
echo '{ "theme": "JupyterLab Dark" }' > "$JUPYTERLAB_SETTINGS_DIR"/@jupyterlab/apputils-extension/themes.jupyterlab-settings
echo '{ "fetchNews": "false" }' > "$JUPYTERLAB_SETTINGS_DIR"/@jupyterlab/apputils-extension/notification.jupyterlab-settings
enable_venv "$2"
python -m pip install --timeout 120 --retries 10000000000 -U pip setuptools
pip install --timeout 120 --retries 10000000000 -r requirements.txt
;;
k|-k|kernel|--kernel)
enable_venv "$2"
mkdir -pv "$R_LIBS_SITE"
if command -v Rscript >> /dev/null; then
Rscript -e 'install.packages("IRkernel",repos="http://cran.r-project.org");library(IRkernel);IRkernel::installspec()'
else
echo "R isn't installed"
exit 1
fi
;;
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 -rfv "$VENV_PATH" "$JUPYTER_ROOT_PATH" "$R_LIBS_SITE" .ipynb_checkpoints __pycache__
;;
l|-l|launch|--launch)
enable_venv "$2"
if command -v jupyter >> /dev/null; then
exec jupyter lab --ip 0.0.0.0 --ServerApp.iopub_data_rate_limit 1e16
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