Jupyter Notebook

This article provides some pratical skills for using Jupyter Notebook.

Check Python Version Within Jupyter Notebook:

import platform
print(platform.python_version())

Modes and Commands

There are two modes of selection when inside a Jupyter Notebook:
1. Command Mode – When you hit up/down arrows you select different cells. Hit enter to enter edit mode.
1. Edit Mode – You can edit the cell. Hit Esc to enter Command Mode again.

In Command Mode (cell highlighted blue):

h - bring up help window (contains full list of shortcuts!)
<enter> - Enter Edit Mode
a - create new cell above selected
b - create cell below selected
d, d - delete selected cell

In Edit Mode (cell highlighted green):

<esc> - Enter Command Mode
<shift> + <enter> - Run cell and move to cell below in Command Mode
<ctrl> + <enter> - Run cell in place

Magic Commands

The %connect_info line magic displays the information necessary to connect another frontend to the Notebook kernel.

%connect_info

The command will output:

{
  "shell_port": 52141,
  "iopub_port": 50475,
  "stdin_port": 56387,
  "control_port": 33193,
  "hb_port": 54963,
  "ip": "127.0.0.1",
  "key": "502fde53-83868f35653ra15a29452d7",
  "transport": "tcp",
  "signature_scheme": "hmac-sha256",
  "kernel_name": ""
}

Paste the above JSON into a file, and connect with:
    $> jupyter <app> --existing <file>
or, if you are local, you can connect with just:
    $> jupyter <app> --existing kernel-bb59f470-8044-4573-8ea1-680dc6277df3.json
or even just:
    $> jupyter <app> --existing
if this is the most recent Jupyter kernel you have started.

Other magic commands: https://ipython.readthedocs.io/en/stable/interactive/magics.html

Access Jupyter Notebook Remotely

  • First, make sure you install Jupyter notebook in both remote (working station in your office) and local (your home computer).
  • In remote host, open the terminal, change directory to where you have your notebooks and type:
jupyter notebook --no-browser --port=8889
  • In your local computer, open Powershell (if using Windows) or Unix/Linux terminal, then type:
ssh -N -f -L localhost:8888:localhost:8889 username@your_remote_host_name
  • Now open web browser in local machine and type in:
http://localhost:8888

Reference: https://amber-md.github.io/pytraj/latest/tutorials/remote_jupyter_notebook

Remote Jupyter Note with Security

We can use jupyter notebook with SSL and password:

First, we need to configure the Jupyter Server:

1.Set up password:

jupyter notebook password

Input your password, and the output will be as follows:

Enter password:
Verify password:
[NotebookPasswordApp] Wrote hashed password to /home/ubuntu/.jupyter/jupyter_notebook_config.json

2.Create a self-signed SSL certificate:

$ cd ~
$ mkdir ssl
$ cd ssl
$ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mykey.key -out mycert.pem

Follow the prompts to fill out your locality as you see fit. You must enter . if you wish to leave a prompt blank. Your answers will not impact the functionality of the certificate.

Then, we can start the jupyter server now:

 jupyter notebook --certfile=~/ssl/mycert.pem --keyfile ~/ssl/mykey.key

Finally, we can access the SSL powered jupyter notebook server by:

ssh -i ~/mykeypair.pem -N -f -L 8888:localhost:8888 ubuntu@xxx.xxx.xxx.xxx

We shall be able open the jupyter notebook in our broswer now:

https://localhost:8888

You may need to enable non-secure connection for it.

Reference: AWS – Set up a Jupyter Notebook Server