In this one, we are gonna take a look at some quite useful add-ons on top of the ones discussed earlier. These have been accumulated from various other sources I have been going through.
More Extensions
Notebook extensions let you move beyond the general vanilla way of using the Jupyter Notebooks. Notebook extensions (or nbextensions) are JavaScript modules that you can load on most of the views in your Notebook’s frontend. These extensions modify the user experience and interface.
Let us discuss some of the useful extensions that have been missed out in the last article.
1. Hinterland
Hinterland enables code autocompletion menu for every keypress in a code cell, instead of only calling it with the tab. This makes Jupyter notebook’s autocompletion behave like other popular IDEs such as PyCharm and VCS.
AWESOME!
2. Split Cells Notebook
This extension splits the cells of the notebook and places then adjacent to each other. These will be of great use for comparing things for a presentation.
3. Snippets
With this you get a drop-down menu to the Notebook toolbar that allows easy insertion of code snippet cells from many popular libraries into the current notebook.
4. Collapsible Headings
Collapsible Headings allows the notebook to have collapsible sections, separated by headings. So in case you have a lot of dirty code in your notebook, you can simply collapse it to avoid scrolling it again and again.
DOPE!!
Slideshow
Notebooks are an effective tool for teaching and writing explainable codes. Jupyter Notebooks can be easily converted to slides and we can easily choose what to show and what to hide from the notebooks.
NO MORE POWERPOINT!!
This can be done in two ways:
1. Jupyter’s inbuilt Slide
In your notebook navigate to View → Cell Toolbar → Slideshow. A light grey bar appears on top of each cell, and you can customize the slides.
Now go to the directory where the notebook is present and enter the following code:
jupyter nbconvert *.ipynb --to slides --post serve
# insert your notebook name instead of *.ipynb
The slides get displayed at port 8000. Also, a .html
file will be generated in the directory, and you can also access the slides from there.
Here, you can see the code but cannot edit it. RISE plugin offers a solution.
2. RISE plugin
This I have already mentioned in my last article. RISE is an acronym for Reveal.js — Jupyter/IPython Slideshow Extension. It utilized the reveal.jsto run the slideshow. This is super useful since it also gives the ability to run the code without having to exit the slideshow. Install with:
#Using conda
conda install -c conda-forge rise
#Using pip
pip install RISE
Install the JS and CSS in the proper places:
jupyter-nbextension install rise --py --sys-prefix
#enable the nbextension:jupyter-nbextension enable rise --py --sys-prefix
Now we notice a new extension that says “Enter/Exit RISE Slideshow.”
Click on it. You get your interactive slides. Awesome!
Jupyter Widgets
Jupyter 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. Install them with:
# pip
pip install ipywidgets
jupyter nbextension enable --py widgetsnbextension
# Condaconda install -c conda-forge ipywidgets
#Installing ipywidgets with conda automatically enables the extension
For complete details, you can visit their Github repository. For now, let’s have a look at some of the widgets.
- Interact
It is the easiest way to get started using IPython’s widgets. The interact
function (ipywidgets.interact
) automatically creates a user interface (UI) controls for exploring code and data interactively.
There are few actions less efficient in data exploration than re-running the same cell over and over again, each time slightly changing the input parameters. The ideal solution to this issue would be interactive controls to change inputs without needing to rewrite or rerun code. We’ll see how to get started with IPython widgets ( ipywidgets
), interactive controls you can build with one line of code. This library allows us to turn Jupyter Notebooks from static documents into interactive dashboards, perfect for exploring and visualizing data.
IPython widgets, unfortunately, do not render on GitHub or nbviewer but you can still access the notebook and run locally.
# Start with some imports!
from ipywidgets import interact, interact_manual
import ipywidgets as widgets
def f(x):
return x
# Generate a slider
interact(f, x=10,);
# Booleans generate check-boxes
interact(f, x=True);
# Strings generate text areas
interact(f, x='Hi there!');
We can use this @interact
decorator to quickly turn any ordinary function into an interactive widget.
@interact
def show_articles_more_than(column='claps', x=5000):
return df.loc[df[column] > x]
Source: https://towardsdatascience.com/interactive-controls-for-jupyter-notebooks-f5c94829aee6
import os
from IPython.display import Image
@interact
def show_images(file=os.listdir('images/')):
display(Image(fdir+file))
Source: https://towardsdatascience.com/interactive-controls-for-jupyter-notebooks-f5c94829aee6
Now we can quickly cycle through all the images without re-running the cell. This might actually be useful if you were building a convolutional neural network and wanted to examine the images your network had missclassified.
The uses of widgets for data exploration are boundless. Another simple example is finding correlations between two columns:
Widget for correlation between two columns.
2. Play Widget
The Play widget is useful to perform animations by iterating on a sequence of integers at a certain speed. The value of the slider below is linked to the player.
play = widgets.Play(
# interval=10,
value=50,
min=0,
max=100,
step=1,
description="Press play",
disabled=False
)
slider = widgets.IntSlider()
widgets.jslink((play, 'value'), (slider, 'value'))
widgets.HBox([play, slider])
3. Date picker
The date picker widget works in Chrome and IE Edge but does not currently work in Firefox or Safari because they do not support the HTML date input field.
widgets.DatePicker(
description='Pick a Date',
disabled=False
)
4. Color picker
widgets.ColorPicker(
concise=False,
description='Pick a color',
value='blue',
disabled=False
)
5. Tabs
tab_contents = ['P0', 'P1', 'P2', 'P3', 'P4']
children = [widgets.Text(description=name) for name in tab_contents]
tab = widgets.Tab()
tab.children = children
for i in range(len(children)):
tab.set_title(i, str(i))
tab
6. Widgets for Plots
Interactive widgets are especially helpful for selecting data to plot. We can use the same @interact
decorator with functions that visualize our data:
import cufflinks as cf
@interact
def scatter_plot(
x=list(df.select_dtypes('number').columns), y=list(df.select_dtypes('number').columns)[1:], theme=list(cf.themes.THEMES.keys()), colorscale=list(cf.colors._scales_names.keys())):
df.iplot(
kind='scatter',
x=x,
y=y,
mode='markers',
xTitle=x.title(),
yTitle=y.title(),
text='title',
title=f'{y.title()} vs {x.title()}',
theme=theme,
colorscale=colorscale
)
Here, cufflinks+plotly combination is used to make an interactive plot with interactive IPython widget controls.
If the plot was a little slow to update, we can use @interact_manual
which requires a button for updating.
7. Qgrid
Qgrid is a Jupyter notebook widget but mainly focussed at dataframes. It uses SlickGrid to render pandas DataFrames within a Jupyter notebook. This allows you to explore your DataFrames with intuitive scrolling, sorting and filtering controls, as well as edit your DataFrames by double-clicking cells. The Github Repository contains more details and examples. Install:
#with pip
pip install qgrid
jupyter nbextension enable --py --sys-prefix qgrid
# only required if you have not enabled the ipywidgets nbextension yet
jupyter nbextension enable --py --sys-prefix widgetsnbextension
#with conda
# only required if you have not added conda-forge to your channels yet
conda config --add channels conda-forge
conda install qgrid
Embedding URL/ PDF/Youtube
Using IPython’s display module, you can easily embed an URLs, PDFs, and videos into your Jupyter Notebooks.
URLs
#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)
PDFs
from IPython.display import IFrame
IFrame('https://arxiv.org/pdf/1406.2661.pdf', width=800, height=450)
Youtube Videos
from IPython.display import YouTubeVideo
YouTubeVideo('9SA7FaKxZVI', width=800, height=300)
Jupyter Themes
Just like your IDE’s schema or theme setting, Jupyter also have themes which can get the same thematic feeling. These themes also enhance the presentation of the code. For installation:
pip install jupyterthemes
List out the available themes with:
jt -l
Though not high in numbers, these are still pretty good and more are gonna come in future. Choose your favourite with:
jt -t <name of the theme>
Reset to original with:
jt -r
Chesterish and solarizedl
Wrapping Things Up
I urge you to read it as this one is adding more content to the last one. Install these Jupyter Notebook add-ons, spend some time figuring out which ones are useful to you, and improve your productivity. While none of these is life-changing, they all add just enough benefit to be worthwhile, cumulatively saving you hours of valuable development time.
No comments:
Post a Comment