10 Productive Jupyter Notebook Hacks, Tips and Tricks!

Ram Dewani 20 Apr, 2020 • 6 min read

Overview

  • Jupyter notebooks are a great tool for data scientists but we can make them even better using these hacks, tips, and tricks
  • We will demonstrate each Jupyter notebook hack in this article using code as well as a video demonstration

 

Introduction

A full month has elapsed since most of India Inc. and the world went into lockdown mode. Most of us have been working from home during this period and like me, I’m sure you’ve reflected on the value of a positive and well functioning physical environment.

Personally, I have adopted different techniques to become more efficient and productive during this time. This got me thinking – how can I become even more efficient in my projects, especially when it comes to Jupyter notebooks?

Like a lot of data scientists today, I spend a lot of time with my head buried in Jupyter notebooks. Could I translate my efficiency into my programming habits? So I took it upon myself and compiled a list of 10 Jupyter Notebook hacks that helped me out and will definitely make you a pro as well!

jupyter_hacks

Jupyter Notebook is the most widely used environment for data science purposes. It is an open-source web application to create code, display visualizations and to perform data analysis. As the name suggests, it was developed for Julia, Python, and R. Later on, it added more languages to its collection.

This is part three of my Data Science hacks, tips, and tricks series. I highly recommend reading the first and second parts.

I have also converted my learning into a free course that you can check out:

Also, if you have your own Data Science hacks, tips, and tricks, you can share it with the open community on this GitHub repository: Data Science hacks, tips and tricks on GitHub.

We are posting these hacks daily on social media platforms like LinkedIn, Twitter, Facebook. Make sure to follow #avhackoftheday to get your daily dose of freshly brewed data science hacks, tips, and tricks!

 

Table of Contents

We’ll cover these Jupyter Notebook hacks, tips, and tricks in this article:

  1. Jupyter Notebook Hack #1 – Interactive Shell
  2. Jupyter Notebook Hack #2 – Multicursor in Jupyter Notebook
  3. Jupyter Notebook Hack #3 – %who
  4. Jupyter Notebook Hack #4 – %history
  5. Jupyter Notebook Hack #5 – Changing Jupyter themes
  6. Jupyter Notebook Hack #6 – Change cell width
  7. Jupyter Notebook Hack #7 – Share notebooks via nbviewer
  8. Jupyter Notebook Hack #8 – Nbconvert to present Jupyter notebook as slides
  9. Jupyter Notebook Hack #9 – %prun
  10. Jupyter Notebook Hack #10 – %%history

 

Jupyter Notebook Hack #1 – Interactive Shell

Have you ever wondered whether you can get multiple outputs in the same cell? Well, here you go! Now you can print multiple statements within the same cell. This gives you the ability to analyze multiple outputs together and provides high flexibility.

 

Jupyter Notebook Hack #2: Multicursor in Jupyter Notebook

This is one of the most exciting hacks in a Jupyter notebook. Do you ever wonder what you would do if you made the same mistake at multiple places? It’d be a daunting task to go and rectify it everywhere.

Let’s take an example where I committed a silly mistake of naming the variable wrong at multiple places. Here’s the good news – I was able to correct them in one go using multicursor. All you have to do is – CTRL + Click at the desired points!

 

Jupyter Notebook Hack #3: %who – List of Variables

%who is a useful magic command that returns the variables present in the global space along with its type. This is particularly useful where you have a long notebook. You can also pass arguments to get a list of specific variables.

We can see how it works in this example:

 

Jupyter Notebook Hack #4: %history – List of commands

I was recently writing my code in a Jupyter notebook and it consisted of at least 100 cells. During this process, I also deleted many cells. Now, when I had to look at the summary of the flow of my notebook, I needed to go through all the cells one by one. As you can imagine, it was quite time-consuming!

This is why Jupyter Notebook provides a special magic command – %history. It provides a list of all the commands given in the notebook. Pretty cool, right?

 

Jupyter Notebook Hack #5 – Changing the Theme of a Jupyter Notebook

I know a lot of you will love this hack.

