Jupyter notebook in the Cloud
While this is a super simple tutorial on how to serve Jupyter notebooks from the cloud, knowledge on how to start and access a Amazon Web Service (AWS) EC2 server is needed.
If you need some tips on that, look here.
Local
Starting a Jupyter notebook locally, i.e. on your laptop, is straightforward. Assuming you have Anaconda installed, all that is needed is to type jupyter notebook
in the terminal and the Jupyter notebook environment pops right up.
In the Cloud
But we sometimes want to have access to a Jupyter notebook environment from any computer, or offer code in a notebook for someone else to experiment with.
This post will explain how to setup a Jupyter notebook on a server, so that we (or someone else) will be able to access a Jupyter notebook environment hosted on a server from a browser anywhere.
Install Anaconda
Download the latest Anaconda installer for Linux
wget http://repo.continuum.io/archive/Anaconda3-5.2.0-Linux-x86_64.sh
Install.
bash Anaconda3-5.2.0-Linux-x86_64.sh
There will be a long series of prompts, just read and a answer yes.
Right at the end, there will be prompt on whether to append the installation you just did to the .bashrc file. Answer yes. This will in the words of a simple Google search …
.bashrc is a shell script that Bash runs whenever it is started interactively. It initializes an interactive shell session. You can put any command in that file that you could type at the command prompt
Reload the .bashrc file
source .bashrc
Check which python version you are using.
which python
It should look something like this
/home/ubuntu/anaconda3/bin/python
Generate password
We need to first generate a password for use later in the configuration files.
ipython
Which will bring you into the python environment.
from IPython.lib import passwd
passwd()
Enter the password you want to use.
Enter password:
Verify password:
Save this output.
Out[3]: ‘sha1:162ab6e2bf89:a1edf51b3be462b635fbb0b6c0c1554000472a71'
Generate configuration file and cert
Create the standard configuration file for Jupyter. We will be saving the output above within this file and making some changes.
jupyter notebook --generate-config
Now we generate a cert for the configuration file as well (for HTTPS access).
mkdir certs
cd certs
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
Now, get into the folder where the configuration file is.
cd ~/.jupyter/
And start editing.
nano jupyter_notebook_config.py
Edit configuration file
Append this to the end of the configuration file.
# Kernel config
c = get_config()
# if you want plotting support always in your notebook
c.IPKernelApp.pylab = u'inline'
c.IPKernelApp.pyplot = u'inline'
# Notebook config
c.NotebookApp.certfile = u'/home/ubuntu/certs/mycert.pem'
#location of your certificate file
c.NotebookApp.ip = '*'
#so that the ipython notebook does not opens up a browser by default
c.NotebookApp.open_browser = False
#the encrypted password we generated above
c.NotebookApp.password = u'sha1:162ab6e2bf89:a1edf51b3be462b635fbb0b6c0c1554000472a71'
# It is a good idea to put it on a known, fixed port
c.NotebookApp.port = 8888
Now make a directory to store your notebooks.
cd ~
mkdir Notebooks
cd Notebooks
Done!
Boot up the Jupyter notebook.
jupyter notebook
To make this Jupyter notebook persist even after get out from the SSH tunnel to the server.
nohup jupyter notebook
To see what was generated in the terminal
lsof nohup.out
To check and kill this process.
kill -9 "PID"
To access it from the browser, you will need to enter something like this. The IP should correspond to what you see in your AWS dashboard for this instance.
https://18.236.86.159:8888