jump to content
Snippets and tips
  1. Home
  2. Holiday
  3. Posts
  4. Tools
  5. Tags
  6. Categories

Jupyter

Tips and tricks around Jypyter

Jupyter is quite useful

Executing Python notebooks on a remote powerful server - what more? Here some tips.

Formatting #

Jupyter notebooks are edited in online web-sessions, where editor extensions are not available.

But pretty printing can be done:

First, install the following packages

1
pip install black blackcellmagic

After that in the jupyter notebook, add as a first line

1
%load_ext blackcellmagic

This will load an extension into python.

In all cells, prepend the text one wants to prettify with

1
%%black

When the cell gets executed, the text gets replaced (without the %%black) with a pretty version.

Running Jupyter on a server #

I have a separate server to run Jupyter on. In order to keep it going, it is started in a tmux session:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/bin/bash

if [ -z "$TMUX" ] 
then
  echo "starting tmux session"
  exec tmux new-session -s Jupyter $0
fi
echo "continuing"
source ~/prj/.myenv/bin/activate
echo $VIRTUAL_ENV
sleep 5
jupyter notebook --ip=0.0.0.0 --port=2345 --no-browser
echo "finally..."
sleep 10

This starts a jupyter instance that is accessible from other machines. For it to work, one needs to set a password.

Reuse system site packages #

1
python -m venv --system-site-packages myenv