Skip to content

Instantly share code, notes, and snippets.

@alexhrescale
Last active August 21, 2019 22:51
Show Gist options
  • Save alexhrescale/7767a0cbc2432e8d76cab079a4cb48cb to your computer and use it in GitHub Desktop.
Save alexhrescale/7767a0cbc2432e8d76cab079a4cb48cb to your computer and use it in GitHub Desktop.
working jupyter notebook with bash, jupytext, nbextensions (osx + linux), and ExecuteTime + TOC2 enabled by default
# sets up a working jupyter notebook environment with
# - bash kernel support
# - jupyter_nbextensions_configurator with ExecuteTime enabled
# - jupytext
# tested on
# - CentOS, macos using nix==2.2.2
# - docker://ubuntu:latest using nix==2.1.3
# to connect to a remote host via ssh in bash_kernel, use:
# ssh target-hostname -t 'PS1="[PEXPECT_PROMPT>" bash --norc --noprofile'
with import <nixpkgs> {};
stdenv.mkDerivation rec {
name = "jupyter-env";
env = buildEnv {
name = name;
paths = buildInputs;
};
buildInputs = [
python3
openssl
libxml2
libffi
libxslt
curl # for curl-config
] ++ (if (system == "x86_64-darwin")
then [ darwin.apple_sdk.frameworks.ApplicationServices ]
else []);
shellHook = ''
unset SOURCE_DATE_EPOCH # fix for python building
export PATH=$PATH:${curl.dev}/bin # provides curl-config; not added to path by default
export PYCURL_SSL_LIBRARY=openssl
# shadow $HOME to try to prevent jupyter side effects to/from normal $HOME
export HOME=$PWD
export JUPYTER_CONFIG_DIR=$PWD
function __make-temp-venv() {
export TEMP_VENV=$(mktemp -d --suffix -venv)
echo creating temp virtualenv in $TEMP_VENV...
function cleanup-temp-venv() {
if [ "x$TEMP_VENV" == "x" ]; then
return
fi
echo cleaning up $TEMP_VENV...
rm -rf $TEMP_VENV
}
trap cleanup-temp-venv EXIT
export VIRTUAL_ENV=$TEMP_VENV
}
function setup-venv() {
pip install jupyter==1.0.0 bash_kernel==0.7.2
python -m bash_kernel.install --sys-prefix
pip install jupyter_contrib_nbextensions==0.5.1 jupytext==1.2.1
_JUPYTER_NOTEBOOK_CONFIG=$PWD/jupyter_notebook_config.py
if [ ! -e $_JUPYTER_NOTEBOOK_CONFIG ]; then
jupyter notebook --generate-config
echo 'c.NotebookApp.contents_manager_class="jupytext.TextFileContentsManager"' >> $_JUPYTER_NOTEBOOK_CONFIG
fi
jupyter contrib nbextension install --sys-prefix
jupyter nbextensions_configurator enable --sys-prefix
jupyter nbextension install --py jupyter_nbextensions_configurator --sys-prefix
jupyter nbextension enable jupyter_nbextensions_configurator --py --sys-prefix
if [ ! -e $HOME/nbconfig ]; then
mkdir -p $HOME/nbconfig
echo '{"nbext_hide_incompat": false}' >> $HOME/nbconfig/common.json
echo '{"load_extensions": {"execute_time/ExecuteTime": true, "toc2/main": true}}' >> $HOME/nbconfig/notebook.json
fi
# sanity checks
jupyter serverextension list
jupyter nbextension list
jupyter kernelspec list
}
if [ "x$VIRTUAL_ENV" == "x" ]; then
echo no VIRTUAL_ENV specified
__make-temp-venv
fi
if [ -e $VIRTUAL_ENV/bin/activate ]; then
source $VIRTUAL_ENV/bin/activate
else
python -m venv $VIRTUAL_ENV
source $VIRTUAL_ENV/bin/activate
setup-venv
fi
function start() {
if [ $USER == "root" ]; then
# assume running in docker
jupyter notebook --ip 0.0.0.0 --allow-root --no-browser $*
else
jupyter notebook --no-browser $*
fi
}
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment