Remote notebooks and Jupyter themes

Sunday, 1 November 2020

Remote notebooks and Jupyter themes

Jupyter notebooks are great. PyCharm is great for writing a module, but Jupyter notebook let's you test snippets of code really easily. You can add a Julia kernel, run bash and JS snippets and add markdown notes. The even greater thing is that you can run them off remote machines. If you have too many notebooks on different machines it gets confusing, but luckily there is jupyter themes that let's you customise the colours. Here are the different colours.

Set up of remote notebooks

NB Before running any notebook remotely it is important that you know whether it is a secure thing to do. Namely, do not route your notebook through your modem ot the web, or worse still assigning a subdomain to it. To server a notebook across your local network you can call it with jupyter notebook --no-browser --ip="*". If it's a remote machine that you ssh into, you can ssh tunnel the port ssh -N -L localhost:9999:localhost:8888 username@remote, the second port number is the remote port (8888 in the example), while the first port is the one you are binding to. Remember that only 4+ digit ports are unpriviledged so you don't need to be root. If you are securing your ssh by obfuscation it by setting the ssh port to something other than 22, say 444, you would add -p 444. The jupyter notebook does not need --ip="*", but it does need to run when you log out of ssh. So either call it with nohup: nohup jupyter notebook --no-browser & (does not require root), the terminal multipler tmux is another option. To kill it call it to the foreground (fg). An important modification for a remote jupyter notebook is setting a password (jupyter notebook password). For a machine you always want to run a jupyter notebook off, say a Rasperry pi, the best option is making it a service. Add using sudo nano /etc/systemd/system/jupyter.service the following
[Unit]
Description=Run jupyer notebook
After=network.target

[Service]
User=pi
WorkingDirectory=/home/pi/
ExecStart=bash /home/pi/.run_jupyter.sh
Restart=always

[Install]
WantedBy=multi-user.target
Don't be fancy as sudo printf or echo dont work first go. Assuming jupyter is in /home/pi/.local/bin/jupyter And then:
printf '#!/bin/bash\n/home/pi/.local/bin/jupyter notebook --no-browser --ip="*"' > .run_jupyter.sh

sudo systemctl start jupyter
#sudo systemctl status jupyter # run this to check.
sudo systemctl enable jupyter
It does not need to be split, but chances are you are going to add something else, like a webhook to slack you its IP address. To do the latter, go to api.slack.com, not the www version, and then create an "App" (it's very straightforward), then make an incoming webhook, which will result in something like:
HOST_IP=$(hostname -I)
PAYLOADSLACK='{"text":"somedescriptive name for your machine '$HOST_IP'"}'
curl -X POST -H 'Content-type: application/json' --data "$PAYLOADSLACK" https://hooks.slack.com/services/👾👾/👾👾/👾👾👾
Where the value of the data attribute contains the command substitution, where hostname -I gives the IP, while hostname gives the network name as defined in network manager (nmcli) so you'd likely write something more meaningful.

Jupyter themes

Jupyter themes is well documented at . It can be installed with pip (pip install jupyterthemes), it is easy to use, but you'll need a few flags (jt -t oceans16 -N -T -kl. However, I have not found a satisfying table showing visually all the themes. Hence why I am making this post.

chesterish

grade3

gruvboxd

gruvboxl

monokai

oceans16

onedork

solarizedd

solarizedl

NB. This theme has a really annoying feature in that selected brackets go white on yellowish white, which is hard to read and gives nasty flashbacks to anyone that has had to pick a white from thousands of off-whites from a Dulux swatch...

No comments:

Post a comment