Data science requires a lot of coding and it can get monotonous using the same interface every day. You can make your environment much cooler by changing the background of your Jupyter notebook to a dark theme. jupyter-themes provides an easy way to change the theme, fonts and much more in your Jupyter notebook.

Follow the below steps:

  1. Install jupyter-themes:
    • using anaconda conda install -c conda-forge jupyterthemes
    • using pip pip install jupyterthemes
  2. Check the list of themes – jt - l
  3. Select a theme jt -t chesterish
  4. To restore to default theme – jt -r

 

Jupyter Notebook Hack #6 – Change Cell Width

I use a high-resolution screen and I wanted to make use of the extra space by expanding cell width. So how do I do it?  jupyter-themes provides a very simple way to alter cell width height and width:

  1. Install jupyter-themes:
    • using anacondaconda install -c conda-forge jupyterthemes
    • using pipconda install -c pip install jupyterthemes
  2. Change the theme, cell width, cell height jt -t chesterish -cellw 100% lineh 170

 

Jupyter Notebook Hack #7: Share notebooks via Jupyter nbviewer

I often come across situations where I needed to share my Jupyter notebooks with my colleagues or peers. So what’s the simplest way to do this? Enter Nbviewer.

Nbviewer is a web application that lets us enter the URL of a Jupyter Notebook file, renders that notebook as a static HTML web page, and gives us a stable link to that page which we can share with others.

You can also use Binder to open the static web page in an executable environment. Pro-tip – You can paste the GitHub links mentioned in this article into binder and you’ll have all the codes at your disposal!

 

Jupyter Notebook Hack #8: Nbconvert to Present your Notebook as Slides

Jupyter provides a very powerful tool that will help you convert your notebook into useful documents – Nbconver.

This is used for converting Jupyter’s ipynb file into another static format including HTML, LaTeX, PDF and more. It can be a really handy tool to straightaway convert your findings into a presentation format. Now that’s productivity at its peak!

In this example, we’ll convert a small notebook into the form of slides. Nbconvert is the tool that powers the Nbviewer mentioned in the previous hack.

Here’s how to do it:

  1. Open a Jupyter notebook
  2. In the toolbar, go to View>>Slideshow
  3. Now each cell will have a “Slide Type” in the top right corner. Select the type of slide depending on your content
  4. Go to command prompt/terminal and type the following command –  “jupyter nbconvert [file_name] –to slides”
  5. Go to the folder where you are running the code and you’ll find, “[file_name.html]”
  6. Open the file and use the keys to change the slides

 

Jupyter Notebook Hack #9: %prun – Run Python Profiler

Jupyter notebook provides a simple wrapper to run Python Profiler – %prun. A profiler helps you analyze your code so that you can optimize the pain points. It provides you with the number of function calls and the execution time. Other information provided is:

  • ncalls – number of calls
  • tottime – The total time spent in the given function (excluding time made in calls to sub-functions)
  • percall – the quotient of tottime divided by ncalls
  • cumtime –  the total time spent in this and all subfunctions (from invocation till exit)
  • percall – the quotient of cumtime divided by primitive calls

Let’s see this in the example below:

 

Jupyter Notebook Hack #10: %%heat – Heatmap over Code

This last hack is one of the true gems out there. It’s one of the easiest ways to visualize the profiling of your code. By using %%heat, you’ll get an output of your code in the form of a heatmap. This will help you recognize the most time-consuming statements and help you optimize it.

Let’s checkout the steps in your Jupyter notebook:

  1. !pip install py-heat-magic
  2. To load the heat as a magic function in your notebook – %load_ext heat
  3. Use %%heat in your notebook cell for which you want to get the heatmap

 

End Notes

In this article, we covered 10 Jupyter notebook hacks, tips, and tricks to save you a lot of time and make your coding experience a whole lot more enjoyable. Go ahead and try these out – you’ll love them!

Let me know your Data Science hacks, tips and tricks in the comments section below.

Ram Dewani 20 Apr 2020

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

Clear

Dr.D.K.Samuel
Dr.D.K.Samuel 16 Apr, 2020

Well written. Useful. Thanks. Will you please write on saving animated charts as MP4 and as animated gifs. This is to insert them in Microsoft PowerPoint.

Related Courses