Jupyter notebook extensions and tools

Catalogue
  1. 1. Collapse heading extension / code folding extension
    1. 1.1. Installation
    2. 1.2. Enable “Collapsible Headings”
      1. 1.2.1. Extensions that I usually enable
    3. 1.3. Some extra sweet?
  2. 2. Use Jupyter Themes
  3. 3. Other interesting tools
    1. 3.1. Interactive dataframe output
    2. 3.2. Embed URL, PDF, or Videos!
  4. 4. Other tips
    1. 4.1. Run jupyter without token
    2. 4.2. Replace tab with space

Here are some extensions and tips that help we get nice notebooks and easily presenting to others.

Collapse heading extension / code folding extension

The jupyter contrib nbextensions Python package contains a code-folding extension that can be enabled within the notebook.

Installation

To install using command line:

1
2
3
4
5
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user

# also can install with conda, this below command will automatically install the js and css files, so no need for the 2nd command shown above
# conda install -c conda-forge jupyter_contrib_nbextensions

To make life easier in managing them, I’d also recommend the jupyter nbextensions configurator package. This provides an extra tab in your Notebook interface from where you can easily (de)activate all installed extensions.

1
2
pip install jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

Then the package will provide a UI that you can open in your browser.
If your dashboard is at

1
http://localhost:8888/custom/base/url/tree

then you’ll find the configurator UI page at

1
http://localhost:8888/custom/base/url/nbextensions

It will also be opened from the tab on your dashboard

Enable “Collapsible Headings”

Now, go to “Nbextensions” tab, and enable the functions you need:

Extensions that I usually enable

Collapsible Headings
Limit Output: 1000
Snippets
Table of Contents
Codefolding
Hinterland
Split Cells Notebook

Some extra sweet?

It’s nice to have all those foldings inside our notebooks when we are doing DS work and presenting this to others. However, when we share the notebooks to others, especially those who don’t have the same jupyter environment setup like us, how can we make sure they can see the same nice formatted report?
Well, run the following magic command and you will see an html exported with the same “collapsible heading” format!

1
jupyter nbconvert --to html_ch some_notebook.ipynb

jupyter nbconvert supports many other formats e.g slides, latex, pandoc, please refer to their documentation

Use Jupyter Themes

Got tired with the default notebook style? You can change to other themes and different plotting styles!

1
2
3
4
5
6
pip install jupyterthemes
jt -l
# selecting a particular theme
jt -t <name of the theme>
# reverting to original Theme
jt -r

You can have a look of how each of the theme look like here

Other interesting tools

Interactive dataframe output

1
2
3
# only required if you have not added conda-forge to your channels yet
conda config --add channels conda-forge
conda install qgrid
1
2
import qgrid
qgrid.show_grid(df, show_toolbar=True)

Embed URL, PDF, or Videos!

1
2
3
4
5
6
7
8
9
#Note that http urls will not be displayed. Only https are allowed inside the Iframe
from IPython.display import IFrame
IFrame('https://en.wikipedia.org/wiki/HTTPS', width=800, height=450)

from IPython.display import IFrame
IFrame('https://arxiv.org/pdf/1406.2661.pdf', width=800, height=450)

from IPython.display import YouTubeVideo
YouTubeVideo('mJeNghZXtMo', width=800, height=300)

Other tips

Run jupyter without token

1
jupyter notebook --ip='*' --NotebookApp.token='' --NotebookApp.password='' --port=51111 --no-browser

Replace tab with space

Edit -> Replace -> Use regex -> \t -> replace with 4 spaces