Raspberry Pi: Dummy Tutorial on hosting a Jupyter Notebook that you can access anywhere | by JimSpark | Towards Data Science

This series is mainly about setting up your Raspberry Pi with a Jupyter Notebook server that you can access anywhere with open internet. And we are at the final chapter.
In the previous sessions, we have covered how to set up port forwarding or a cloud proxy server in order for you to connect to Raspberry Pi via SSH securely. And in my first article, I have also covered how TCP/IP handles different incoming traffic. Now that we know how to open up our Pi and access it via SSH, we could actually replicate the same idea. In a simple one-liner, Jupyter Notebook runs on HTTP (Which seems apparent since we all use our web browser to use Jupyter Notebook). Therefore, what we need to do is to simply open up a port for HTTP traffic from the Pi, and hook it up with the Jupyter Notebook application.
For the necessary prerequisite, go check out the article below that introduce cloud proxy server setup. I will assume that you already have a cloud proxy server registered at remote.in and are able to connect to your Pi via SSH. I want to highlight that many cloud proxy server providers offer free service for you to access your Raspberry Pi. Since you don’t really rent any server but hosting your own, the services from many providers in general are quite generous. So do check out other providers by yourself!
medium.com
And we want to do this in the most secure way. That’s why this chapter will combine all the things we have learnt so far. Not only are we gonna expose a port on our Raspberry Pi, we are also going to connect the port/Jupyter Notebook app to the cloud proxy server. From the user point of view, nothing is different, they just need to visit another URL instead of the original IP address. But from the server point of view, this gives you great security. And it really matters, since I assume most of you set up your Pi at home, which means your home network will be exposed to attack if anything goes wrong.
Again, this is just for fun. Raspberry Pi doesn’t offer a lot of computing power. But imagine you got a NVIDIA computing box, then it might actually be a good idea to set up remote access. Besides, on the data science industry, using a linux server to do ML stuff is a norm right now. And learning how to set up one just gives your lot of edge and convenience over others.
You could definitely open up the public access to your notebook app via port forwarding. But its really dangerous to do so. Since jupyter notebook generally enables “jupyter magic”, which could create, alter and delete any file on your running os, anyone intentionally visit your jupyter notebook will impose great risk equal to a SSH access.
With all the BS cover, here is the list of steps. Let's do this.
Install Jupyter Notebook on raspberry pi.
Add configuration and set up a password.
Set up an application on cloud proxy server so that you could expose your Jupyter Notebook to open internet.
The current Raspberry Pi release does not include python3-pip. So first we will need to install pip3, then jupyter. SSH into your Raspberry Pi, then run
# install pipsudo apt -y install python3-pip# install jupyterpip3 install jupyter# running pip freeze should show jupyter in the list of packages your installedpip freeze
Jupyter provides a lot of handy tools for us to deal with the setup process. It includes these 3 steps.
1. Create a password and hash it.
A good practice to host a public notebook server is to require a password every time a user visits the notebook. To do that, first, we will need to create a password, and store it securely in the web server (hence the hash part).
To create a password, please run jupyter notebook password,
$ jupyter notebook passwordEnter password:  ****Verify password: ****[NotebookPasswordApp] Wrote hashed password to /Users/you/.jupyter/ jupyter_notebook_config.json
Then, use any editor to open the json, and you should see there is a hash password (start with sha1, see the picture below), please copy it now as we will need to paste it in the configuration file later.
2. Create a configuration file.
Run jupyter notebook --generate-config, a jupyter_notebook_config.py should be generated under /home/USERNAME/.jupyter/jupyter_notebook_config.py.
Use any editor to open the .py file, you should see all setting are commented, so we could simply add in any configuration we needed without conflicting any setting.
3. Add in configuration parameter.
In any line, add in the following configuration.
c.NotebookApp.allow_password_change = Truec.NotebookApp.password = u'your_copied_hash_password'c.NotebookApp.open_browser = Falsec.NotebookApp.port = 8888c.NotebookApp.allow_remote_access = True
allow_password_change: Allows you to change the password once you successfully use the password to login to your notebook server.
password: Copy the password you have just generated, remember to paste the sha hashed password, but not the literal password!
open_browser: Set it to false to prevent web server opening a browser like we normally do in our local machine.
port: The port number notebook server use. You need to remember this as we will need to expose this to the cloud proxy server.
Once done, save the file and the setup is finished! Let’s run jupyter notebook to host a notebook on the Raspberry Pi!
Most of the content covered here are on the official documentation.
In a nutshell, what a cloud proxy server does is to listen to the port you pointed to, and host a public server that you could access anywhere from open internet. Whenever you access that public server, the only thing it does is to forward your request to the application of the open port. In this case, jupyter notebook is running on port 8888 with HTTP protocol. Which means when a user visits the public cloud proxy server, the HTTP get request will be forward to your Pi at port 8888, which return the notebook website to the user.
Assuming you have a remote server set up at remote.in, we can add an HTTP application by visiting http://find.remote.it/. Select your Raspberry Pi device and you should be in the dashboard, go to setting and select your Pi there.
If you need some quick help to set up the cloud proxy server in your Raspberry Pi, the best resource is the quickstart guide from the official gitbook, or my last article that cover the setup.
Then, click Add Manually >, and fill in the new service. You could fill in any name you prefer, with a Type set to HTTP, port set to 8888 (or any port that you choose to use in your jupyter config).
Then the setup is finally finished! Now visit https://app.remote.it/, and select your Pi device, click on the service you just created.
And it should return a public URL where you can access.
Visit this website and you will access your jupyter notebook! This is not limited to the same LAN, you could literally access it anywhere from open internet now!
And this wraps up the whole series. We covered how to install a Raspberry Pi OS, setting up SSH, cloud proxy server and finally a Jupyter Notebook server. The same methodology also stands for setting up a website and other IDEs like visual studio code (technically there is a compiling issue when setting up a visual studio code as of today, which we can't really resolve by ourselves. But this is definitely possible in the future!). So do try different things out, google it and test your skills.
If you have any other side projects you would like some help on, let me know in the comment session. Thank you for reading this series.