Skip to content

Instantly share code, notes, and snippets.

@jpbarto
Last active June 10, 2020 15:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpbarto/b61edc8ae4e52b72381188f27c7e59d9 to your computer and use it in GitHub Desktop.
Save jpbarto/b61edc8ae4e52b72381188f27c7e59d9 to your computer and use it in GitHub Desktop.
Default lifecycle configuration for SageMaker notebooks
#!/bin/bash
#
# This script takes about 5 minutes to run and will delay the availability of
# your SageMaker notebook.
# v1.3, release 05may2020
#
# THIS SCRIPT INSTALLS:
# - THEIA IDE w/ Jupyter Server proxy
# - UPGRADES JUPYTERLAB
# - INSTALL VOILA (https://github.com/voila-dashboards/voila)
# - INSTALL S3 BROWSER (https://github.com/IBM/jupyterlab-s3-browser)
# - INSTALL JUPTER INTEGRATION WITH PYTHON LANGUAGE SERVER (https://github.com/krassowski/jupyterlab-lsp)
#
####
set -e
sudo -u ec2-user -i <<'EOP'
###############################
#
# INSTALL THEIA IDE
#
###############################
echo INSTALLING THEIA
mkdir ~/theia && cd ~/theia
### begin by installing NVM, NodeJS v10, and Yarn
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash
source ~/.nvm/nvm.sh
nvm install 10
nvm use 10
npm install -g yarn
# now compile Theia-IDE from source, retrieving the configuration package.json from GitHub
curl https://raw.githubusercontent.com/jpbarto/amazon-sagemaker-notebook-instance-lifecycle-config-samples/master/scripts/install-theia-ide/package.json -o ${HOME}/theia/package.json
# the build can take a while so detach it from this process and let it run in the background
nohup yarn &
### Configure Theia defaults
THEIA_PATH=$PATH
mkdir ~/.theia
mkdir -p ~/SageMaker/.theia
curl https://raw.githubusercontent.com/jpbarto/amazon-sagemaker-notebook-instance-lifecycle-config-samples/master/scripts/install-theia-ide/launch.json -o ${HOME}/SageMaker/.theia/launch.json
cat >${EC2_HOME}/.theia/settings.json <<EOS
{
"workbench.iconTheme": "theia-file-icons",
"terminal.integrated.inheritEnv": true,
"python.linting.pylintEnabled": true,
"python.linting.flake8Enabled": true,
"python.linting.pycodestyleEnabled": true,
"python.linting.enabled": true
}
EOS
###############################
#
# UPDATE JUPYTER AND EXTENSIONS
#
###############################
echo CONFIGURING NOTEBOOK SERVER
source ~/anaconda3/bin/activate JupyterSystemEnv
echo UPGRADING JUPYTERLAB
# UPGRADE JUPYTERLAB, VOILA, IPYWIDGETS, S3 BROWSER, PYTHON LANGUAGE SERVER, AND SERVER PROXY
pip install --upgrade pip
pip install -U jupyterlab
pip install -U voila ipywidgets \
jupyterlab-s3-browser python-language-server[all] \
python-jsonrpc-server jupyter-lsp \
jupyter-server-proxy pylint autopep8 yapf pyflakes pycodestyle
jupyter labextension update --all
## CONFIGURE JUPYTER PROXY TO MAP TO THE THEIA IDE
JUPYTER_ENV=~/anaconda3/envs/JupyterSystemEnv
cat >>${JUPYTER_ENV}/etc/jupyter/jupyter_notebook_config.py <<EOC
c.ServerProxy.servers = {
'theia': {
'command': ['yarn', '--cwd', '/home/ec2-user/theia', 'start', '/home/ec2-user/SageMaker', '--port', '{port}'],
'environment': {'PATH': '${THEIA_PATH}'},
'absolute_url': False,
'timeout': 30
}
}
EOC
echo INSTALLING LAB EXTENSIONS
jupyter labextension install @jupyter-voila/jupyterlab-preview jupyterlab-s3-browser @krassowski/jupyterlab-lsp @jupyterlab/server-proxy
jupyter serverextension enable --py --sys-prefix jupyter_server_proxy
# CONFIGURE JUPYTER LAB DARK MODE
echo CONFIGURE LAB UI
mkdir -p ~/.jupyter/lab/user-settings/\@jupyterlab/apputils-extension
cat >~/.jupyter/lab/user-settings/\@jupyterlab/apputils-extension/themes.jupyterlab-settings <<EOF
{
"theme": "JupyterLab Dark"
}
EOF
source ~/anaconda3/bin/deactivate
echo NOTEBOOK CONFIGURATION COMPLETE
EOP
## RESTART THE JUPYTER SERVER
initctl restart jupyter-server --no-wait
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment