Running Jupyter Notebooks on the Jetson Nano (via https)

Today we’re going to configure Jupyter notebooks so we can connect to our Nano over https. Note, this same logic could easily be applied to a server in the cloud.

Prereqs/Assumptions

  1. You will be doing this for Python3

  2. You already have your default python3 configured for virtualenvs

  3. You have created a virtualenv that we’ll install Jupyter & the Ipython kernel on.

  4. We’re doing this setup while we are sshed into the Nano (ssh <jetson name>@<jetson_ip>)

If you haven’t completed any of the prereqs you can see my previous post on setting up a virtual environment with Pytorch.

The Install (source code)

Rather than just paste a whole bash script I’ll elaborate on it piece by piece for clarity. Also, my script in the source code is slightly different and will require executing it from within a clone of my joys-of-jetson repo for it to work properly.

Activating the virutal enviroment

First thing you need to do is activate the virtualenv you’re going to install Jupyter and Ipython on. We’ll also install some packages that matplotlib requires and another package for parsing json with bash.

# VENV you're using to install with
VENV=pytorch # my virtualenv

workon $VENV && echo "Working on $VENV virtual environment"

echo 'Installing dependencies for matplotlib & json parsing in bash'
sudo apt update \
&& sudo apt install -y libfreetype6-dev pkg-config libpng-dev jq

echo 'Pip installing jupyter, matplotlib, ipython kernal'
echo "which pip3 == $(which pip3)"
pip3 install jupyter matplotlib ipykernel

Creating https certificates

Next, you’ll need to create some certificate files for https authentication. I posted the link to a git issue that came up while installing; the openssl command below is different from the ipython docs because of the git issue so feel free to read about it there.

cd $HOME
mkdir certs
echo "Certificat directory at $HOME/certs"
cd certs
echo "Configuring openssl"|  

# https://github.com/jupyter/notebook/issues/507#issuecomment-145390380
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mycert.pem -out mycert.pem
sudo chown $USER:$USER $HOME/certs/mycert.pem

Notebook configuration

Now we’ll generate our jupyter notebook configuration and update it with our password and a few other settings.

jupyter notebook --generate-config
jupyter notebook password # enter password on promt
sudo chown $USER:$USER $HOME/.jupyter #change ownership for file prepending

JUPYTER_CONFIG_FILE="$HOME/.jupyter/jupyter_notebook_config.py"
# Parses password from json
JUPYTER_PWD=$(sudo jq '.NotebookApp.password' $HOME/.jupyter/jupyter_notebook_config.json)

# Configuration string
JUPYTER_CONFIG_UPDATE=\
"c = get_config()

# Inline plotting
c.IPKernelApp.pylab = 'inline'

# Notebook config
c.NotebookApp.certfile = u'$HOME/certs/mycert.pem' #location of your certificate file
c.NotebookApp.ip = '0.0.0.0'
c.NotebookApp.open_browser = False  #so that the ipython notebook does not opens up a browser by default
c.NotebookApp.password = u$JUPYTER_PWD  #the encrypted password we generated above
# Set the port to 8888
c.NotebookApp.port = 8888

# Configuration file for jupyter-notebook.
c.Notebook.allow_origin='*'

c.InteractiveShellApp.extensions = ['autoreload']
c.InteractiveShellApp.exec_lines = ['%autoreload 2']"

# Concat config string with original file and update config file
sudo printf '%s\n%s\n' "$JUPYTER_CONFIG_UPDATE" "$(sudo cat $JUPYTER_CONFIG_FILE)" > $JUPYTER_CONFIG_FILE

Test it out

After you’ve run all the code above you should be able to start the kernel with jupyter notebook and head over to https://<JETSON_IP>:8888 from your local browser.

Just an FYI, my shell gave me the name of my Nano with the port attached as the Jupyter link (i.e. https://<nano_name>:8888) but I had to fall back to using the actual IP of my Jetson to connect properly.

And there you have it

We’re all ready to crank out some notebooks on the Nano; thanks for reading.

And here are a couple more links that helped bring this together:

  1. https://chrisalbon.com/aws/basics/run_project_jupyter_on_amazon_ec2/
  2. https://www.zaferarican.com/post/transfer-learning-training-on-jetson-nano-with-pytorch

Comments

comments powered by Disqus