10 Jupyter Notebook Hacks You'll Love!

10 Jupyter Notebook Hacks You'll Love!

“ Give me six hours to chop down a tree and I will spend the first four sharpening the axe.” – Abraham Lincoln

Jupyter Notebook, formerly known as the IPython notebook, is a popular tool used by data scientists to perform end to end data science workflows – data cleaning, statistical modeling, building and training machine learning models, visualizing data, and a lot more.

In this article, I'll discuss some tips that can make your work easier and ultimately make your life easier. Let's go!

Hack 1: Checking function documentation

Use Shift + Tab to get the documentation of any function. This can be useful when you can't remember the parameters for a particular method or function, or maybe you have a question about what the function does. The image below shows the documentation of the print function. Screenshot (169).png

Hack 2: To interrupt a kernel

Be sure the cell is in command mode, then press I twice( I + I). This can come in handy when you're running an infinite loop, want to stop a code from running because you saw some errors in it, or in my case, if your model is taking too long to compute.

Hack 3: To see the list of variables

Type %who in a code cell to get a list of variables present in your notebook. The output shows variable names, their data types, and the data stored by the variables.

Hack 4: Hide irrelevant Matplotlib texts

There's this annoying line/lines (depending on how many plots) that appear when you create a plot. Screenshot (173)_LI.jpg Add a semi-colon at the end of the code to get rid of the texts. Screenshot (174).png Better right?

Hack 5: Use full browser width

Don't waste your screen's real estate. Run the code below in an empty cell to use the full width of your browser.

from IPython.core.display import display, HTML
display(HTML("<style>.container { width:100% !important; }</style>"))

That way, you won't have to scroll left and right to see a cell's output/input.

Hack 6: Display multiple outputs at once

Jupyter Notebook displays output one at a time, and this can be tiring when you have several variables in a cell. To analyze multiple outputs together, run this code in an empty cell.

from IPython.core.interactiveshell import InteractiveShell InteractiveShell.ast_node_interactivity = 'all'
# demo of hack6

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

fave_series = ['Shameless', 'Blacklist', 'Breaking Bad', 'Revenge']
fave_acts = ['Carl Gallagher', 'Aram Mojtabai', 'Walter White', 'Nolan Ross']

fave_series
fave_acts

#output
['Shameless', 'Blacklist', 'Breaking Bad', 'Revenge']
['Carl Gallagher', 'Aram Mojtabai', 'Walter White', 'Nolan Ross']

The example above shows that we can have multiple outputs at once without using the print function.

Hack 7: To type LaTeX in markdown cells

Enclose the LaTex text within dollars ($) signs. This can be useful when writing scientific/mathematical formulas and equations. Running the code below in a markdown cell will display the infamous Almighty Formula used in algebra.

$x = \frac{{ -b \pm \sqrt {b^2 - 4ac} }}{{2a}}$

Hack 8: Present your notebook as slides

Nbconvert is a tool used to convert notebooks into documents; it can convert your notebook's content into a presentation format. Here's how:

  • Go to View from the toolbar, then select Slideshow
  • Select the type of slide you want from the 'Slide Type' option of each cell. The Slide Type option can be found in the top right corner of each cell.
  • Type this command in your command prompt/terminal: “jupyter nbconvert [file_name] –to slides”
  • The converted file can be found in the same folder you're writing the code. It usually has an HTML extension, for example, hacks.html

Hack 9: View notebook history

One time, I wrote code for a hackathon to attend a Bootcamp (shout-out to Data Science Nigeria) and forgot the parameters I used to achieve my model's highest accuracy score. I'd deleted many cells and ran more commands than I could possibly remember; I was sweating in all possible places!😓

Stack overflow saved me that day. I used %history to see the list of all commands used in the notebook and was able to retrace my steps.

P.S. I qualified for the Bootcamp, and it ended yesterday. It was a beautiful and memorable event. Screenshot (177).png

Hack 10: Use Images in Jupyter Notebook

They say a picture speaks more than a thousand words; pictures can be used to explain texts or even codes. Here's how to insert images in Jupyter Notebook;

![alt text](image url)
#for example
![data science meme](https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTPtcuCy41wdJ0iw3CDulKzFaR7AcerFC1RTw&usqp=CAU)

The image below is the output of running the above code in a markdown cell meme9.jpg

EXTRAS: Because I'm a nice person😉, I'll throw in a couple of extra hacks. These ones are for aesthetics.

Bonus 1: Jupyter Themes

Many people don't know there's an alternative to the default light mode of Jupyter Notebook. meme5.jpg If you're like me and don't like working in a light mode or want to make your environment more beautiful/attractive, follow the steps below to change to a dark and beautiful theme.

  • Install jupyter-themes using either:

Anaconda: conda install -c conda-forge jupyterthemes

Pip: pip install jupyterthemes

  • Use jt -l to check the list of themes; there are about seven available themes

  • Select a theme with this command: jt -t For example, jt -t monokai to select the monokai theme. That's my favourite.

  • Add -T -N to toggle the toolbar and notebook name on. For example, jt -t onedork -T -N

  • If you still prefer the default theme after checking out the themes, then hey, no pressure, just type jt -r in the command prompt, and you're back to square one!

  • These commands should be run in the command prompt. I recommend using the conda command if you have Anaconda installed.

Check this repo for more theme commands. I found this site helpful too.

Bonus 2: Fonts and Font Size

You can further customize your notebook by changing the code fonts, text fonts, notebook fonts, and their sizes. I used the code below to change my fonts and their sizes.

jt -t monokai -T -N -f fira -fs 11 -tf roboto -tfs 11 -nf ptsans -nfs 12 -cellw 80%

# f = font, fs = font size, tf= text font, nf = notebook font, cellw = cell width

That's it for now, guys. I hope you find these tips helpful. Feel free to add other hacks you know in the comment section below.

Thank you for reading.

This article is submitted in fulfillment of the first task given under the Hashnode Technical Writing Bootcamp II.