Skip to content

Instantly share code, notes, and snippets.

@tuliocasagrande
Last active August 20, 2024 16:42
Show Gist options
  • Save tuliocasagrande/09e7e7af1c5d9e8393cbf160d3ae7b47 to your computer and use it in GitHub Desktop.
Save tuliocasagrande/09e7e7af1c5d9e8393cbf160d3ae7b47 to your computer and use it in GitHub Desktop.
#!/bin/bash
# -e Exit immediately if a command exits with a non-zero status.
# -u Treat unset variables as an error when substituting.
# -x Print commands and their arguments as they are executed.
set -eux
# This is heavily based on https://github.com/aws-samples/sagemaker-studio-lifecycle-config-examples/blob/main/scripts/install-lsp-features/on-jupyter-server-start.sh
# with a few modifications
# Install:
# - The core JupyterLab LSP integration and whatever language servers you need (omitting autopep8
# and yapf code formatters for Python, which don't yet have integrations per
# https://github.com/jupyter-lsp/jupyterlab-lsp/issues/632)
# - Additional LSP plugins for formatting (black, isort) and refactoring (rope)
# - Spellchecker for markdown cells
# - Code formatting extension to bridge the LSP gap, and supported formatters
echo "Installing jupyterlab-lsp and language tools"
pip install jupyterlab-lsp \
'python-lsp-server[flake8,mccabe,pycodestyle,pydocstyle,pyflakes,pylint,rope]' \
jupyterlab-spellchecker \
jupyterlab-code-formatter black isort
# Other LSP language servers for file editor, not notebooks. For full list of language servers see:
# https://jupyterlab-lsp.readthedocs.io/en/latest/Language%20Servers.html
npm install --save-dev bash-language-server dockerfile-language-server-nodejs pyright
# This configuration override is optional, to make LSP "extra-helpful" by default:
CMP_CONFIG_DIR=.jupyter/lab/user-settings/@krassowski/jupyterlab-lsp/
CMP_CONFIG_FILE=completion.jupyterlab-settings
CMP_CONFIG_PATH="$CMP_CONFIG_DIR/$CMP_CONFIG_FILE"
if test -f $CMP_CONFIG_PATH; then
echo "jupyterlab-lsp config file already exists: Skipping default config setup"
else
echo "Setting continuous hinting to enabled by default"
mkdir -p $CMP_CONFIG_DIR
echo '{ "continuousHinting": true }' >$CMP_CONFIG_PATH
fi
# Similarly can set other configurations. Note:
# - Line width is unfortunately configured separately for several of these plugins.
# - If you enable automatic "formatOnSave" as shown here, you'll see an error dialog when saving a
# notebook with incomplete/invalid Python syntax - but the file should still save.
FMT_CONFIG_DIR=~/.jupyter/lab/user-settings/@ryantam626/jupyterlab_code_formatter
FMT_CONFIG_FILE=settings.jupyterlab-settings
FMT_CONFIG_PATH="$FMT_CONFIG_DIR/$FMT_CONFIG_FILE"
if test -f $FMT_CONFIG_PATH; then
echo "jupyterlab-code-formatter config file already exists: Skipping default config setup"
else
echo "Configuring jupyterlab-code-formatter format on save and line width"
mkdir -p $FMT_CONFIG_DIR
cat >$FMT_CONFIG_PATH <<EOF
{"black": {"line_length": 100}, "isort": {"line_length": 100}, "formatOnSave": true}
EOF
fi
# Configure pycodestyle linter max line width
PYCODESTYLE_CONFIG_DIR=~/.config
PYCODESTYLE_CONFIG_FILE=pycodestyle
PYCODESTYLE_CONFIG_PATH="$PYCODESTYLE_CONFIG_DIR/$PYCODESTYLE_CONFIG_FILE"
if test -f $PYCODESTYLE_CONFIG_PATH; then
echo "pycodestyle config file already exists: Skipping default config setup"
else
echo "Configuring pycodestyle linter max line width"
mkdir -p $PYCODESTYLE_CONFIG_DIR
cat >$PYCODESTYLE_CONFIG_PATH <<EOF
[pycodestyle]
max-line-length = 100
ignore = E303
EOF
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment