Jupyter notebook is an open-source project that supports interactive data science and scientific computing across programming languages. Notebooks are great to have all your code, text, images, and equations in one single document. This is easy to share, run on other datasets, and customize scripts from a template. This tutorial is on how to setup Jupyter notebooks on a Jetstream instance. For more information on Jetstream, and how to get started here is a previous blog post.
Downloading Jupyter notebooks on Jetstream
Start Ubuntu 18.04 Devel and Docker v1.32 with an instance on Jetstream.
#update the image sudo apt-get update
#make sure python3 and pip3 is installed #in this case, python3 was installed but pip wasn’t. #Installing python3-pip sudo apt-get install python3-pip
#download jupyter notebook sudo apt-get install jupyter pip3 install numpy
#install R and IRkernel sudo apt install r-base sudo R #in the R terminal run the below commands. >install.packages('IRkernel') # To install system-wide, set user to False in the installspec command: >IRkernel::installspec(user = FALSE) #close R and it does not matter if you save workspace or not q() #start jupyter notebook jupyter notebook
#if the above commands were run on the web shell or terminal/putty, then a browser tab will not open right away. To work with jupyter notebook, open the Web Desktop and copy-paste the URL with the token provided (as shown below). Make sure Jupyter notebook command is running or you will lose connection.
Opening Jupyter notebooks locally on your browser instead of Web Desktop
If you are planning to set it up Jupyter notebook on Jetstream but instead of using Web Desktop if you would like to open the notebook on your browser, follow the below steps
#Setup a password for your username on Jetstream, replace username with your username sudo passwd username #enter new password #re-enter the new password #on your local computer, either on terminal ssh -L 8888:localhost:8888 username@VM IP address #enter password just set #this should log you into the VM. #once you are logged in, type the command to start notebook jupyter notebook
Now on your computer
You can copy the link with the token onto your browser and start working on the notebook. Currently, Jupyter notebooks are compatible with Chrome, Safari, and Firefox.
Alternatively, you can also type in https://localhost:8888/tree in the browser, but this will require typing in the token when you first log in to the notebook.
Securing a Jupyter notebook
Generate a notebook configuration file, Jupyter_notebook_config.py, by running the below command.
jupyter notebook --generate-config Writing default config to: /home/username/.jupyter/jupyter_notebook_config.py
By default, the notebook config file has all fields commented out. You can uncomment different options in the config file as necessary to secure the notebook. For more information on these options, follow the documentation here (click here).
Enable JupyterLab extension
JupyterLab is an extension to a notebook with an improved interface and open to developers to add other extensions.
In order to enable this extension, the Jupyter notebook has to be version 4.3 or higher.
#check the jupyter notebook version jupyter notebook --version #install jupyter lab using pip3 pip install jupyterlab #enable jupyter lab extension jupyter serverextension enable --py jupyterlab --sys-prefix #start jupyter lab jupyter lab # should prompt a URL that can be copied to a browser on Web Desktop
If you would like to open jupyter lab on your browser, then run the below commands.
#on your local computer, either on terminal ssh -L 8888:localhost:8888 username@VM IP address #enter password just set #this should log you into the VM.
For more information on securing the jupyter lab, more information is available in this link (click here).
Logging out of Jupyter notebook and lab
You can log out of the jupyter notebook or lab, the option is on the top right corner of your screen. To log back in, you will be required to type in the token that comes with URL when jupyter notebook or lab was started.
Shutting down Jupyter notebook and lab
Closing the browser won’t shut down the notebook, you will also have to kill the command running the terminal. To kill the command select Ctrl+C. This should provide a prompt
Shutdown this notebook server (y/[n])? #type y for yes and n for no
Shutdown this notebook lab (y/[n])? #type y for yes and n for no.
Shoutout to the Jetstream team who answer all our questions and helping us with ssh tunneling to allow opening notebooks locally.