Getting the Most out of Jupyter Notebooks (2024)

Many users familiar with text editors commonly used Python or even other languages may be more comfortable in a darker environment. Thankfully, Kyle Dunovan has implemented numerous themes that allow you to customize everything from the background color to output text size and font.

Installation is simple and can be run in the notebook with a ‘!’ before any code. This applies to all cmd commands from here on out.

pip install jupyterthemes

You can look at the documentation here: https://github.com/dunovank/jupyter-themes which goes through how to any and every element of your notebooks aesthetic as well as all the available themes.

You can set a theme by simply running

jt -t [Theme Name]# available themes:
[onedork | grade3 | oceans16 | chesterish | monokai | solarizedl | solarizedd | gruxboxd | gruxboxl]

My personal setup is:

jt -t oceans16 -cursc r -cursw 5 -fs 95 -altp -tfs 11 -nfs 115 -cellw 88% -T

It sets the oceans16 theme with a red cursor for more contrast and adjusts the text sizes to match my preferences:

Getting the Most out of Jupyter Notebooks (1)

There are so many extensions that can make using Jupyter Notebook easier and more efficient, here are a few of the ones that I use regularly. Even better is all only one installation is required to access every extension I’m about to recommend

pip install jupyter_contrib_nbextensions

Documentation: https://github.com/ipython-contrib/jupyter_contrib_nbextensions

After installing open a Jupyter Notebook and you’ll see a new tab labeled Nbextensions

Getting the Most out of Jupyter Notebooks (2)

From this tab you can enable as many extensions as you’d like and also see their documentation.

There are so many to explore you should definitely try whichever extensions help your productivity the most. Even I use more than the ones recommended here.

Shortcuts: On a side note, one potential error you may encounter when enabling multiple extensions is that their shortcut keys may overlap. If for some reason the shortcut mentioned in the documentation isn’t working it may because another one is using the same shortcut.

Hinterland:

If you’ve had past experience in Java then this is a must. Rather than pressing TAB to autocomplete code, suggestions will automatically appear as you code

Getting the Most out of Jupyter Notebooks (3)

Variable Inspector:

This one is for those accustomed to R. It adds a button that creates a separate window with all your variables as well a bunch of useful information like data type.

Getting the Most out of Jupyter Notebooks (4)
Getting the Most out of Jupyter Notebooks (5)

Organization:

If you’ve ever been bothered by having to scroll up past your hundred line function or class and then back down to find the cell you were working on previously then you’ve been missing out on numerous extensions that can save you a ton of time and effort.

Table of Contents, Collapsible Headings, and CodeFolding:

Table of Contents extension automatically converts your markdown cells into a table of contents based on heading size and allows you to instantly navigate to any section.

Collapsible Headings makes your notebook even more organized by allowing you to collapse your code based on the heading section.

CodeFolding allows you to collapse sections of code such as loops, functions, classes etc.

Getting the Most out of Jupyter Notebooks (6)
Getting the Most out of Jupyter Notebooks (7)
Getting the Most out of Jupyter Notebooks (8)

Combined these three will make both make your notebook more presentable and reduce pointless scrolling.

ExecuteTime:

You may have noticed in all the above examples that underneath each cell it displays the time it took to run the cell and when it was run. And if you guessed that this functionality was added through a function you would be right! Simply enable it like any other function and that’s it!

Scratchpad:

Sometimes you may want to test a quick snippet of code, be it trying to remember the syntax or see what a method does, without it saving variables or having to delete the cells from your notebook after. With the scratchpad extension, you can now press CRTL+B (or use the small arrow in the bottom right) and run the code using your notebook’s kernel without anything being saved. The scratchpad works just like another cell in your notebook and can even be used for plotting: press SHIFT+ENTER to run the cell.

Getting the Most out of Jupyter Notebooks (9)

A common dilemma I encounter is tracking the progress of a loop especially a long one as its often hard to accurately estimate how long a segment of code will take to run. A simple way to get some information while your code is running is using print statements to track the progress of your loops. However, this can result in an informative but unsightly wall of text.

tqdm is a library that offers elegant and extremely easy to implement progress bars that can be dropped into just about any loop. I can’t stress how easy using this library is!

pip install tqdm_notebook

Documentation: https://github.com/tqdm/tqdm

Simply put tqdm_notebook() around whatever you’re iterating through and that’s it!

Getting the Most out of Jupyter Notebooks (10)

Just importing tqdm and using the tqdm() works for certain loops, but for some iterables it will print a new progress bar below the previous one for each iteration, so using tqdm_notebook is better in general as it creates a widget.

A huge benefit of using this library is that you now get an estimate of how long it will take for your loop to run.

Getting the Most out of Jupyter Notebooks (2024)

References

Top Articles
Latest Posts
Article information

Author: Patricia Veum II

Last Updated:

Views: 5943

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Patricia Veum II

Birthday: 1994-12-16

Address: 2064 Little Summit, Goldieton, MS 97651-0862

Phone: +6873952696715

Job: Principal Officer

Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.