Skip to content

Instantly share code, notes, and snippets.

@davclark
Created March 23, 2016 18:35
Show Gist options
  • Save davclark/573cfd28620579eee88e to your computer and use it in GitHub Desktop.
Save davclark/573cfd28620579eee88e to your computer and use it in GitHub Desktop.
The steps to set up Jupyter notebooks to run nicely on a remote server. Please feel free to make your own mods!
#!/usr/bin/bash
# XXX The below was made before the jupyter switch and would be nice to
# update! For now, you can run this *before* ever running the jupyter notebook,
# OR you can `rm ~/.jupyter/migrated` and move any existing `jupyter_notebook_config.py`
# after you run this script and it should re-import. -DJC
# Make it easy to set up password / encryption for IPython notebook
echo "
This script will update your default ipython notebook profile.
It will be configured to use the SSL certificate it creates in the
current directory.
Please type a password to use for the notebook server:"
sha_str=$(python3 -c \
'from IPython.lib import passwd; print(passwd(input()))' )
# Just for the vertical space
echo
# Create the ipython profile_default
ipython profile create
# There's probably a slightly better way to get this filename
cfile=~/.ipython/profile_default/ipython_notebook_config.py
cat >> $cfile <<EOF
# Automatically added by setup_ipython_notebook.sh
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 9999
c.NotebookApp.certfile = u'$PWD/ipython.pem'
# Necessary for some versions of Tornado?
c.NotebookApp.keyfile = u'$PWD/ipython.pem'
c.NotebookApp.password = u'$sha_str'
EOF
# Create the ipython profile_default
ipython profile create scala
sfile=~/.ipython/profile_default/ipython_notebook_config.py
cat >> $sfile <<EOF
# Automatically added by setup_ipython_notebook.sh
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 9999
c.NotebookApp.certfile = u'$PWD/ipython.pem'
# Necessary for some versions of Tornado?
c.NotebookApp.keyfile = u'$PWD/ipython.pem'
c.NotebookApp.password = u'$sha_str'
EOF
echo "Added config to end of these files:
$cfile
$sfile
Generating SSL certificate. Answer questions however you like.
(Hit enter to continue)"
# We don't care what they type, as long as they hit enter
read
# Generate a reasonable SSL cert
openssl req -x509 -nodes -days 365 -newkey rsa:1024 \
-keyout ipython.pem -out ipython.pem
# Thanks internet!
myip=$(dig +short myip.opendns.com @resolver1.opendns.com)
echo "
The following line should now run the notebook on port 9999
\$ ipython notebook
To modify this behavior (including if you move ipython.pem) edit:
$cfile
You'll also need to open TCP access from your web browser's IP. For example,
using Amazon's EC2 console, click on the security group for your instance and
add an incoming rule for port 9999.
Then, you should be able to point your (local) web browser to:
https://$myip:9999/
You'll probably need to click through security warnings in Chrome each time.
Firefox should let you save the certificate for future usage.
Your password will be the one you typed above. It's in cleartext, so you can
check if you've forgotten already!
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment