DEV Community

Prateek Sharma
Prateek Sharma

Posted on • Originally published at keytodatascience.com

Jupyter Notebook Extensions You Didn’t Know You Needed

Jupyter notebook is a very powerful tool to do EDA analysis, present findings and share code. Over time, I came to know of a few extensions (plugins) that changed that were not present in the original Jupyter notebook. However, implementing these add-ons changed the look and feel of Jupyter notebook and it has been more fun to work on.

Table Of Contents

1. Themes (Dark theme) Extension

What if I tell you there are more to Jupyter than white background notebook. So if are bored with the custom theme, you are in luck. From YouTube to Instagram, the dark theme can be found everywhere. So this add-on is also available in the notebook with many more.

  • Whenever you change the theme, the notebook needs to be reloaded.
  • Below commands can either be run on Anaconda Prompt or within the Jupyter Notebook by placing ‘!’ before the command. Let’s start with the installation
pip install jupyterthemes
Enter fullscreen mode Exit fullscreen mode

List of available themes

jt -l
Enter fullscreen mode Exit fullscreen mode

Install the dark theme with

jt -t chesterish
Enter fullscreen mode Exit fullscreen mode

Restore the original theme any time using

jt -r
Enter fullscreen mode Exit fullscreen mode

Chesterish Theme (Dark Theme)

Click here to find the Jupyterthemes GitHub repository.

2. Nbextensions

Extend the notebook experience by using one of the most popular extension package Nbextensions which is a bundle of useful plugins.

You can install Nbextensions from Jupyter notebook using

conda install -c conda-forge jupyter_contrib_nbextensions
conda install -c conda-forge jupyter_nbextensions_configurator 
Enter fullscreen mode Exit fullscreen mode

Once they’re installed, you’ll see a nbextensions tab. Explore away!

After the installation of Nbextensions go to the home page of your jupyter notebook, and you will find a new NBExtensions tab.

NBExtensions

Just click on the extension of your choice to enable it. For further information on extension scroll down after selecting the extension.

Scroll down selected Nbextensions extension for its description

Popular extensions

1. Execute Time —

ExecuteTime extension displays the time when each cell has been executed and how long it took.
Execute Time

2. Hinterland —

This enables code autocompletion menu for every keypress in a code cell, instead of calling it with tab.

Hinterland autocomplete

3. Snippets Extension has 2 popular use cases:

3(a). Snippets Menu —

Adds a drop-down menu to the Notebook toolbar that allows easy insertion of basic code snippet cells into the current notebook.
Snippets Menu Extension

3(b). Snippets —

As the name suggests you can create snippets of codes that you reuse a lot.

Whenever I open a new Jupyter notebook, I used to copy-paste a lot of libraries and use the custom settings for some of them. Well not anymore.

Pandas, numpy, matplotlib are some of the typical libraries that are always used. So to automate this, Snippets plugin comes to rescue.

Snippets extension adds a drop-down menu to the Notebook toolbar that allows easy insertion of code snippets into the notebook cells.

To add a new snippet that imports numpy, matplotlib, and other libraries, the snippets.json file needs to be modified. The file can be found at C:\Users..\AppData\Local\Continuum\anaconda3\share\jupyter\nbextensions\snippets location. This path may vary for your system. Or you can simply search for snippets.json on your system. Make the following changes to the JSON file.

{
     "snippets" : [
         {
             "name" : "example",
             "code" : [
                 "# This is an example snippet!",
                 "# To create your own, add a new snippet block to the",
                 "# snippets.json file in your jupyter nbextensions directory:",
                 "# /nbextensions/snippets/snippets.json",
                 "import this"
             ]
         },
{         
"name" : "StandardImport",
       "code" : [
             "#IMPORT ALL THE LIBRARIES BELOW",
             "import pandas as pd",
             "import numpy as np",
             "import matplotlib.pyplot as plt",
             "%matplotlib inline",
             "pd.set_option('display.max_columns',200)",
             "pd.set_option('display.max_rows',200)",
             "print('StandardImport Completed')"
         ]
     }
   ]
 }
Enter fullscreen mode Exit fullscreen mode

Now, the changes will be reflecting in the Snippet extension.

Snippets Extension

Nbextensions is open-source and tons of extensions have been developed for Jupyter Notebooks. You can find the extension list here.

3. Widget Extensions

Widgets are eventful python objects that have a representation in the browser, often as a control like a slider, textbox, etc. Widgets can be used to build interactive GUIs for the notebooks.

Let’s start with the installation

pip install ipywidgets
jupyter nbextension enable --py widgetsnbextension
Enter fullscreen mode Exit fullscreen mode

The ipywidgets library has an interact function that creates interactive user interface (UI) controls for exploring data.

#Import widgets library
from ipywidgets import interact
import ipywidgets as widgets 
Enter fullscreen mode Exit fullscreen mode

1. Slider Widget —

Pass this function as the first argument to interact with an integer argument e.g. (x=10), a slider will be generated.

def slider(x):
    return x  
interact(slider, x=10,);
Enter fullscreen mode Exit fullscreen mode

Slider Widget Extension

2. Date picker Widget —

The date picker widget works in Chrome and IE Edge but does not currently work in Firefox or Safari as they do not support the HTML date input field.

 widgets.DatePicker(
    description='Pick a Date',
    disabled=False)
Enter fullscreen mode Exit fullscreen mode

Date picket calender widget
You can find widgets extension docs here.

Conclusion

These are a list of extensions available in the Jupyter Notebooks that I found useful. They have helped me in improving my efficiency while checking the execution time of cells, reducing repetitive tasks. nbextensions package provides many other extensions. Do check them out and try to see which ones you find useful. Hopefully, they will be able to save you some time and provide you a better UI experience. Also, feel free to suggest other useful features in the comments.

If you have any comments or questions feel free to leave your feedback below. You can visit my blog KeyToDataScience or follow me up at Medium.

Start your Data Science Journey here.

Top comments (0)