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 and nobody ever trusts EOF commands without doublechecking. Say jupyter is in /home/pi/.local/bin/jupyter (installed without sudo pip):
printf '#!/bin/bash\n\n/home/pi/.local/bin/jupyter notebook --no-browser --ip="*"\n' > .run_jupyter.sh
Then you can start and enable it with:

sudo systemctl start jupyter
sudo systemctl enable jupyter
If it failed you can check what happened with either:

# short
sudo systemctl status jupyter
# long
sudo journalctl -u jupyter.service | tail
The run_jupyter.sh command 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:
SLACKHOOK="https://hooks.slack.com/services/👾👾/👾👾/👾👾👾"
HOST_IP=$(hostname -I)
PAYLOADSLACK='{"text":"somedescriptive name for your machine '$HOST_IP'"}'
curl -X POST -H 'Content-type: application/json' --data "$PAYLOADSLACK" $SLACKHOOK
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. As always remember not to put slack webhooks in your GitHub repo or else you will get adverts. The above code works if the network is ethernet but with WiFi the connection established after then network chip is awake (After=network.target in the service), so a wee tweak is required:
SLACKHOOK="https://hooks.slack.com/services/👾👾/👾👾/👾👾👾"
HOST_IP=$(hostname -I)
while true
do
echo 'Testing network...'
PAYLOADSLACK='{"text":"companion_pi '$HOST_IP'"}'
curl -X POST -H 'Content-type: application/json' --data "$PAYLOADSLACK" $SLACKHOOK
if [ $? -eq 0 ]
then
break
fi
sleep 1
done
An alternative to Slack would be to announce audibly what the IP address is, which 110% naff, but works. This requires eSpeak and pyttsx3 in Python and if there is no audio jack, PWM-audio. For more see my Furby project, where this combo is used.

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