From 55a4179ecf1bcc00f57f1a22fe8349d28907b7f8 Mon Sep 17 00:00:00 2001 From: JP Ungaretti <19893438+jungaretti@users.noreply.github.com> Date: Wed, 11 May 2022 12:11:43 -0700 Subject: [PATCH] Add option to create JupyterLab config for user (#1439) * Copy config to Jupyter directory * Create config in Jupyter script * Copy changes to script-library --- .../.devcontainer/base.Dockerfile | 2 +- .../library-scripts/jupyterlab-debian.sh | 17 +++++++++++++++++ script-library/jupyterlab-debian.sh | 17 +++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/containers/codespaces-linux/.devcontainer/base.Dockerfile b/containers/codespaces-linux/.devcontainer/base.Dockerfile index 488f3e641e..e7c92c0781 100644 --- a/containers/codespaces-linux/.devcontainer/base.Dockerfile +++ b/containers/codespaces-linux/.devcontainer/base.Dockerfile @@ -72,7 +72,7 @@ RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ RUN bash /tmp/scripts/python-debian.sh "none" "/opt/python/latest" "${PIPX_HOME}" "${USERNAME}" "true" \ # Install JupyterLab and common machine learning packages && PYTHON_BINARY="${PYTHON_ROOT}/current/bin/python" \ - && bash /tmp/scripts/jupyterlab-debian.sh "latest" "automatic" ${PYTHON_BINARY} \ + && bash /tmp/scripts/jupyterlab-debian.sh "latest" "automatic" ${PYTHON_BINARY} "true" \ && bash /tmp/scripts/setup-python-tools.sh ${PYTHON_BINARY} \ # Install rvm, rbenv, any missing base gems && chown -R ${USERNAME} /opt/ruby/* \ diff --git a/containers/codespaces-linux/.devcontainer/library-scripts/jupyterlab-debian.sh b/containers/codespaces-linux/.devcontainer/library-scripts/jupyterlab-debian.sh index f9f1f886e1..f6c93f9c6c 100755 --- a/containers/codespaces-linux/.devcontainer/library-scripts/jupyterlab-debian.sh +++ b/containers/codespaces-linux/.devcontainer/library-scripts/jupyterlab-debian.sh @@ -14,6 +14,7 @@ set -e VERSION=${1:-"latest"} USERNAME=${2:-"automatic"} PYTHON=${3:-"python"} +ALLOW_ORIGIN=${4:-""} # If in automatic mode, determine if a user already exists, if not use vscode if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then @@ -43,6 +44,18 @@ sudoUserIf() { fi } +addToJupyterConfig() { + JUPYTER_DIR="/home/${USERNAME}/.jupyter" + JUPYTER_CONFIG="${JUPYTER_DIR}/jupyter_notebook_config.py" + + # Make sure the config file exists + test -d ${JUPYTER_DIR} || sudoUserIf mkdir ${JUPYTER_DIR} + test -f ${JUPYTER_CONFIG} || sudoUserIf touch ${JUPYTER_CONFIG} + + # Don't write the same line more than once + grep -q ${1} ${JUPYTER_CONFIG} || echo ${1} >> ${JUPYTER_CONFIG} +} + # Make sure that Python is available if ! ${PYTHON} --version > /dev/null ; then echo "You need to install Python before installing JupyterLab." @@ -56,3 +69,7 @@ if [ "${VERSION}" = "latest" ]; then else sudoUserIf ${PYTHON} -m pip install jupyterlab=="${VERSION}" --no-cache-dir fi + +if [ "${ALLOW_ORIGIN}" = 'true' ]; then + addToJupyterConfig "c.ServerApp.allow_origin = '*'" +fi diff --git a/script-library/jupyterlab-debian.sh b/script-library/jupyterlab-debian.sh index f9f1f886e1..f6c93f9c6c 100755 --- a/script-library/jupyterlab-debian.sh +++ b/script-library/jupyterlab-debian.sh @@ -14,6 +14,7 @@ set -e VERSION=${1:-"latest"} USERNAME=${2:-"automatic"} PYTHON=${3:-"python"} +ALLOW_ORIGIN=${4:-""} # If in automatic mode, determine if a user already exists, if not use vscode if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then @@ -43,6 +44,18 @@ sudoUserIf() { fi } +addToJupyterConfig() { + JUPYTER_DIR="/home/${USERNAME}/.jupyter" + JUPYTER_CONFIG="${JUPYTER_DIR}/jupyter_notebook_config.py" + + # Make sure the config file exists + test -d ${JUPYTER_DIR} || sudoUserIf mkdir ${JUPYTER_DIR} + test -f ${JUPYTER_CONFIG} || sudoUserIf touch ${JUPYTER_CONFIG} + + # Don't write the same line more than once + grep -q ${1} ${JUPYTER_CONFIG} || echo ${1} >> ${JUPYTER_CONFIG} +} + # Make sure that Python is available if ! ${PYTHON} --version > /dev/null ; then echo "You need to install Python before installing JupyterLab." @@ -56,3 +69,7 @@ if [ "${VERSION}" = "latest" ]; then else sudoUserIf ${PYTHON} -m pip install jupyterlab=="${VERSION}" --no-cache-dir fi + +if [ "${ALLOW_ORIGIN}" = 'true' ]; then + addToJupyterConfig "c.ServerApp.allow_origin = '*'" +fi