Jupyter notebook is a tool to interactively develop with python. When working with datascience, it can through the use of different block of code, text or rich content make it all integrate smoothly in one place.

It’s called notebook a notebook because you can run bits of code, add any content, edit and save it all as one document.

All cells or blocks of code have access to the same python Kernel. Hence, what has been run in one cell can be used in another.

Setup

If you’re not sure of your python environment, follow how to manage your python environment article. Once python3 is set up, install jupyter with:

pip install jupyter

Then start a jupyter notebook using:

jupyter notebook

This will run jupyter notebook which you can then access through your web browser. Create a new notebook file ending with .ipynb which will contain all the necessary information of your notebook.

You can ignore the .ipynb_checkpoints/** folder in your .gitignore.

Customization

Extensions

Let’s add some jupyter packages for customization, first install the dependencies:

pip install jupyter_nbextensions_configurator jupyter_contrib_nbextensions
jupyter contrib nbextensions install

Everytime you install a package or do an extension modification, you’ll have to restart the notebook for the effect to be taken into consideration. You should now see a range of feature in a Nbextension tab.

Looks

You can also customize your notebook looks…

pip install jupyterthemes

Here is a full list of the available themes from towardsdatascience.com which is a must see with so many great articles about jupyter notebook and datascience!

jupyter-theme.png

You can modify the theme by using these commands:

jt -l                           # To list the available themes
jt -t oceans16 -T -N -kl        # To add the theme with tool bar and logo
jt -r                           # To remove the theme

Like before once you run those commands you will need to restart your jupyter notebook for theme to be accounted for. Basically it adds a custom.css in your ~/.jupyter/ folder.

Magic commands

Jupyter magic commands come from IPython and will usually start with %, use %magic for info inside a jupyter code cell.

The notebook is not only for python, you can change the language at the beginning of a cell using:

%% ruby  # This cell will output "hello world!"
puts 'hello world!'

You can also use other language, find the list on the jupyter edu’s book

Another cool magic command is the %%capture which will capture the output of a code cell when displaying the content is not necessary for example.

Finally, the last one is the ! in front of the next command allow me to execute a command directly in the terminal:

!pip install pandas

This way you can install packages directly from your jupyter notebook, but you can also run other bash commands or scripts.

Conclusion

There’s so much you can do with jupyter notebook, and it makes temporary scripting with python so much easier! Also find a fully functional example in /assets/notebook/ where I used all the tricks in this article within 🎉