Jupyter Notebook Extensions You Didn’t Know You Needed
Tags: python, productivity, machinelearning, datascience
Published: 2020-02-16 20:52:48

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](#themes-extension)
* [2. Nbextensions](#nbextensions)
* [3. Widget Extensions](#widget-extensions)

###1. Themes (Dark theme) Extension <a name="themes-extension"></a>
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

```python
pip install jupyterthemes
```
List of available themes
```python
jt -l
```
Install the dark theme with
```python
jt -t chesterish
```
Restore the original theme any time using
```python
jt -r
```
![Chesterish Theme (Dark Theme)](https://dev-to-uploads.s3.amazonaws.com/i/fsp8k6mhi69itrssu06l.gif )

Click [here](https://github.com/dunovank/jupyter-themes) to find the Jupyterthemes GitHub repository.


###2. Nbextensions <a name="nbextensions"></a>
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
```python
conda install -c conda-forge jupyter_contrib_nbextensions
conda install -c conda-forge jupyter_nbextensions_configurator 
```
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](https://dev-to-uploads.s3.amazonaws.com/i/6tkc3aa21l4ovg9nsm81.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/i/2ffaoyaxpczi83fwtnvo.gif)

**Popular extensions**

####1. Execute Time —
ExecuteTime extension displays the time when each cell has been executed and how long it took.
![Execute Time](https://dev-to-uploads.s3.amazonaws.com/i/vkfuxf5ymdqxp0yryury.jpeg)

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

![Hinterland autocomplete](https://dev-to-uploads.s3.amazonaws.com/i/nol0uehykjio05yw6qwg.gif)

####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](https://dev-to-uploads.s3.amazonaws.com/i/wk203wnnbj4n00a6f0vr.gif)

#####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.

```python
{
     "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')"
         ]
     }
   ]
 }
```

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

![Snippets Extension](https://dev-to-uploads.s3.amazonaws.com/i/mao5bf24lvnm8afspn7w.gif)

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


###3. Widget Extensions <a name="widget-extensions"></a>

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

```python
pip install ipywidgets
jupyter nbextension enable --py widgetsnbextension
```
The ipywidgets library has an interact function that creates interactive user interface (UI) controls for exploring data.

```python
#Import widgets library
from ipywidgets import interact
import ipywidgets as widgets 
```

####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.

```python
def slider(x):
    return x  
interact(slider, x=10,);
```
![Slider Widget Extension](https://dev-to-uploads.s3.amazonaws.com/i/nqncc3bahvdlc3k1m8f4.png)

####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.

```python
 widgets.DatePicker(
    description='Pick a Date',
    disabled=False)
``` 
![Date picket calender widget](https://dev-to-uploads.s3.amazonaws.com/i/0s5hb6g0fd46u3hug6qq.png)
You can find widgets extension docs [here](https://ipywidgets.readthedocs.io/en/stable/examples/Widget%20Basics.html#What-are-widgets?).

###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](https://keytodatascience.com/) or follow me up at [Medium](https://medium.com/@s.prateek3080).

Start your Data Science Journey [here](https://keytodatascience.com/machine-learning-titanic-disaster-problem/).