Created
September 13, 2023 17:32
-
-
Save barronh/b4bb6b894cf16b214d892b094dae16e2 to your computer and use it in GitHub Desktop.
Add a Kernel in a Jupyter Environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
def_yes=n | |
while getopts ":hy" opt; do | |
case ${opt} in | |
h ) # process option h | |
PRINT_HELP=y | |
;; | |
y ) # process option y | |
def_yes=y | |
;; | |
\? ) | |
PRINT_HELP=y | |
;; | |
esac | |
done | |
if [[ $# == 0 || $PRINT_HELP == "y" ]]; then | |
echo "Usage $0 [-h] [-y] ENV_DIR [ENV_NAME [REQUIREMENTS]]" | |
echo " Install a Python virtual environment for use as a Jupyter Kernel" | |
echo "" | |
echo " -h : print usage help" | |
echo " -y : answer yes to confirmation prompt" | |
echo " ENV_DIR : required path to create a new environment" | |
echo " ENV_NAME : optional str for the name of the kernel" | |
echo " REQUIREMENTS : optional path for required libraries." | |
echo | |
echo "PYTHON environment variable influences which version is used" | |
echo "By default, this uses python3.6 to be consistent with RHEL7" | |
echo "JupyterHub on Atmos. On RHEL8, update to python3.8" | |
echo | |
echo "PYTHON=/usr/bin/python3.6" | |
exit | |
fi | |
shift $(($OPTIND - 1)) | |
ENV_DIR=${1?Required ENV_DIR of where to make your new kernel} | |
ENV_NAME_DEF=$(basename ${ENV_DIR}) | |
ENV_NAME=${2:-${ENV_NAME_DEF}} | |
REQPATH=${3} | |
PYTHON=${PYTHON:=/usr/bin/python3.6} | |
echo "Create ${ENV_NAME} at $ENV_DIR with ${PYTHON}" | |
if [[ ${REQPATH} != "" ]]; then | |
echo "and packages in ${REQPATH}" | |
fi | |
if [[ $def_yes == y ]]; then | |
REPLY=y | |
else | |
read -p "Are you sure? " -n 1 -r | |
fi | |
echo | |
# Optionally, modify environment before running | |
# module purge | |
# module load netcdf-4.8.1/intel-19.0 | |
# module load hdf5-1.10.8/intel-19.0 | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
# Create a "virtual environment" called "tempo_env" for this training | |
${PYTHON} -m venv ${ENV_DIR} | |
# Upgrade pip | |
${ENV_DIR}/bin/python -m pip install --upgrade pip | |
# Install ipykernel | |
${ENV_DIR}/bin/python -m pip install ipykernel | |
# Install optional additional libraries | |
if [[ ${REQPATH} != "" ]]; then | |
${ENV_DIR}/bin/python -m pip install -r $3 | |
fi | |
# Register kernel | |
${ENV_DIR}/bin/python -m ipykernel install --user --name=${ENV_NAME} | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Download and run. For more details on how, run
bash ./add_kernel.sh -h