Jupyter on Raspberry pi

Jupyter is an excellent free open source package for literate programming, especially in Python. It’s nice because it keeps a good record of what you try in experimental programming. And it’s easy to tidy up your work when you figure out what you’ve done, and store or publish it.

It’s also sweet because it’s a web app. You can program directly from your web browser.

I’ve set it up to use for Raspberry pi programming, to experiment with programs that set GPIO pins and similar device programming.

Here’s what I did to get it working.

First of all, Python programs for device programming must run as root on your pi. So your Jupyter installation must also run as root. (Jupyter grumbles at you about security when you try to run it as root. Ignore that.)

Get a root shell on your pi and run these commands to get python set up. (You probably already have done this.)

apt update
apt install python3-pip python3-dev
pip3 install --upgrade pip
pip3 install jupyter
ipython kernel install

Still with your root shell, make a directory for your Jupyter installation. Notice that we don’t use a python virtual environment for this Jupyter instance, because we need access to the various python site packages.

mkdir ~/jupyter
cd ~/jupyter

Finally, run it. Use –allow-root so it will run as root (reminder: you need to do that if you’ll do pi device programming). If you’re doing this work in a ssh terminal, add –no-browser.  If you’re working on your pi’s desktop, don’t add it; jupyter will launch a browser for you on the desktop.

jupyter notebook --allow-root --no-browser

When you run it, Jupyter puts out a message looking something like this:

The Jupyter Notebook is running at:
http://localhost:8888/?token=7790b65cafredacted5b41redacted8b846bbf

Then, you can set up your ssh client to tunnel port 8888, and use that URL from your browser.

If you want to set up your jupyter instance to be accessible directly you can do these things before you run it.

jupyter notebook --generate-config

Then edit the generated config file. Find three lines and change them so they read like this.

From                                                       To

#c.NotebookApp.allow_remote_access = False    c.NotebookApp.allow_remote_access = True
#c.NotebookApp.allow_root = False             c.NotebookApp.allow_root = True
#c.NotebookApp.ip = 'localhost'               c.NotebookApp.ip = '0.0.0.0'
#c.NotebookApp.open_browser = True            c.NotebookApp.open_browser = False

Then you can simply run

jupyter notebook  --no-browser

Then, use your web browser to visit

http://pi-ip-address:8888

and paste the token value into the form.

Finally,  when you come back to use your notebook after shutting down your pi and restarting it, do these things.

sudo su
cd ~/jupyter
juypter notebook --no-browser --allow-root

 

Leave a Comment